108 lines
3.8 KiB
Python
108 lines
3.8 KiB
Python
from drf_spectacular.utils import (
|
|
OpenApiExample,
|
|
OpenApiResponse,
|
|
extend_schema,
|
|
)
|
|
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from config.openapi import (
|
|
build_envelope_serializer,
|
|
build_response,
|
|
)
|
|
|
|
from .serializers import FertilizationRecommendRequestSerializer
|
|
|
|
|
|
FertilizationValidationErrorSerializer = build_envelope_serializer(
|
|
"FertilizationValidationErrorSerializer",
|
|
data_required=False,
|
|
allow_null=True,
|
|
)
|
|
FertilizationResponseSerializer = build_envelope_serializer(
|
|
"FertilizationResponseSerializer",
|
|
data_schema=None,
|
|
)
|
|
|
|
|
|
class FertilizationRecommendView(APIView):
|
|
"""
|
|
توصیه کودهی به صورت مستقیم.
|
|
POST با farm_uuid، plant_name، growth_stage.
|
|
اطلاعات گیاه از plant app دریافت میشود.
|
|
نیازی به دریافت نوع آبیاری نیست.
|
|
"""
|
|
|
|
@extend_schema(
|
|
tags=["Fertilization Recommendation"],
|
|
summary="درخواست توصیه کودهی",
|
|
description=(
|
|
"دادههای سنسور و گیاه را دریافت کرده و "
|
|
"توصیه کودهی را مستقیم برمیگرداند. "
|
|
"اطلاعات گیاه از جدول Plant بارگذاری میشود. "
|
|
"محاسبات مربوط به نیاز آبی در این endpoint انجام نمیشود و مستقل از توصیه کودهی است."
|
|
),
|
|
request=FertilizationRecommendRequestSerializer,
|
|
responses={
|
|
200: build_response(
|
|
FertilizationResponseSerializer,
|
|
"توصیه کودهی با موفقیت تولید شد.",
|
|
),
|
|
400: build_response(
|
|
FertilizationValidationErrorSerializer,
|
|
"پارامتر ورودی نامعتبر است.",
|
|
),
|
|
500: build_response(
|
|
FertilizationValidationErrorSerializer,
|
|
"خطا در تولید توصیه کودهی.",
|
|
),
|
|
},
|
|
examples=[
|
|
OpenApiExample(
|
|
"نمونه درخواست",
|
|
value={
|
|
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
|
"plant_name": "گوجهفرنگی",
|
|
"growth_stage": "گلدهی",
|
|
},
|
|
request_only=True,
|
|
),
|
|
],
|
|
)
|
|
def post(self, request):
|
|
from rag.services.fertilization import get_fertilization_recommendation
|
|
|
|
serializer = FertilizationRecommendRequestSerializer(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
|
|
farm_uuid = validated["farm_uuid"]
|
|
plant_name = validated.get("plant_name")
|
|
growth_stage = validated.get("growth_stage")
|
|
query = validated.get("query")
|
|
|
|
try:
|
|
result = get_fertilization_recommendation(
|
|
farm_uuid=farm_uuid,
|
|
plant_name=plant_name,
|
|
growth_stage=growth_stage,
|
|
query=query,
|
|
)
|
|
except Exception as exc:
|
|
return Response(
|
|
{"code": 500, "msg": f"خطا در تولید توصیه کودهی: {exc}", "data": None},
|
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
)
|
|
|
|
# Public API exposes only the final farmer-facing recommendation object.
|
|
final_result = {"sections": result.get("sections", [])}
|
|
return Response(
|
|
{"code": 200, "msg": "success", "data": final_result},
|
|
status=status.HTTP_200_OK,
|
|
)
|