Some checks failed
CI/CD Pipeline / Run Tests (push) Failing after 2m53s
CI/CD Pipeline / Code Quality Checks (push) Successful in 3m12s
CI/CD Pipeline / Build Docker Images (push) Has been skipped
CI/CD Pipeline / Push to Gitea Registry (push) Has been skipped
CI/CD Pipeline / Deploy to Server (push) Has been skipped
239 lines
7.7 KiB
Python
239 lines
7.7 KiB
Python
"""
|
|
Сериализаторы для организаций.
|
|
|
|
Содержит:
|
|
- legacy nested serializers for report forms;
|
|
- frontend-facing serializers for organization catalog endpoints.
|
|
"""
|
|
|
|
from apps.organization.availability import has_financial_reports, has_tax_reports
|
|
from apps.organization.models import Organization
|
|
from apps.organization.scope_utils import (
|
|
SCOPE_LABELS,
|
|
primary_registry_category,
|
|
registry_category_label,
|
|
)
|
|
from apps.registers.models import Register
|
|
from rest_framework import serializers
|
|
|
|
|
|
class OrganizationRegisterSerializer(serializers.ModelSerializer):
|
|
"""Краткий сериализатор активного реестра организации."""
|
|
|
|
class Meta:
|
|
model = Register
|
|
fields = ["id", "name"]
|
|
read_only_fields = fields
|
|
|
|
|
|
class OrganizationSerializer(serializers.ModelSerializer):
|
|
"""Полный nested-сериализатор организации для внутренних API."""
|
|
|
|
active_registry_names = serializers.SerializerMethodField()
|
|
active_registries = serializers.SerializerMethodField()
|
|
|
|
@staticmethod
|
|
def get_active_registry_names(obj: Organization) -> list[str]:
|
|
return obj.get_active_registry_names()
|
|
|
|
@staticmethod
|
|
def get_active_registries(obj: Organization) -> list[dict[str, str]]:
|
|
return OrganizationRegisterSerializer(
|
|
obj.get_active_registries(), many=True
|
|
).data
|
|
|
|
class Meta:
|
|
model = Organization
|
|
fields = [
|
|
"id",
|
|
"name",
|
|
"inn",
|
|
"ogrn",
|
|
"kpp",
|
|
"okpo",
|
|
"active_registry_names",
|
|
"active_registries",
|
|
"created_at",
|
|
"updated_at",
|
|
]
|
|
read_only_fields = ["id", "created_at", "updated_at"]
|
|
|
|
|
|
class OrganizationListSerializer(serializers.ModelSerializer):
|
|
"""Краткий nested-сериализатор для списков."""
|
|
|
|
active_registry_names = serializers.SerializerMethodField()
|
|
|
|
@staticmethod
|
|
def get_active_registry_names(obj: Organization) -> list[str]:
|
|
return obj.get_active_registry_names()
|
|
|
|
class Meta:
|
|
model = Organization
|
|
fields = [
|
|
"id",
|
|
"name",
|
|
"inn",
|
|
"ogrn",
|
|
"active_registry_names",
|
|
]
|
|
|
|
|
|
class GeneralDirectorSerializer(serializers.Serializer):
|
|
"""Сериализатор блока генерального директора."""
|
|
|
|
full_name = serializers.CharField()
|
|
inn = serializers.CharField()
|
|
appointment_date = serializers.DateField(allow_null=True)
|
|
|
|
|
|
class OrganizationCatalogSummarySerializer(serializers.Serializer):
|
|
"""Сериализатор summary блока организации."""
|
|
|
|
financial_reports_available = serializers.BooleanField()
|
|
tax_reports_available = serializers.BooleanField()
|
|
active_registry_names = serializers.ListField(child=serializers.CharField())
|
|
|
|
|
|
class OrganizationCatalogBaseSerializer(serializers.ModelSerializer):
|
|
"""Базовый сериализатор frontend-контракта организации."""
|
|
|
|
short_name = serializers.SerializerMethodField()
|
|
full_name = serializers.CharField(source="name", read_only=True)
|
|
corporation_scope = serializers.SerializerMethodField()
|
|
corporation_scope_label = serializers.SerializerMethodField()
|
|
registry_category = serializers.SerializerMethodField()
|
|
registry_category_label = serializers.SerializerMethodField()
|
|
organization_type_label = serializers.CharField(read_only=True)
|
|
active_registry_names = serializers.SerializerMethodField()
|
|
|
|
@staticmethod
|
|
def get_short_name(obj: Organization) -> str:
|
|
return obj.display_short_name
|
|
|
|
@staticmethod
|
|
def get_active_registry_names(obj: Organization) -> list[str]:
|
|
return obj.get_active_registry_names()
|
|
|
|
@staticmethod
|
|
def get_registry_category(obj: Organization) -> str:
|
|
return primary_registry_category(obj.get_active_registry_names())
|
|
|
|
@staticmethod
|
|
def get_registry_category_label(obj: Organization) -> str:
|
|
return registry_category_label(
|
|
OrganizationCatalogBaseSerializer.get_registry_category(obj)
|
|
)
|
|
|
|
@staticmethod
|
|
def _primary_scope_or_default(obj: Organization) -> str:
|
|
scopes = obj.get_corporation_scopes()
|
|
return scopes[0] if scopes else ""
|
|
|
|
@staticmethod
|
|
def get_corporation_scope(obj: Organization) -> str:
|
|
return OrganizationCatalogBaseSerializer._primary_scope_or_default(obj)
|
|
|
|
@staticmethod
|
|
def get_corporation_scope_label(obj: Organization) -> str:
|
|
scope = OrganizationCatalogBaseSerializer._primary_scope_or_default(obj)
|
|
return OrganizationCatalogBaseSerializer._label_or_empty(scope)
|
|
|
|
@staticmethod
|
|
def _label_or_empty(scope_code: str) -> str:
|
|
if not scope_code:
|
|
return ""
|
|
return SCOPE_LABELS.get(scope_code, "")
|
|
|
|
|
|
class OrganizationCatalogListSerializer(OrganizationCatalogBaseSerializer):
|
|
"""Сериализатор списка организаций для `/api/v1/organizations/`."""
|
|
|
|
class Meta:
|
|
model = Organization
|
|
fields = [
|
|
"id",
|
|
"short_name",
|
|
"full_name",
|
|
"corporation_scope",
|
|
"corporation_scope_label",
|
|
"registry_category",
|
|
"registry_category_label",
|
|
"organization_type",
|
|
"organization_type_label",
|
|
"inn",
|
|
"ogrn",
|
|
"kpp",
|
|
"okpo",
|
|
"active_registry_names",
|
|
]
|
|
|
|
|
|
class OrganizationCatalogDetailSerializer(OrganizationCatalogBaseSerializer):
|
|
"""Сериализатор детальной карточки организации."""
|
|
|
|
active_registries = serializers.SerializerMethodField()
|
|
general_director = serializers.SerializerMethodField()
|
|
summary = serializers.SerializerMethodField()
|
|
|
|
@staticmethod
|
|
def get_active_registries(obj: Organization) -> list[dict[str, str]]:
|
|
return OrganizationRegisterSerializer(
|
|
obj.get_active_registries(), many=True
|
|
).data
|
|
|
|
@staticmethod
|
|
def get_general_director(obj: Organization) -> dict[str, str | None]:
|
|
return {
|
|
"full_name": obj.general_director_name,
|
|
"inn": obj.general_director_inn,
|
|
"appointment_date": obj.general_director_appointment_date,
|
|
}
|
|
|
|
@staticmethod
|
|
def get_summary(obj: Organization) -> dict[str, object]:
|
|
return {
|
|
"financial_reports_available": has_financial_reports(obj),
|
|
"tax_reports_available": has_tax_reports(obj),
|
|
"active_registry_names": obj.get_active_registry_names(),
|
|
}
|
|
|
|
class Meta:
|
|
model = Organization
|
|
fields = [
|
|
"id",
|
|
"short_name",
|
|
"full_name",
|
|
"corporation_scope",
|
|
"corporation_scope_label",
|
|
"registry_category",
|
|
"registry_category_label",
|
|
"organization_type",
|
|
"organization_type_label",
|
|
"inn",
|
|
"ogrn",
|
|
"kpp",
|
|
"okpo",
|
|
"registration_date",
|
|
"legal_address",
|
|
"activity_type",
|
|
"founder_name",
|
|
"ownership_type",
|
|
"legal_form",
|
|
"charter_capital_amount",
|
|
"general_director",
|
|
"summary",
|
|
"active_registries",
|
|
"created_at",
|
|
"updated_at",
|
|
]
|
|
|
|
|
|
class CorporationScopeDictionarySerializer(serializers.Serializer):
|
|
"""Serializer for corporation scope dictionary entries."""
|
|
|
|
code = serializers.CharField()
|
|
name = serializers.CharField()
|
|
short_name = serializers.CharField()
|
|
sort_order = serializers.IntegerField()
|