feat: route采用引用方式允许重复创建
This commit is contained in:
parent
9af23216b6
commit
c997cba6a5
|
@ -58,5 +58,6 @@ class RouteFilter(filters.FilterSet):
|
||||||
"mgroup": ["exact", "in", "isnull"],
|
"mgroup": ["exact", "in", "isnull"],
|
||||||
"mgroup__name": ["exact", "contains"],
|
"mgroup__name": ["exact", "contains"],
|
||||||
"mgroup__belong_dept": ["exact"],
|
"mgroup__belong_dept": ["exact"],
|
||||||
"mgroup__belong_dept__name": ["exact", "contains"]
|
"mgroup__belong_dept__name": ["exact", "contains"],
|
||||||
|
"from_route": ["exact", "isnull"],
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Generated by Django 3.2.12 on 2025-09-02 03:22
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mtm', '0061_material_img'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='route',
|
||||||
|
name='from_route',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='route_f', to='mtm.route', verbose_name='来源路线'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='route',
|
||||||
|
name='parent',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='route_parent', to='mtm.route', verbose_name='上级路线'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -415,8 +415,9 @@ class Route(CommonADModel):
|
||||||
batch_bind = models.BooleanField('是否绑定批次', default=True)
|
batch_bind = models.BooleanField('是否绑定批次', default=True)
|
||||||
materials = models.ManyToManyField(Material, verbose_name='关联辅助物料', related_name="route_materials",
|
materials = models.ManyToManyField(Material, verbose_name='关联辅助物料', related_name="route_materials",
|
||||||
through="mtm.routemat", blank=True)
|
through="mtm.routemat", blank=True)
|
||||||
parent = models.ForeignKey('self', verbose_name='上级路线', on_delete=models.CASCADE, null=True, blank=True)
|
parent = models.ForeignKey('self', verbose_name='上级路线', on_delete=models.CASCADE, null=True, blank=True, related_name="route_parent")
|
||||||
params_json = models.JSONField('工艺参数', default=dict, blank=True)
|
params_json = models.JSONField('工艺参数', default=dict, blank=True)
|
||||||
|
from_route = models.ForeignKey('self', verbose_name='来源路线', on_delete=models.SET_NULL, null=True, blank=True, related_name="route_f")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
x = ""
|
x = ""
|
||||||
|
|
|
@ -247,7 +247,7 @@ class RouteSerializer(CustomModelSerializer):
|
||||||
# if material and process and Route.objects.filter(material=material, process=process).exists():
|
# if material and process and Route.objects.filter(material=material, process=process).exists():
|
||||||
# raise ValidationError('已选择该工序!!')
|
# raise ValidationError('已选择该工序!!')
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
instance = super().create(validated_data)
|
instance:Route = super().create(validated_data)
|
||||||
material_out = instance.material_out
|
material_out = instance.material_out
|
||||||
if material_out:
|
if material_out:
|
||||||
if material_out.process is None:
|
if material_out.process is None:
|
||||||
|
@ -263,12 +263,16 @@ class RouteSerializer(CustomModelSerializer):
|
||||||
if instance.material:
|
if instance.material:
|
||||||
instance.material_out = RouteSerializer.gen_material_out(instance, material_out_tracking, user=self.request.user)
|
instance.material_out = RouteSerializer.gen_material_out(instance, material_out_tracking, user=self.request.user)
|
||||||
instance.save()
|
instance.save()
|
||||||
rx = Route.objects.filter(material_in=instance.material_in, material_out=instance.material_out, process=process).exclude(id=instance.id).first()
|
rx = Route.objects.filter(
|
||||||
|
material_in=instance.material_in, material_out=instance.material_out,
|
||||||
|
process=process).exclude(id=instance.id).order_by("create_time").first()
|
||||||
if rx:
|
if rx:
|
||||||
msg = ""
|
instance.from_route = rx
|
||||||
if rx.routepack:
|
instance.save()
|
||||||
msg = rx.routepack.name
|
# msg = ""
|
||||||
raise ParseError(f"该工艺步骤已存在-{msg}")
|
# if rx.routepack:
|
||||||
|
# msg = rx.routepack.name
|
||||||
|
# raise ParseError(f"该工艺步骤已存在-{msg}")
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
|
@ -294,12 +298,16 @@ class RouteSerializer(CustomModelSerializer):
|
||||||
if instance.material:
|
if instance.material:
|
||||||
instance.material_out = RouteSerializer.gen_material_out(instance, material_out_tracking, user=self.request.user)
|
instance.material_out = RouteSerializer.gen_material_out(instance, material_out_tracking, user=self.request.user)
|
||||||
instance.save()
|
instance.save()
|
||||||
rx = Route.objects.filter(material_in=instance.material_in, material_out=instance.material_out, process=process).exclude(id=instance.id).first()
|
rx = Route.objects.filter(
|
||||||
|
material_in=instance.material_in, material_out=instance.material_out,
|
||||||
|
process=process).exclude(id=instance.id).order_by("create_time").first()
|
||||||
if rx:
|
if rx:
|
||||||
msg = ""
|
instance.from_route = rx
|
||||||
if rx.routepack:
|
instance.save()
|
||||||
msg = rx.routepack.name
|
# msg = ""
|
||||||
raise ParseError(f"该工艺步骤已存在-{msg}")
|
# if rx.routepack:
|
||||||
|
# msg = rx.routepack.name
|
||||||
|
# raise ParseError(f"该工艺步骤已存在-{msg}")
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
|
@ -330,6 +338,12 @@ class RouteMatSerializer(CustomModelSerializer):
|
||||||
model = RouteMat
|
model = RouteMat
|
||||||
fields = "__all__"
|
fields = "__all__"
|
||||||
read_only_fields = EXCLUDE_FIELDS_BASE
|
read_only_fields = EXCLUDE_FIELDS_BASE
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
route:Route = attrs["route"]
|
||||||
|
if route.from_route is not None:
|
||||||
|
raise ParseError("该工艺步骤引用其他步骤,无法修改")
|
||||||
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
class MaterialExportSerializer(CustomModelSerializer):
|
class MaterialExportSerializer(CustomModelSerializer):
|
||||||
|
|
|
@ -368,12 +368,23 @@ class RouteViewSet(CustomModelViewSet):
|
||||||
select_related_fields = ['material',
|
select_related_fields = ['material',
|
||||||
'process', 'material_in', 'material_out', 'mgroup', 'routepack']
|
'process', 'material_in', 'material_out', 'mgroup', 'routepack']
|
||||||
|
|
||||||
def update(self, request, *args, **kwargs):
|
@transaction.atomic
|
||||||
obj:Route = self.get_object()
|
def perform_update(self, serializer):
|
||||||
routepack = obj.routepack
|
ins:Route = self.get_object()
|
||||||
|
if ins.from_route is not None:
|
||||||
|
raise ParseError('该工艺步骤引用其他步骤, 无法编辑')
|
||||||
|
old_m_in, old_m_out, process = ins.material_in, ins.material_out, ins.process
|
||||||
|
routepack = ins.routepack
|
||||||
if routepack and routepack.state != RoutePack.RP_S_CREATE:
|
if routepack and routepack.state != RoutePack.RP_S_CREATE:
|
||||||
raise ParseError('该状态下不可编辑')
|
raise ParseError('该工艺路线非创建中不可编辑')
|
||||||
return super().update(request, *args, **kwargs)
|
ins_n:Route = serializer.save()
|
||||||
|
if ins_n.material_in != old_m_in or ins_n.material_out != old_m_out or ins_n.process != process:
|
||||||
|
raise ParseError("该工艺步骤被其他步骤引用, 无法修改关键信息")
|
||||||
|
|
||||||
|
def perform_destroy(self, instance:Route):
|
||||||
|
if Route.objects.filter(from_route=instance).exists():
|
||||||
|
raise ParseError('该工艺步骤被其他步骤引用,无法删除')
|
||||||
|
return super().perform_destroy(instance)
|
||||||
|
|
||||||
|
|
||||||
class SruleViewSet(CustomModelViewSet):
|
class SruleViewSet(CustomModelViewSet):
|
||||||
|
|
Loading…
Reference in New Issue