feat: add demo report import workflow
This commit is contained in:
1
src/apps/organization/management/__init__.py
Normal file
1
src/apps/organization/management/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Organization management commands."""
|
||||
1
src/apps/organization/management/commands/__init__.py
Normal file
1
src/apps/organization/management/commands/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Organization command package."""
|
||||
@@ -0,0 +1,104 @@
|
||||
"""Delete the fixed Mostovik demonstration companies and related data."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from apps.core.management.commands.base import BaseAppCommand
|
||||
from apps.core.models import ReportUpload
|
||||
from apps.organization.models import Organization
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
|
||||
TEST_COMPANY_COUNT = 20
|
||||
|
||||
|
||||
def _legal_entity_inn(index: int) -> str:
|
||||
base = f"770900{index:03d}"
|
||||
weights = (2, 4, 10, 3, 5, 9, 4, 6, 8)
|
||||
checksum = sum(
|
||||
int(digit) * weight for digit, weight in zip(base, weights, strict=True)
|
||||
)
|
||||
return f"{base}{checksum % 11 % 10}"
|
||||
|
||||
|
||||
TEST_COMPANY_IDENTITIES = tuple(
|
||||
(f"Тестовая компания {index}", _legal_entity_inn(index))
|
||||
for index in range(1, TEST_COMPANY_COUNT + 1)
|
||||
)
|
||||
DEMO_REPORT_FILE_NAMES = (
|
||||
"Ф-1_2025-09.xlsx",
|
||||
"Ф-1_2025-12.xlsx",
|
||||
"Ф-1_2026-03.xlsx",
|
||||
"Ф-1_2026-06.xlsx",
|
||||
"Ф-2_2025-Q3.xlsx",
|
||||
"Ф-2_2025-Q4.xlsx",
|
||||
"Ф-2_2026-Q1.xlsx",
|
||||
"Ф-2_2026-Q2.xlsx",
|
||||
"Ф-3_2022.xlsx",
|
||||
"Ф-3_2023.xlsx",
|
||||
"Ф-3_2024.xlsx",
|
||||
"Ф-3_2025.xlsx",
|
||||
"Ф-4_2024-H2.xlsx",
|
||||
"Ф-4_2025-H1.xlsx",
|
||||
"Ф-4_2025-H2.xlsx",
|
||||
"Ф-4_2026-H1.xlsx",
|
||||
"Ф-5_2022.xlsx",
|
||||
"Ф-5_2023.xlsx",
|
||||
"Ф-5_2024.xlsx",
|
||||
"Ф-5_2025.xlsx",
|
||||
"Ф-6_2022.xlsx",
|
||||
"Ф-6_2023.xlsx",
|
||||
"Ф-6_2024.xlsx",
|
||||
"Ф-6_2025.xlsx",
|
||||
)
|
||||
|
||||
|
||||
class Command(BaseAppCommand):
|
||||
"""Delete only the deterministic companies imported from Mostovik."""
|
||||
|
||||
help = "Удаляет 20 тестовых компаний Mostovik и все связанные данные"
|
||||
use_transaction = True
|
||||
|
||||
def execute_command(self, *args, **options) -> str:
|
||||
report_uploads = list(
|
||||
ReportUpload.objects.filter(file_name__in=DEMO_REPORT_FILE_NAMES)
|
||||
)
|
||||
stored_files = [
|
||||
(upload.original_file.storage, upload.original_file.name)
|
||||
for upload in report_uploads
|
||||
if upload.original_file and upload.original_file.name
|
||||
]
|
||||
report_uploads_deleted = len(report_uploads)
|
||||
ReportUpload.objects.filter(
|
||||
pk__in=[item.pk for item in report_uploads]
|
||||
).delete()
|
||||
for storage, file_name in stored_files:
|
||||
transaction.on_commit(
|
||||
lambda storage=storage, file_name=file_name: storage.delete(file_name)
|
||||
)
|
||||
|
||||
identity_query = Q()
|
||||
for name, inn in TEST_COMPANY_IDENTITIES:
|
||||
identity_query |= Q(name=name, inn=inn)
|
||||
|
||||
queryset = Organization.objects.filter(identity_query)
|
||||
organizations_deleted = queryset.count()
|
||||
total_deleted, deleted_by_model = queryset.delete()
|
||||
organization_label = Organization._meta.label
|
||||
related_objects_deleted = total_deleted - deleted_by_model.get(
|
||||
organization_label, 0
|
||||
)
|
||||
|
||||
rendered = json.dumps(
|
||||
{
|
||||
"dry_run": self.dry_run,
|
||||
"organizations_deleted": organizations_deleted,
|
||||
"report_uploads_deleted": report_uploads_deleted,
|
||||
"related_objects_deleted": related_objects_deleted,
|
||||
},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
)
|
||||
self.log_success(rendered)
|
||||
return rendered
|
||||
Reference in New Issue
Block a user