UPDATE
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from django.apps import apps
|
||||
|
||||
from drf_spectacular.utils import OpenApiExample, extend_schema
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
@@ -6,6 +8,8 @@ from rest_framework.views import APIView
|
||||
from config.openapi import build_envelope_serializer, build_response
|
||||
|
||||
from .serializers import (
|
||||
FertilizationPlanParserRequestSerializer,
|
||||
FertilizationPlanParserResponseSerializer,
|
||||
FertilizationRecommendationResponseDataSerializer,
|
||||
FertilizationRecommendRequestSerializer,
|
||||
)
|
||||
@@ -20,6 +24,10 @@ FertilizationResponseSerializer = build_envelope_serializer(
|
||||
"FertilizationResponseSerializer",
|
||||
data_schema=FertilizationRecommendationResponseDataSerializer,
|
||||
)
|
||||
FertilizationPlanParserEnvelopeSerializer = build_envelope_serializer(
|
||||
"FertilizationPlanParserEnvelopeSerializer",
|
||||
data_schema=FertilizationPlanParserResponseSerializer,
|
||||
)
|
||||
|
||||
|
||||
class FertilizationRecommendView(APIView):
|
||||
@@ -147,3 +155,80 @@ class FertilizationRecommendView(APIView):
|
||||
{"code": 200, "msg": "success", "data": final_result},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class FertilizationPlanParserView(APIView):
|
||||
@extend_schema(
|
||||
tags=["Fertilization Recommendation"],
|
||||
summary="استخراج برنامه کودهی از متن آزاد",
|
||||
description=(
|
||||
"توضیح متنی کاربر درباره برنامه کودهی را می گیرد و آن را به JSON ساختاریافته تبدیل می کند. "
|
||||
"اگر اطلاعات کافی نباشد، سوالات تکمیلی لازم را برمی گرداند تا در درخواست بعدی پاسخ داده شوند."
|
||||
),
|
||||
request=FertilizationPlanParserRequestSerializer,
|
||||
responses={
|
||||
200: build_response(
|
||||
FertilizationPlanParserEnvelopeSerializer,
|
||||
"نتیجه استخراج یا سوالات تکمیلی برنامه کودهی.",
|
||||
),
|
||||
400: build_response(
|
||||
FertilizationValidationErrorSerializer,
|
||||
"داده ورودی نامعتبر است.",
|
||||
),
|
||||
500: build_response(
|
||||
FertilizationValidationErrorSerializer,
|
||||
"خطا در پردازش برنامه کودهی.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست کامل",
|
||||
value={
|
||||
"message": "برای گندم در مرحله پنجه زنی هر 12 روز یک بار 20-20-20 به مقدار 35 کیلوگرم در هکتار از طریق کودآبیاری می دهم.",
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
OpenApiExample(
|
||||
"نمونه درخواست تکمیلی",
|
||||
value={
|
||||
"partial_plan": {
|
||||
"crop_name": "گندم",
|
||||
"applications": [{"fertilizer_name": "20-20-20"}],
|
||||
},
|
||||
"answers": {
|
||||
"amount": "35 کیلوگرم در هکتار",
|
||||
"timing": "هر 12 روز یک بار",
|
||||
},
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request):
|
||||
serializer = FertilizationPlanParserRequestSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response(
|
||||
{"code": 400, "msg": "داده نامعتبر.", "data": serializer.errors},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
validated = serializer.validated_data
|
||||
service = apps.get_app_config("fertilization").get_free_text_plan_parser_service()
|
||||
try:
|
||||
result = service.parse_plan(
|
||||
message=validated.get("message", ""),
|
||||
answers=validated.get("answers"),
|
||||
partial_plan=validated.get("partial_plan"),
|
||||
farm_uuid=validated.get("farm_uuid"),
|
||||
)
|
||||
except Exception as exc:
|
||||
return Response(
|
||||
{"code": 500, "msg": f"خطا در پردازش برنامه کودهی: {exc}", "data": None},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"code": 200, "msg": "موفق", "data": result},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user