from django.db import models from apps.utils.models import BaseModel from apps.mtm.models import Material from apps.pum.models import Supplier from apps.wpm.models import WmStateOption from rest_framework.exceptions import NotFound, ParseError # Create your models here. class LableMat(BaseModel): """TN: 标签物料""" state = models.PositiveSmallIntegerField('状态', default=10, choices=WmStateOption.choices) material = models.ForeignKey(Material, on_delete=models.CASCADE) batch = models.TextField('批次号', db_index=True) supplier = models.ForeignKey(Supplier, verbose_name='外协供应商', on_delete=models.SET_NULL, null=True, blank=True) notok_sign = models.CharField('不合格标记', max_length=10, null=True, blank=True) defect = models.ForeignKey("qm.defect", verbose_name='缺陷', on_delete=models.SET_NULL, null=True, blank=True) material_origin = models.ForeignKey(Material, verbose_name='原始物料', on_delete=models.SET_NULL, null=True, blank=True, related_name='lm_mo') class LabelTemplate(BaseModel): """TN: 标签模板""" name = models.TextField("名称") commands = models.JSONField("指令模板", default=list, blank=True) process_json = models.JSONField("工序", default=list, blank=True) @classmethod def gen_commands(cls, label_template, label_template_name, tdata): if 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 try: n_commands = [] for item in commands: item = item.format(**tdata) n_commands.append(item) except Exception as e: raise ParseError(f"标签解析错误-{str(e)}") return n_commands