feat: add demo report import workflow
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import logging
|
||||
|
||||
from apps.core.report_uploads import ReportUploadService
|
||||
from apps.core.upload_contracts import (
|
||||
build_upload_error_response,
|
||||
build_upload_success_payload,
|
||||
@@ -50,13 +51,20 @@ class FormF5UploadView(APIView):
|
||||
report_year = serializer.validated_data["report_year"]
|
||||
|
||||
report_quarter = serializer.validated_data.get("report_quarter")
|
||||
report_upload = ReportUploadService.create(
|
||||
uploaded_file=file,
|
||||
form="f5",
|
||||
user=request.user,
|
||||
)
|
||||
|
||||
if file.size > BACKGROUND_THRESHOLD:
|
||||
ReportUploadService.mark_queued(report_upload.id)
|
||||
task = process_form_f5_file.delay(
|
||||
file.read(),
|
||||
file.name,
|
||||
report_year,
|
||||
report_quarter,
|
||||
str(report_upload.id),
|
||||
)
|
||||
return Response(
|
||||
build_upload_success_payload(
|
||||
@@ -64,6 +72,7 @@ class FormF5UploadView(APIView):
|
||||
report_year=report_year,
|
||||
status="queued",
|
||||
job_id=task.id,
|
||||
upload_id=str(report_upload.id),
|
||||
),
|
||||
status=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
@@ -74,16 +83,19 @@ class FormF5UploadView(APIView):
|
||||
report_year=report_year,
|
||||
report_quarter=report_quarter,
|
||||
)
|
||||
ReportUploadService.finalize(report_upload.id, result)
|
||||
return Response(
|
||||
build_upload_success_payload(
|
||||
form="f5",
|
||||
report_year=report_year,
|
||||
status="done",
|
||||
result=FormF5ParseResultSerializer(result).data,
|
||||
upload_id=str(report_upload.id),
|
||||
),
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
except Exception as e:
|
||||
ReportUploadService.mark_failed(report_upload.id)
|
||||
logger.exception("Ошибка обработки файла Ф-5")
|
||||
return build_upload_error_response(
|
||||
error_code="processing_error",
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import logging
|
||||
from io import BytesIO
|
||||
|
||||
from apps.core.report_uploads import ReportUploadService
|
||||
from apps.core.tasks import TrackedTask
|
||||
from apps.form_5.services import FormF5Parser
|
||||
from celery import shared_task
|
||||
@@ -17,10 +18,20 @@ def process_form_f5_file(
|
||||
file_name: str,
|
||||
report_year: int,
|
||||
report_quarter: int | None = None,
|
||||
report_upload_id: str | None = None,
|
||||
) -> dict:
|
||||
logger.info(f"Начало обработки файла Ф-5: {file_name}")
|
||||
if report_upload_id is not None:
|
||||
ReportUploadService.mark_processing(report_upload_id)
|
||||
parser = FormF5Parser(report_year=report_year, report_quarter=report_quarter)
|
||||
result = parser.parse(BytesIO(file_content))
|
||||
try:
|
||||
result = parser.parse(BytesIO(file_content))
|
||||
if report_upload_id is not None:
|
||||
ReportUploadService.finalize(report_upload_id, result)
|
||||
except Exception:
|
||||
if report_upload_id is not None:
|
||||
ReportUploadService.mark_failed(report_upload_id)
|
||||
raise
|
||||
logger.info(
|
||||
f"Обработка Ф-5 завершена: {result.loaded_count} загружено, {result.skipped_count} пропущено"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user