Add initial implementations for forms and organization apps with serializers, factories, and admin configurations
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 5m5s
CI/CD Pipeline / Run Tests (push) Failing after 5m5s
CI/CD Pipeline / Build Docker Images (push) Has been skipped
CI/CD Pipeline / Push to Gitea Registry (push) Has been skipped
CI/CD Pipeline / Deploy to Server (push) Has been skipped
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 5m5s
CI/CD Pipeline / Run Tests (push) Failing after 5m5s
CI/CD Pipeline / Build Docker Images (push) Has been skipped
CI/CD Pipeline / Push to Gitea Registry (push) Has been skipped
CI/CD Pipeline / Deploy to Server (push) Has been skipped
This commit is contained in:
58
src/apps/form_4/api.py
Normal file
58
src/apps/form_4/api.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""API формы Ф-4."""
|
||||
|
||||
import logging
|
||||
|
||||
from apps.core.viewsets import BaseViewSet
|
||||
from apps.form_4.models import FormF4Record
|
||||
from apps.form_4.serializers import (
|
||||
FormF4ParseResultSerializer,
|
||||
FormF4RecordListSerializer,
|
||||
FormF4RecordSerializer,
|
||||
FormF4UploadSerializer,
|
||||
)
|
||||
from apps.form_4.services import FormF4Service, parse_form_f4_file
|
||||
from apps.form_4.tasks import process_form_f4_file
|
||||
from rest_framework import status
|
||||
from rest_framework.parsers import MultiPartParser
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
BACKGROUND_THRESHOLD = 1024 * 1024
|
||||
|
||||
|
||||
class FormF4UploadView(APIView):
|
||||
parser_classes = [MultiPartParser]
|
||||
|
||||
def post(self, request):
|
||||
serializer = FormF4UploadSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
file = serializer.validated_data["file"]
|
||||
|
||||
if file.size > BACKGROUND_THRESHOLD:
|
||||
task = process_form_f4_file.delay(file.read(), file.name)
|
||||
return Response(
|
||||
{"success": True, "message": "Файл поставлен в очередь", "task_id": task.id},
|
||||
status=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
|
||||
try:
|
||||
result = parse_form_f4_file(file)
|
||||
return Response({"success": True, "data": FormF4ParseResultSerializer(result).data})
|
||||
except Exception as e:
|
||||
logger.exception("Ошибка обработки файла Ф-4")
|
||||
return Response({"success": False, "error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class FormF4RecordViewSet(BaseViewSet):
|
||||
queryset = FormF4Record.objects.select_related("organization").all()
|
||||
serializer_class = FormF4RecordSerializer
|
||||
service_class = FormF4Service
|
||||
|
||||
def get_serializer_class(self):
|
||||
return FormF4RecordListSerializer if self.action == "list" else FormF4RecordSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
qs = super().get_queryset()
|
||||
batch_id = self.request.query_params.get("batch_id")
|
||||
return qs.filter(load_batch=batch_id) if batch_id else qs
|
||||
Reference in New Issue
Block a user