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_5.models import FormF5Record
|
||||
from apps.form_5.serializers import (
|
||||
@@ -27,9 +32,12 @@ class FormF5UploadView(APIView):
|
||||
|
||||
def post(self, request):
|
||||
serializer = FormF5UploadSerializer(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")
|
||||
|
||||
if file.size > BACKGROUND_THRESHOLD:
|
||||
@@ -40,11 +48,12 @@ class FormF5UploadView(APIView):
|
||||
report_quarter,
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"success": True,
|
||||
"message": "Файл поставлен в очередь",
|
||||
"task_id": task.id,
|
||||
},
|
||||
build_upload_success_payload(
|
||||
form="f5",
|
||||
report_year=report_year,
|
||||
status="queued",
|
||||
job_id=task.id,
|
||||
),
|
||||
status=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
|
||||
@@ -55,12 +64,20 @@ class FormF5UploadView(APIView):
|
||||
report_quarter=report_quarter,
|
||||
)
|
||||
return Response(
|
||||
{"success": True, "data": FormF5ParseResultSerializer(result).data}
|
||||
build_upload_success_payload(
|
||||
form="f5",
|
||||
report_year=report_year,
|
||||
status="done",
|
||||
result=FormF5ParseResultSerializer(result).data,
|
||||
),
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("Ошибка обработки файла Ф-5")
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
"""Сериализаторы формы Ф-5."""
|
||||
|
||||
from apps.form_5.models import FormF5Record
|
||||
from apps.core.upload_contracts import (
|
||||
UploadAnnualSerializer,
|
||||
UploadParseResultSerializer,
|
||||
)
|
||||
from apps.organization.serializers import OrganizationSerializer
|
||||
from rest_framework import serializers
|
||||
|
||||
@@ -41,40 +45,9 @@ class FormF5RecordListSerializer(serializers.ModelSerializer):
|
||||
]
|
||||
|
||||
|
||||
class FormF5UploadSerializer(serializers.Serializer):
|
||||
file = serializers.FileField(help_text="Excel файл формы Ф-5 (.xlsx)")
|
||||
report_year = serializers.IntegerField(min_value=2000, help_text="Отчетный год")
|
||||
report_quarter = serializers.IntegerField(
|
||||
min_value=1,
|
||||
max_value=4,
|
||||
required=False,
|
||||
allow_null=True,
|
||||
help_text="Отчетный квартал от 1 до 4. Пусто для годовой формы.",
|
||||
)
|
||||
|
||||
def validate_file(self, value):
|
||||
if not value.name.endswith((".xlsx", ".xls")):
|
||||
raise serializers.ValidationError("Неподдерживаемый формат файла")
|
||||
if value.size > 50 * 1024 * 1024:
|
||||
raise serializers.ValidationError("Размер файла превышает 50MB")
|
||||
return value
|
||||
class FormF5UploadSerializer(UploadAnnualSerializer):
|
||||
"""Загрузка файла формы Ф-5 (годовая отчетность)."""
|
||||
|
||||
|
||||
class FieldErrorSerializer(serializers.Serializer):
|
||||
field = serializers.CharField()
|
||||
message = serializers.CharField()
|
||||
|
||||
|
||||
class RowValidationErrorSerializer(serializers.Serializer):
|
||||
row = serializers.IntegerField()
|
||||
inn = serializers.CharField(allow_null=True)
|
||||
kpp = serializers.CharField(allow_null=True)
|
||||
organization_name = serializers.CharField(allow_null=True)
|
||||
errors = FieldErrorSerializer(many=True)
|
||||
|
||||
|
||||
class FormF5ParseResultSerializer(serializers.Serializer):
|
||||
batch_id = serializers.IntegerField()
|
||||
loaded_count = serializers.IntegerField()
|
||||
skipped_count = serializers.IntegerField()
|
||||
errors = RowValidationErrorSerializer(many=True)
|
||||
class FormF5ParseResultSerializer(UploadParseResultSerializer):
|
||||
"""Сериализатор результата парсинга."""
|
||||
|
||||
Reference in New Issue
Block a user