UPDATE
This commit is contained in:
@@ -13,6 +13,8 @@ from config.openapi import (
|
||||
from .models import IrrigationMethod
|
||||
from .serializers import (
|
||||
IrrigationMethodSerializer,
|
||||
IrrigationPlanParserRequestSerializer,
|
||||
IrrigationPlanParserResponseSerializer,
|
||||
IrrigationRecommendRequestSerializer,
|
||||
WaterStressRequestSerializer,
|
||||
WaterStressResponseSerializer,
|
||||
@@ -37,6 +39,10 @@ IrrigationRecommendResponseSerializer = build_envelope_serializer(
|
||||
"IrrigationRecommendResponseSerializer",
|
||||
data_schema=None,
|
||||
)
|
||||
IrrigationPlanParserEnvelopeSerializer = build_envelope_serializer(
|
||||
"IrrigationPlanParserEnvelopeSerializer",
|
||||
IrrigationPlanParserResponseSerializer,
|
||||
)
|
||||
WaterStressEnvelopeSerializer = build_envelope_serializer(
|
||||
"WaterStressEnvelopeSerializer",
|
||||
WaterStressResponseSerializer,
|
||||
@@ -219,6 +225,83 @@ class IrrigationRecommendView(APIView):
|
||||
)
|
||||
|
||||
|
||||
class IrrigationPlanParserView(APIView):
|
||||
@extend_schema(
|
||||
tags=["Irrigation Recommendation"],
|
||||
summary="استخراج برنامه آبیاری از متن آزاد",
|
||||
description=(
|
||||
"توضیح متنی کاربر درباره برنامه آبیاری را می گیرد و آن را به JSON ساختاریافته تبدیل می کند. "
|
||||
"اگر اطلاعات کافی نباشد، سوالات تکمیلی لازم را برمی گرداند تا در درخواست بعدی پاسخ داده شوند."
|
||||
),
|
||||
request=IrrigationPlanParserRequestSerializer,
|
||||
responses={
|
||||
200: build_response(
|
||||
IrrigationPlanParserEnvelopeSerializer,
|
||||
"نتیجه استخراج یا سوالات تکمیلی برنامه آبیاری.",
|
||||
),
|
||||
400: build_response(
|
||||
IrrigationValidationErrorSerializer,
|
||||
"داده ورودی نامعتبر است.",
|
||||
),
|
||||
500: build_response(
|
||||
IrrigationValidationErrorSerializer,
|
||||
"خطا در پردازش برنامه آبیاری.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست کامل",
|
||||
value={
|
||||
"message": "برای گوجه فرنگی با آبیاری قطره ای هر سه روز یک بار صبح زود 25 دقیقه آبیاری می کنم و حدود 18 لیتر برای هر بوته می دهم.",
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
OpenApiExample(
|
||||
"نمونه درخواست تکمیلی",
|
||||
value={
|
||||
"partial_plan": {
|
||||
"crop_name": "گوجه فرنگی",
|
||||
"irrigation_method": "قطره ای",
|
||||
},
|
||||
"answers": {
|
||||
"water_amount_per_event": "18 لیتر برای هر بوته",
|
||||
"preferred_time_of_day": "صبح زود",
|
||||
},
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request):
|
||||
serializer = IrrigationPlanParserRequestSerializer(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("irrigation").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,
|
||||
)
|
||||
|
||||
|
||||
class IrrigationMethodDetailView(APIView):
|
||||
"""دریافت، ویرایش و حذف یک روش آبیاری."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user