fix: hide organizations absent from the current full directory
Some checks failed
Mostovik Backend CI/CD / Tests and lint (push) Failing after 3m16s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
Some checks failed
Mostovik Backend CI/CD / Tests and lint (push) Failing after 3m16s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
This commit is contained in:
18
docs/current-organization-directory.md
Normal file
18
docs/current-organization-directory.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Текущий полный перечень организаций
|
||||||
|
|
||||||
|
Импорт справочника XLSX через import-directory и import_organization_directory
|
||||||
|
считает каждый успешно обработанный файл полным снимком перечня.
|
||||||
|
Отсутствующим организациям выставляется directory_is_current=False;
|
||||||
|
повторно включённым — True. Исходный opk_registry_membership и остальные
|
||||||
|
поля отсутствующих организаций сохраняются вместе со связанными записями источников.
|
||||||
|
API списка/карточки организаций, расширений и записей источников исключает
|
||||||
|
выбывших. Недельные адресные lookup-задачи также исключают их.
|
||||||
|
|
||||||
|
Весь импорт и смена текущего состава выполняются в одной транзакции.
|
||||||
|
Некорректный или пустой файл не изменяет текущий состав; пустой снимок отклоняется.
|
||||||
|
Частичные перечни нельзя загружать этим маршрутом.
|
||||||
|
|
||||||
|
Миграция 0022 сохраняет текущую видимость существующих организаций.
|
||||||
|
Исключение выбывших произойдёт при следующем успешном импорте полного перечня.
|
||||||
|
Поле отражает текущую видимость; отдельный журнал всех прежних снимков этим
|
||||||
|
изменением не создаётся. Существующие исторические записи не удаляются.
|
||||||
@@ -200,7 +200,8 @@ def _active_registry_lookup_targets(
|
|||||||
) -> list[RegistryLookupTarget]:
|
) -> list[RegistryLookupTarget]:
|
||||||
"""Вернуть организации, которые сейчас состоят хотя бы в одном реестре."""
|
"""Вернуть организации, которые сейчас состоят хотя бы в одном реестре."""
|
||||||
queryset = SourceOrganization.objects.filter(
|
queryset = SourceOrganization.objects.filter(
|
||||||
Q(opk_registry_membership=True) | _rosatom_roscosmos_scope_query()
|
Q(opk_registry_membership=True) | _rosatom_roscosmos_scope_query(),
|
||||||
|
directory_is_current=True,
|
||||||
)
|
)
|
||||||
if organization_ids is not None:
|
if organization_ids is not None:
|
||||||
queryset = queryset.filter(uid__in=organization_ids)
|
queryset = queryset.filter(uid__in=organization_ids)
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ class OrganizationDirectoryImportService:
|
|||||||
file_hash = cls._sha256(file_path)
|
file_hash = cls._sha256(file_path)
|
||||||
imported_at = timezone.now()
|
imported_at = timezone.now()
|
||||||
|
|
||||||
|
snapshot_ids = set()
|
||||||
scanned = 0
|
scanned = 0
|
||||||
created = 0
|
created = 0
|
||||||
updated = 0
|
updated = 0
|
||||||
@@ -134,15 +135,23 @@ class OrganizationDirectoryImportService:
|
|||||||
imported_at=imported_at,
|
imported_at=imported_at,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
defaults["directory_is_current"] = True
|
||||||
_organization, was_created = Organization.objects.update_or_create(
|
_organization, was_created = Organization.objects.update_or_create(
|
||||||
rn=rn,
|
rn=rn,
|
||||||
defaults=defaults,
|
defaults=defaults,
|
||||||
)
|
)
|
||||||
|
snapshot_ids.add(_organization.pk)
|
||||||
if was_created:
|
if was_created:
|
||||||
created += 1
|
created += 1
|
||||||
else:
|
else:
|
||||||
updated += 1
|
updated += 1
|
||||||
|
|
||||||
|
if not snapshot_ids:
|
||||||
|
raise OrganizationDirectoryImportError("Full directory must not be empty")
|
||||||
|
Organization.objects.filter(directory_is_current=True).exclude(
|
||||||
|
pk__in=snapshot_ids
|
||||||
|
).update(directory_is_current=False)
|
||||||
|
|
||||||
result = OrganizationDirectoryImportResult(
|
result = OrganizationDirectoryImportResult(
|
||||||
scanned=scanned,
|
scanned=scanned,
|
||||||
created=created,
|
created=created,
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ class OrganizationFilter(filters.FilterSet):
|
|||||||
registry_id: str | None = None,
|
registry_id: str | None = None,
|
||||||
registry_name: str | None = None,
|
registry_name: str | None = None,
|
||||||
) -> Q:
|
) -> Q:
|
||||||
query = Q(opk_registry_membership=True)
|
query = Q(opk_registry_membership=True, directory_is_current=True)
|
||||||
if registry_id:
|
if registry_id:
|
||||||
query &= Q(ropk_razdel_num=str(registry_id)) | Q(gk_code=str(registry_id))
|
query &= Q(ropk_razdel_num=str(registry_id)) | Q(gk_code=str(registry_id))
|
||||||
if registry_name:
|
if registry_name:
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [("organizations", "0021_enable_yearly_partitioned_exports")]
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="organization", name="directory_is_current",
|
||||||
|
field=models.BooleanField(
|
||||||
|
"присутствует в текущем полном перечне", default=True, db_index=True,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -304,6 +304,11 @@ class Organization(models.Model):
|
|||||||
blank=True,
|
blank=True,
|
||||||
db_index=True,
|
db_index=True,
|
||||||
)
|
)
|
||||||
|
directory_is_current = models.BooleanField(
|
||||||
|
_("присутствует в текущем полном перечне"),
|
||||||
|
default=True,
|
||||||
|
db_index=True,
|
||||||
|
)
|
||||||
opk_registry_membership = models.BooleanField(
|
opk_registry_membership = models.BooleanField(
|
||||||
_("членство в реестре ОПК"),
|
_("членство в реестре ОПК"),
|
||||||
null=True,
|
null=True,
|
||||||
|
|||||||
@@ -385,7 +385,7 @@ class OrganizationViewSet(CachedReadOnlyMixin, ReadOnlyModelViewSet):
|
|||||||
return (
|
return (
|
||||||
super()
|
super()
|
||||||
.get_queryset()
|
.get_queryset()
|
||||||
.filter(opk_registry_membership=True)
|
.filter(opk_registry_membership=True, directory_is_current=True)
|
||||||
.prefetch_related("source_extensions")
|
.prefetch_related("source_extensions")
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -525,7 +525,10 @@ class OrganizationSourceExtensionViewSet(ReadOnlyModelViewSet):
|
|||||||
|
|
||||||
queryset = (
|
queryset = (
|
||||||
OrganizationSourceExtension.objects.select_related("organization")
|
OrganizationSourceExtension.objects.select_related("organization")
|
||||||
.filter(organization__opk_registry_membership=True)
|
.filter(
|
||||||
|
organization__opk_registry_membership=True,
|
||||||
|
organization__directory_is_current=True,
|
||||||
|
)
|
||||||
.order_by(
|
.order_by(
|
||||||
"organization__name",
|
"organization__name",
|
||||||
"source_group",
|
"source_group",
|
||||||
@@ -582,6 +585,7 @@ class OrganizationSourceRecordViewSet(ReadOnlyModelViewSet):
|
|||||||
Q(extension__organization__opk_registry_membership=True)
|
Q(extension__organization__opk_registry_membership=True)
|
||||||
| Q(extension__source_group__in=REGISTRY_GROUPS)
|
| Q(extension__source_group__in=REGISTRY_GROUPS)
|
||||||
)
|
)
|
||||||
|
.filter(extension__organization__directory_is_current=True)
|
||||||
.prefetch_related("financial_lines")
|
.prefetch_related("financial_lines")
|
||||||
.order_by("-created_at", "-uid")
|
.order_by("-created_at", "-uid")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -169,6 +169,90 @@ class OrganizationDirectoryImportServiceTest(TestCase):
|
|||||||
self.assertEqual(second.inn, "7701001021")
|
self.assertEqual(second.inn, "7701001021")
|
||||||
self.assertEqual(second.integrated_structure, "")
|
self.assertEqual(second.integrated_structure, "")
|
||||||
|
|
||||||
|
def test_full_snapshot_hides_missing_preserves_history_and_restores_returned(self):
|
||||||
|
from django.urls import reverse
|
||||||
|
from rest_framework.test import APIClient
|
||||||
|
from organizations.models import (
|
||||||
|
GovernmentSupportExtension,
|
||||||
|
OrganizationSourceRecord,
|
||||||
|
)
|
||||||
|
from tests.apps.user.factories import UserFactory
|
||||||
|
|
||||||
|
first = self._workbook_path(
|
||||||
|
[
|
||||||
|
self._row(rn="10", opk_registry_membership=".T."),
|
||||||
|
self._row(
|
||||||
|
rn="20",
|
||||||
|
inn="7701001002",
|
||||||
|
ogrn="1027700100002",
|
||||||
|
opk_registry_membership=".T.",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
OrganizationDirectoryImportService.import_xlsx(first)
|
||||||
|
missing = Organization.objects.get(rn=20)
|
||||||
|
extension = GovernmentSupportExtension.objects.create(organization=missing)
|
||||||
|
record = OrganizationSourceRecord.objects.create(
|
||||||
|
extension=extension,
|
||||||
|
source="fns_sme_support_recipients",
|
||||||
|
record_type="sme_support_measure",
|
||||||
|
external_id="historical",
|
||||||
|
payload={"history": "kept"},
|
||||||
|
)
|
||||||
|
second = self._workbook_path(
|
||||||
|
[self._row(rn="10", opk_registry_membership=".T.")]
|
||||||
|
)
|
||||||
|
OrganizationDirectoryImportService.import_xlsx(second)
|
||||||
|
missing.refresh_from_db()
|
||||||
|
self.assertFalse(missing.directory_is_current)
|
||||||
|
self.assertTrue(missing.opk_registry_membership)
|
||||||
|
self.assertEqual(Organization.objects.count(), 2)
|
||||||
|
record.refresh_from_db()
|
||||||
|
self.assertEqual(record.payload, {"history": "kept"})
|
||||||
|
client = APIClient()
|
||||||
|
client.force_authenticate(UserFactory.create_user())
|
||||||
|
for route, uid in (
|
||||||
|
("organizations", missing.uid),
|
||||||
|
("organization-sources", extension.uid),
|
||||||
|
("organization-source-records", record.uid),
|
||||||
|
):
|
||||||
|
response = client.get(
|
||||||
|
reverse(f"api_v2:organizations:{route}-detail", args=[uid])
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 404, response.data)
|
||||||
|
listing = client.get(reverse(f"api_v2:organizations:{route}-list"))
|
||||||
|
self.assertEqual(listing.status_code, 200, listing.data)
|
||||||
|
self.assertNotIn(str(uid), str(listing.data))
|
||||||
|
response = client.get(reverse("api_v2:organizations:organizations-list"))
|
||||||
|
self.assertNotIn(str(missing.uid), str(response.data))
|
||||||
|
OrganizationDirectoryImportService.import_xlsx(first)
|
||||||
|
missing.refresh_from_db()
|
||||||
|
self.assertTrue(missing.directory_is_current)
|
||||||
|
self.assertEqual(
|
||||||
|
client.get(
|
||||||
|
reverse("api_v2:organizations:organizations-detail", args=[missing.uid])
|
||||||
|
).status_code,
|
||||||
|
200,
|
||||||
|
)
|
||||||
|
self.assertTrue(OrganizationSourceRecord.objects.filter(pk=record.pk).exists())
|
||||||
|
|
||||||
|
def test_invalid_or_empty_snapshot_does_not_remove_or_update_current_rows(self):
|
||||||
|
original = self._workbook_path(
|
||||||
|
[self._row(rn="10", opk_registry_membership=".T.")]
|
||||||
|
)
|
||||||
|
OrganizationDirectoryImportService.import_xlsx(original)
|
||||||
|
for rows in (
|
||||||
|
[],
|
||||||
|
[self._row(rn="10", full_name="Changed"), self._row(rn="bad")],
|
||||||
|
):
|
||||||
|
with self.assertRaises(OrganizationDirectoryImportError):
|
||||||
|
OrganizationDirectoryImportService.import_xlsx(
|
||||||
|
self._workbook_path(rows)
|
||||||
|
)
|
||||||
|
organization = Organization.objects.get(rn=10)
|
||||||
|
self.assertTrue(organization.directory_is_current)
|
||||||
|
self.assertEqual(organization.name, 'ООО "По умолчанию"')
|
||||||
|
|
||||||
def _workbook_path(
|
def _workbook_path(
|
||||||
self,
|
self,
|
||||||
rows: list[dict[str, object]],
|
rows: list[dict[str, object]],
|
||||||
|
|||||||
Reference in New Issue
Block a user