File size: 1,136 Bytes
b4f2238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Module for validators."""

from __future__ import annotations

from collections.abc import Iterable

from django.utils.translation import gettext_lazy as _

from apps.core.exceptions import ValidationException
from apps.core.models.enums import CaseStage, CaseType

APPLICABLE_TYPES = {CaseType.CIVIL, CaseType.CRIMINAL, CaseType.ADMINISTRATIVE, CaseType.LABOR, CaseType.INTL}


def normalize_representation_stages(
    case_type: str | None,
    representation_stages: Iterable[str] | None,
    strict: bool = False,
) -> list[str]:
    if not case_type or case_type not in APPLICABLE_TYPES:
        rep = list(representation_stages or [])
        if strict and rep:
            raise ValidationException(_("代理阶段不适用于此合同类型"), code="STAGES_NOT_APPLICABLE")
        return []

    rep = list(representation_stages or [])
    allowed = {c[0] for c in CaseStage.choices}
    invalid = set(rep) - allowed
    if invalid:
        raise ValidationException(
            _("无效的代理阶段"),
            code="INVALID_STAGES",
            errors={"invalid_stages": sorted(invalid)},
        )
    return rep