feat: 支持传入label_template_name
This commit is contained in:
parent
7aafa5d9f7
commit
a9f55eb61c
|
|
@ -8,6 +8,7 @@ from apps.utils.serializers import CustomModelSerializer
|
||||||
class TidSerializer(serializers.Serializer):
|
class TidSerializer(serializers.Serializer):
|
||||||
tid = serializers.CharField(label='表ID')
|
tid = serializers.CharField(label='表ID')
|
||||||
label_template = serializers.CharField(label='标签模板ID(可传default)', allow_null=True, required=False)
|
label_template = serializers.CharField(label='标签模板ID(可传default)', allow_null=True, required=False)
|
||||||
|
label_template_name = serializers.CharField(label='标签模板名称', allow_null=True, required=False)
|
||||||
extra_data = serializers.JSONField(label='额外数据', allow_null=True, required=False)
|
extra_data = serializers.JSONField(label='额外数据', allow_null=True, required=False)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,13 @@ class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericVie
|
||||||
queryset = LableMat.objects.all()
|
queryset = LableMat.objects.all()
|
||||||
select_related_fields = ["material", "material_origin", "supplier"]
|
select_related_fields = ["material", "material_origin", "supplier"]
|
||||||
|
|
||||||
def gen_commands(self, label_template, tdata):
|
def gen_commands(self, label_template, label_template_name, tdata):
|
||||||
if label_template == "default":
|
if label_template:
|
||||||
lt = LabelTemplate.objects.get(name="默认标签模板")
|
|
||||||
else:
|
|
||||||
lt = LabelTemplate.objects.get(id=label_template)
|
lt = LabelTemplate.objects.get(id=label_template)
|
||||||
|
else:
|
||||||
|
lt = LabelTemplate.objects.filter(name=label_template_name).first()
|
||||||
|
if not lt:
|
||||||
|
raise NotFound("标签模板不存在")
|
||||||
commands:list = lt.commands
|
commands:list = lt.commands
|
||||||
try:
|
try:
|
||||||
for item in commands:
|
for item in commands:
|
||||||
|
|
@ -40,6 +42,7 @@ class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericVie
|
||||||
"""
|
"""
|
||||||
tid = request.data.get("tid")
|
tid = request.data.get("tid")
|
||||||
label_template = request.data.get("label_template", None)
|
label_template = request.data.get("label_template", None)
|
||||||
|
label_template_name = request.data.get("label_template_name", None)
|
||||||
extra_data = request.data.get("extra_data", {})
|
extra_data = request.data.get("extra_data", {})
|
||||||
try:
|
try:
|
||||||
mb = MaterialBatch.objects.get(id=tid)
|
mb = MaterialBatch.objects.get(id=tid)
|
||||||
|
|
@ -48,9 +51,9 @@ class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericVie
|
||||||
obj, _ = LableMat.objects.get_or_create(state=mb.state, material=mb.material, batch=mb.batch, defect=mb.defect, defaults={"supplier": mb.supplier})
|
obj, _ = LableMat.objects.get_or_create(state=mb.state, material=mb.material, batch=mb.batch, defect=mb.defect, defaults={"supplier": mb.supplier})
|
||||||
rdata = LabelMatSerializer(obj).data
|
rdata = LabelMatSerializer(obj).data
|
||||||
rdata["code_label"] = f"mat{SPLIT_FIELD}{obj.id}"
|
rdata["code_label"] = f"mat{SPLIT_FIELD}{obj.id}"
|
||||||
if label_template:
|
if label_template or label_template_name:
|
||||||
tdata = {**rdata, **extra_data}
|
tdata = {**rdata, **extra_data}
|
||||||
commands = self.gen_commands(label_template, tdata)
|
commands = self.gen_commands(label_template, label_template_name, tdata)
|
||||||
rdata["commands"] = commands
|
rdata["commands"] = commands
|
||||||
return Response(rdata)
|
return Response(rdata)
|
||||||
|
|
||||||
|
|
@ -63,6 +66,7 @@ class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericVie
|
||||||
"""
|
"""
|
||||||
tid = request.data.get("tid")
|
tid = request.data.get("tid")
|
||||||
label_template = request.data.get("label_template", None)
|
label_template = request.data.get("label_template", None)
|
||||||
|
label_template_name = request.data.get("label_template_name", None)
|
||||||
extra_data = request.data.get("extra_data", {})
|
extra_data = request.data.get("extra_data", {})
|
||||||
try:
|
try:
|
||||||
wm = WMaterial.objects.get(id=tid)
|
wm = WMaterial.objects.get(id=tid)
|
||||||
|
|
@ -73,9 +77,9 @@ class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericVie
|
||||||
material_origin=wm.material_origin)
|
material_origin=wm.material_origin)
|
||||||
rdata = LabelMatSerializer(obj).data
|
rdata = LabelMatSerializer(obj).data
|
||||||
rdata["code_label"] = f"mat{SPLIT_FIELD}{obj.id}"
|
rdata["code_label"] = f"mat{SPLIT_FIELD}{obj.id}"
|
||||||
if label_template:
|
if label_template or label_template_name:
|
||||||
tdata = {**rdata, **extra_data}
|
tdata = {**rdata, **extra_data}
|
||||||
commands = self.gen_commands(label_template, tdata)
|
commands = self.gen_commands(label_template, label_template_name, tdata)
|
||||||
rdata["commands"] = commands
|
rdata["commands"] = commands
|
||||||
return Response(rdata)
|
return Response(rdata)
|
||||||
|
|
||||||
|
|
@ -88,6 +92,7 @@ class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericVie
|
||||||
"""
|
"""
|
||||||
tid = request.data.get("tid")
|
tid = request.data.get("tid")
|
||||||
label_template = request.data.get("label_template", None)
|
label_template = request.data.get("label_template", None)
|
||||||
|
label_template_name = request.data.get("label_template_name", None)
|
||||||
extra_data = request.data.get("extra_data", {})
|
extra_data = request.data.get("extra_data", {})
|
||||||
try:
|
try:
|
||||||
mioitem: MIOItem = MIOItem.objects.get(id=tid)
|
mioitem: MIOItem = MIOItem.objects.get(id=tid)
|
||||||
|
|
@ -96,9 +101,9 @@ class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericVie
|
||||||
obj, _ = LableMat.objects.get_or_create(state=10, material=mioitem.material, batch=mioitem.batch, defaults={"supplier": mioitem.mio.supplier})
|
obj, _ = LableMat.objects.get_or_create(state=10, material=mioitem.material, batch=mioitem.batch, defaults={"supplier": mioitem.mio.supplier})
|
||||||
rdata = LabelMatSerializer(obj).data
|
rdata = LabelMatSerializer(obj).data
|
||||||
rdata["code_label"] = f"mat{SPLIT_FIELD}{obj.id}"
|
rdata["code_label"] = f"mat{SPLIT_FIELD}{obj.id}"
|
||||||
if label_template:
|
if label_template or label_template_name:
|
||||||
tdata = {**rdata, **extra_data}
|
tdata = {**rdata, **extra_data}
|
||||||
commands = self.gen_commands(label_template, tdata)
|
commands = self.gen_commands(label_template, label_template_name, tdata)
|
||||||
rdata["commands"] = commands
|
rdata["commands"] = commands
|
||||||
return Response(rdata)
|
return Response(rdata)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue