182 lines
6.6 KiB
Python
182 lines
6.6 KiB
Python
"""
|
|
API ViewSets для организаций.
|
|
|
|
Содержит:
|
|
- OrganizationViewSet - frontend-facing read API for organizations.
|
|
"""
|
|
|
|
from apps.core.viewsets import ClassicReadOnlyViewSet
|
|
from apps.organization.contract_serializers import (
|
|
OrganizationCatalogListResponseSerializer,
|
|
)
|
|
from apps.organization.models import Organization
|
|
from apps.organization.scope_utils import (
|
|
filter_queryset_by_registry_categories,
|
|
filter_queryset_by_scopes,
|
|
)
|
|
from apps.organization.serializers import (
|
|
OrganizationCatalogDetailSerializer,
|
|
OrganizationCatalogListSerializer,
|
|
)
|
|
from django_filters import rest_framework as filters
|
|
from drf_yasg import openapi
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
|
class OrganizationFilter(filters.FilterSet):
|
|
"""Фильтры для организаций."""
|
|
|
|
name = filters.CharFilter(lookup_expr="icontains")
|
|
inn = filters.CharFilter(lookup_expr="exact")
|
|
ogrn = filters.CharFilter(lookup_expr="exact")
|
|
corporation_scope = filters.CharFilter(method="filter_corporation_scope")
|
|
registry_category = filters.CharFilter(method="filter_registry_category")
|
|
registryCategory = filters.CharFilter(method="filter_registry_category")
|
|
organization_type = filters.CharFilter(lookup_expr="exact")
|
|
|
|
@staticmethod
|
|
def filter_corporation_scope(queryset, _name, value):
|
|
requested_scopes = [
|
|
item.strip() for item in str(value).split(",") if item.strip()
|
|
]
|
|
if not requested_scopes:
|
|
return queryset
|
|
return filter_queryset_by_scopes(queryset, requested_scopes)
|
|
|
|
@staticmethod
|
|
def filter_registry_category(queryset, _name, value):
|
|
requested_categories = [
|
|
item.strip().lower() for item in str(value).split(",") if item.strip()
|
|
]
|
|
if not requested_categories:
|
|
return queryset
|
|
return filter_queryset_by_registry_categories(queryset, requested_categories)
|
|
|
|
class Meta:
|
|
model = Organization
|
|
fields = [
|
|
"name",
|
|
"inn",
|
|
"ogrn",
|
|
"corporation_scope",
|
|
"registry_category",
|
|
"organization_type",
|
|
]
|
|
|
|
|
|
class OrganizationViewSet(ClassicReadOnlyViewSet[Organization]):
|
|
"""
|
|
ViewSet для просмотра организаций.
|
|
|
|
Только чтение - организации создаются автоматически при загрузке форм.
|
|
|
|
Эндпоинты:
|
|
GET /organizations/ - список организаций
|
|
GET /organizations/{id}/ - детали организации
|
|
"""
|
|
|
|
queryset = Organization.objects.all()
|
|
serializer_class = OrganizationCatalogDetailSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
filterset_class = OrganizationFilter
|
|
search_fields = ["name", "short_name", "inn", "ogrn", "okpo"]
|
|
ordering_fields = ["name", "short_name", "inn", "created_at"]
|
|
ordering = ["name"]
|
|
|
|
serializer_classes = {
|
|
"list": OrganizationCatalogListSerializer,
|
|
}
|
|
|
|
@swagger_auto_schema(
|
|
operation_summary="Список организаций",
|
|
operation_description=(
|
|
"Возвращает основной реестр организаций в paginated-форме "
|
|
"`count / next / previous / results`."
|
|
),
|
|
tags=["Организации"],
|
|
manual_parameters=[
|
|
openapi.Parameter(
|
|
"name",
|
|
openapi.IN_QUERY,
|
|
description="Фильтр по наименованию организации",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"inn",
|
|
openapi.IN_QUERY,
|
|
description="Фильтр по ИНН",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"ogrn",
|
|
openapi.IN_QUERY,
|
|
description="Фильтр по ОГРН",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"corporation_scope",
|
|
openapi.IN_QUERY,
|
|
description="Код корпорации (`rosatom`, `roscosmos`, `opk`, `other`)",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"organization_type",
|
|
openapi.IN_QUERY,
|
|
description="Тип организации",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"registry_category",
|
|
openapi.IN_QUERY,
|
|
description=(
|
|
"Категория активного реестра для сайдбара "
|
|
"(`opk`, `goz`, `other`). Поддерживает список через запятую."
|
|
),
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"registryCategory",
|
|
openapi.IN_QUERY,
|
|
description="CamelCase alias для `registry_category`.",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"search",
|
|
openapi.IN_QUERY,
|
|
description="Полнотекстовый поиск по name/short_name/inn/ogrn/okpo",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"ordering",
|
|
openapi.IN_QUERY,
|
|
description="Сортировка по `name`, `short_name`, `inn`, `created_at`",
|
|
type=openapi.TYPE_STRING,
|
|
),
|
|
openapi.Parameter(
|
|
"page",
|
|
openapi.IN_QUERY,
|
|
description="Номер страницы",
|
|
type=openapi.TYPE_INTEGER,
|
|
),
|
|
openapi.Parameter(
|
|
"page_size",
|
|
openapi.IN_QUERY,
|
|
description="Размер страницы",
|
|
type=openapi.TYPE_INTEGER,
|
|
),
|
|
],
|
|
responses={200: OrganizationCatalogListResponseSerializer},
|
|
)
|
|
def list(self, request, *args, **kwargs):
|
|
return super().list(request, *args, **kwargs)
|
|
|
|
@swagger_auto_schema(
|
|
operation_summary="Карточка организации",
|
|
operation_description="Возвращает детальную карточку организации.",
|
|
tags=["Организации"],
|
|
responses={200: OrganizationCatalogDetailSerializer},
|
|
)
|
|
def retrieve(self, request, *args, **kwargs):
|
|
return super().retrieve(request, *args, **kwargs)
|