File size: 3,667 Bytes
f8b03c1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | """Django admin configuration."""
from __future__ import annotations
import logging
from typing import Any, ClassVar
from django import forms
from django.utils.translation import gettext_lazy as _
from apps.cases.domain.validators import normalize_stages
from apps.cases.models import Case, CaseParty, CaseStage, SupervisingAuthority
class CaseAdminForm(forms.ModelForm[Case]):
current_stage = forms.ChoiceField(
choices=[("", "---------")] + list(CaseStage.choices), required=False, label=_("当前阶段")
)
class Meta:
model = Case
fields: str = "__all__"
widgets: ClassVar[dict[str, Any]] = {
"cause_of_action": forms.TextInput(
attrs={
"class": "vTextField js-cause-autocomplete",
"placeholder": _("请输入案由关键词..."),
"autocomplete": "off",
}
),
}
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
def clean(self) -> dict[str, Any]:
logger = logging.getLogger(__name__)
cleaned: dict[str, Any] = super().clean() or {}
logger.info("[CaseAdminForm.clean] 开始验证, errors so far: %s", self.errors)
cur = cleaned.get("current_stage")
contract = cleaned.get("contract")
ctype = getattr(contract, "case_type", None) if contract else None
rep = getattr(contract, "representation_stages", []) if contract else []
logger.info("[CaseAdminForm.clean] cur=%s, ctype=%s, rep=%s", cur, ctype, rep)
try:
_, cur2 = normalize_stages(ctype, rep, cur, strict=False)
cleaned["current_stage"] = cur2
except ValueError as e:
code = str(e)
logger.error("[CaseAdminForm.clean] normalize_stages error: %s", code)
if code == "invalid_cur":
self.add_error("current_stage", _("当前阶段不在可选范围内"))
elif code == "cur_not_in_rep":
self.add_error("current_stage", _("当前阶段必须在合同的代理阶段范围内"))
elif code == "stages_not_applicable":
self.add_error("current_stage", _("该案件类型不支持阶段设置"))
elif code.startswith("invalid_rep:"):
invalid_stages = code.split(":", 1)[1]
self.add_error("current_stage", _("代理阶段包含无效值: %s") % invalid_stages)
else:
logger.error("未处理的案件验证错误: %s", code)
self.add_error(None, _("案件数据验证失败: %s") % code)
logger.info("[CaseAdminForm.clean] 验证完成, final errors: %s", self.errors)
return cleaned
class CasePartyInlineForm(forms.ModelForm[CaseParty]):
class Meta:
model = CaseParty
fields: str = "__all__"
widgets: ClassVar[dict[str, Any]] = {
"client": forms.Select(
attrs={
"class": "contract-party-client-select",
"data-contract-party-filter": "true",
}
),
}
class SupervisingAuthorityInlineForm(forms.ModelForm[SupervisingAuthority]):
class Meta:
model = SupervisingAuthority
fields: str = "__all__"
widgets: ClassVar[dict[str, Any]] = {
"name": forms.TextInput(
attrs={
"class": "vTextField js-court-autocomplete",
"placeholder": _("请输入法院名称..."),
"autocomplete": "off",
}
)
}
|