fix: accept roskosmos corporation scope alias
All checks were successful
CI/CD Pipeline / Code Quality Checks (push) Successful in 2m22s
CI/CD Pipeline / Run Tests (push) Successful in 2m28s
CI/CD Pipeline / Build Docker Images (push) Successful in 2m24s
CI/CD Pipeline / Push to Gitea Registry (push) Successful in 1s
CI/CD Pipeline / Deploy to Server (push) Successful in 1s

This commit is contained in:
2026-05-28 12:46:10 +02:00
parent e09002d3af
commit cd74427741
4 changed files with 37 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
"""Query serializers for organization analytics endpoints."""
from apps.organization.models import CorporationScope
from apps.organization.scope_utils import SCOPE_ALIASES
from rest_framework import serializers
@@ -17,8 +18,8 @@ class AliasChoiceField(serializers.ChoiceField):
class DashboardQuerySerializer(serializers.Serializer):
corporation_scope = serializers.ChoiceField(
choices=CorporationScope.choices, required=False
corporation_scope = AliasChoiceField(
choices=CorporationScope.choices, aliases=SCOPE_ALIASES, required=False
)

View File

@@ -12,6 +12,10 @@ SCOPE_KEYWORDS: dict[str, tuple[str, ...]] = {
"opk": ("ОПК",),
}
SCOPE_ALIASES: dict[str, str] = {
"roskosmos": "roscosmos",
}
SCOPE_LABELS: dict[str, str] = {
"rosatom": "Госкорпорация «Росатом»",
"roscosmos": "Госкорпорация «Роскосмос»",
@@ -64,6 +68,11 @@ def scope_labels(scope_codes: Iterable[str]) -> list[str]:
return [SCOPE_LABELS[code] for code in scope_codes if code in SCOPE_LABELS]
def normalize_scope_code(scope_code: str) -> str:
normalized = str(scope_code).strip().lower()
return SCOPE_ALIASES.get(normalized, normalized)
def get_corporation_scope_dictionary() -> list[dict[str, str | int]]:
"""Возвращает справочник корпусов для API-словаря."""
items: list[dict[str, str | int]] = []
@@ -121,7 +130,11 @@ def build_scope_query(scope_codes: Iterable[str]) -> Q:
def filter_queryset_by_scopes(
queryset: QuerySet, scope_codes: Iterable[str]
) -> QuerySet:
scope_codes = [code for code in scope_codes if code in SCOPE_KEYWORDS]
scope_codes = [
normalized_code
for code in scope_codes
if (normalized_code := normalize_scope_code(code)) in SCOPE_KEYWORDS
]
if not scope_codes:
return queryset.none()
return queryset.filter(build_scope_query(scope_codes)).distinct()