feat(forms): unify F2-F6 upload contracts and add shared serializers
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
|
||||
import logging
|
||||
|
||||
from apps.core.upload_contracts import (
|
||||
build_upload_error_response,
|
||||
build_upload_success_payload,
|
||||
build_upload_validation_response,
|
||||
)
|
||||
from apps.core.viewsets import ReadOnlyViewSet
|
||||
from apps.form_4.models import FormF4Record
|
||||
from apps.form_4.serializers import (
|
||||
@@ -27,10 +32,13 @@ class FormF4UploadView(APIView):
|
||||
|
||||
def post(self, request):
|
||||
serializer = FormF4UploadSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
if not serializer.is_valid():
|
||||
return build_upload_validation_response(serializer.errors)
|
||||
|
||||
file = serializer.validated_data["file"]
|
||||
report_year = serializer.validated_data["report_year"]
|
||||
report_quarter = serializer.validated_data.get("report_quarter")
|
||||
report_half_year = serializer.validated_data.get("report_half_year")
|
||||
report_quarter = report_half_year
|
||||
|
||||
if file.size > BACKGROUND_THRESHOLD:
|
||||
task = process_form_f4_file.delay(
|
||||
@@ -40,11 +48,13 @@ class FormF4UploadView(APIView):
|
||||
report_quarter,
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"success": True,
|
||||
"message": "Файл поставлен в очередь",
|
||||
"task_id": task.id,
|
||||
},
|
||||
build_upload_success_payload(
|
||||
form="f4",
|
||||
report_year=report_year,
|
||||
report_half_year=report_half_year,
|
||||
status="queued",
|
||||
job_id=task.id,
|
||||
),
|
||||
status=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
|
||||
@@ -55,12 +65,21 @@ class FormF4UploadView(APIView):
|
||||
report_quarter=report_quarter,
|
||||
)
|
||||
return Response(
|
||||
{"success": True, "data": FormF4ParseResultSerializer(result).data}
|
||||
build_upload_success_payload(
|
||||
form="f4",
|
||||
report_year=report_year,
|
||||
report_half_year=report_half_year,
|
||||
status="done",
|
||||
result=FormF4ParseResultSerializer(result).data,
|
||||
),
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("Ошибка обработки файла Ф-4")
|
||||
return Response(
|
||||
{"success": False, "error": str(e)}, status=status.HTTP_400_BAD_REQUEST
|
||||
return build_upload_error_response(
|
||||
error_code="processing_error",
|
||||
error_message=str(e),
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user