Implement exchange imports and frontend reporting APIs
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 3m50s
CI/CD Pipeline / Run Tests (push) Successful in 3m57s
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
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 3m50s
CI/CD Pipeline / Run Tests (push) Successful in 3m57s
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
This commit is contained in:
59
src/apps/organization/scope_utils.py
Normal file
59
src/apps/organization/scope_utils.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""Helpers for corporation scope derivation from active registries."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
|
||||
from django.db.models import Q, QuerySet
|
||||
|
||||
SCOPE_KEYWORDS: dict[str, tuple[str, ...]] = {
|
||||
"rosatom": ("Росатом",),
|
||||
"roscosmos": ("Роскосмос",),
|
||||
"opk": ("ОПК",),
|
||||
}
|
||||
|
||||
SCOPE_LABELS: dict[str, str] = {
|
||||
"rosatom": "Госкорпорация «Росатом»",
|
||||
"roscosmos": "Госкорпорация «Роскосмос»",
|
||||
"opk": "Организации ОПК",
|
||||
}
|
||||
|
||||
|
||||
def scopes_from_registry_names(registry_names: Iterable[str]) -> list[str]:
|
||||
normalized_names = [registry_name.casefold() for registry_name in registry_names]
|
||||
scopes: list[str] = []
|
||||
|
||||
for scope, keywords in SCOPE_KEYWORDS.items():
|
||||
if any(
|
||||
keyword.casefold() in registry_name
|
||||
for registry_name in normalized_names
|
||||
for keyword in keywords
|
||||
):
|
||||
scopes.append(scope)
|
||||
|
||||
return scopes
|
||||
|
||||
|
||||
def scope_labels(scope_codes: Iterable[str]) -> list[str]:
|
||||
return [SCOPE_LABELS[code] for code in scope_codes if code in SCOPE_LABELS]
|
||||
|
||||
|
||||
def build_scope_query(scope_codes: Iterable[str]) -> Q:
|
||||
query = Q()
|
||||
for scope_code in scope_codes:
|
||||
keywords = SCOPE_KEYWORDS.get(scope_code, ())
|
||||
for keyword in keywords:
|
||||
query |= Q(
|
||||
membership_periods__registry__name__contains=keyword,
|
||||
membership_periods__ended_at__isnull=True,
|
||||
)
|
||||
return query
|
||||
|
||||
|
||||
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]
|
||||
if not scope_codes:
|
||||
return queryset.none()
|
||||
return queryset.filter(build_scope_query(scope_codes)).distinct()
|
||||
Reference in New Issue
Block a user