43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
from django.db.models import Q
|
|
|
|
|
|
DEFECT_OK = 10
|
|
DEFECT_OK_B = 20
|
|
DEFECT_NOTOK = 30
|
|
|
|
DEFECT_GRADE_CHOICES = (
|
|
(DEFECT_OK, "合格"),
|
|
(DEFECT_OK_B, "合格B类"),
|
|
(DEFECT_NOTOK, "不合格"),
|
|
)
|
|
DEFECT_GRADE_NAMES = dict(DEFECT_GRADE_CHOICES)
|
|
|
|
|
|
def effective_defect_grade(instance, notok_sign_field=None):
|
|
"""Return the inventory grade without coupling it to inventory state."""
|
|
defect = getattr(instance, "defect", None)
|
|
if defect is not None:
|
|
return defect.okcate
|
|
if notok_sign_field and getattr(instance, notok_sign_field, None):
|
|
return DEFECT_NOTOK
|
|
return DEFECT_OK
|
|
|
|
|
|
def effective_defect_grade_q(value, notok_sign_field=None):
|
|
"""Build an index-friendly query matching ``effective_defect_grade``."""
|
|
explicit_grade = Q(defect__okcate=value)
|
|
without_defect = Q(defect__isnull=True)
|
|
|
|
if not notok_sign_field:
|
|
return explicit_grade | without_defect if value == DEFECT_OK else explicit_grade
|
|
|
|
has_legacy_sign = (
|
|
Q(**{f"{notok_sign_field}__isnull": False})
|
|
& ~Q(**{notok_sign_field: ""})
|
|
)
|
|
if value == DEFECT_OK:
|
|
return explicit_grade | (without_defect & ~has_legacy_sign)
|
|
if value == DEFECT_NOTOK:
|
|
return explicit_grade | (without_defect & has_legacy_sign)
|
|
return explicit_grade
|