2026-02-25 12:21:53 +03:30
|
|
|
"""
|
|
|
|
|
Fertilization Recommendation API views.
|
|
|
|
|
"""
|
|
|
|
|
|
2026-03-24 20:10:48 +03:30
|
|
|
from rest_framework import serializers, status
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
from drf_spectacular.types import OpenApiTypes
|
2026-03-26 15:39:31 +03:30
|
|
|
from drf_spectacular.utils import OpenApiParameter, extend_schema
|
2026-02-25 12:21:53 +03:30
|
|
|
|
2026-03-24 20:10:48 +03:30
|
|
|
from config.swagger import status_response
|
2026-03-25 00:51:04 +03:30
|
|
|
from external_api_adapter import request as external_api_request
|
2026-04-02 23:25:39 +03:30
|
|
|
from farm_hub.models import FarmHub
|
2026-03-25 00:51:04 +03:30
|
|
|
from .mock_data import CONFIG_RESPONSE_DATA
|
2026-04-02 23:25:39 +03:30
|
|
|
from .models import FertilizationRecommendationRequest
|
2026-03-26 15:39:31 +03:30
|
|
|
from .serializers import (
|
|
|
|
|
FertilizationRecommendRequestSerializer,
|
|
|
|
|
FertilizationRecommendResponseDataSerializer,
|
|
|
|
|
FertilizationTaskStatusDataSerializer,
|
|
|
|
|
FertilizationTaskSubmitDataSerializer,
|
|
|
|
|
)
|
2026-02-25 12:21:53 +03:30
|
|
|
|
|
|
|
|
|
2026-04-02 23:25:39 +03:30
|
|
|
class FarmAccessMixin:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _get_farm(request, farm_uuid):
|
|
|
|
|
if not farm_uuid:
|
|
|
|
|
raise serializers.ValidationError({"farm_uuid": ["This field is required."]})
|
|
|
|
|
try:
|
|
|
|
|
return FarmHub.objects.get(farm_uuid=farm_uuid, owner=request.user)
|
|
|
|
|
except FarmHub.DoesNotExist as exc:
|
|
|
|
|
raise serializers.ValidationError({"farm_uuid": ["Farm not found."]}) from exc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigView(FarmAccessMixin, APIView):
|
2026-03-24 20:10:48 +03:30
|
|
|
@extend_schema(
|
|
|
|
|
tags=["Fertilization Recommendation"],
|
2026-04-02 23:25:39 +03:30
|
|
|
parameters=[
|
|
|
|
|
OpenApiParameter(name="farm_uuid", type=OpenApiTypes.UUID, location=OpenApiParameter.QUERY, required=True),
|
|
|
|
|
],
|
2026-03-24 20:10:48 +03:30
|
|
|
responses={200: status_response("FertilizationConfigResponse", data=serializers.JSONField())},
|
|
|
|
|
)
|
2026-02-25 12:21:53 +03:30
|
|
|
def get(self, request):
|
2026-04-02 23:25:39 +03:30
|
|
|
farm = self._get_farm(request, request.query_params.get("farm_uuid"))
|
|
|
|
|
data = dict(CONFIG_RESPONSE_DATA)
|
|
|
|
|
data["farm_uuid"] = str(farm.farm_uuid)
|
|
|
|
|
return Response({"status": "success", "data": data}, status=status.HTTP_200_OK)
|
2026-02-25 12:21:53 +03:30
|
|
|
|
|
|
|
|
|
2026-04-02 23:25:39 +03:30
|
|
|
class RecommendView(FarmAccessMixin, APIView):
|
2026-03-24 20:10:48 +03:30
|
|
|
@extend_schema(
|
|
|
|
|
tags=["Fertilization Recommendation"],
|
2026-03-26 15:39:31 +03:30
|
|
|
request=FertilizationRecommendRequestSerializer,
|
|
|
|
|
responses={200: status_response("FertilizationRecommendResponse", data=FertilizationRecommendResponseDataSerializer())},
|
2026-03-24 20:10:48 +03:30
|
|
|
)
|
2026-02-25 12:21:53 +03:30
|
|
|
def post(self, request):
|
2026-04-02 23:25:39 +03:30
|
|
|
serializer = FertilizationRecommendRequestSerializer(data=request.data)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
payload = serializer.validated_data.copy()
|
|
|
|
|
farm = self._get_farm(request, payload.get("farm_uuid"))
|
|
|
|
|
payload["farm_uuid"] = str(farm.farm_uuid)
|
|
|
|
|
|
2026-03-25 00:51:04 +03:30
|
|
|
adapter_response = external_api_request(
|
|
|
|
|
"ai",
|
|
|
|
|
"/fertilization/recommend",
|
|
|
|
|
method="POST",
|
2026-04-02 23:25:39 +03:30
|
|
|
payload=payload,
|
2026-02-25 12:21:53 +03:30
|
|
|
)
|
2026-03-26 15:39:31 +03:30
|
|
|
|
2026-04-02 23:25:39 +03:30
|
|
|
response_data = adapter_response.data if isinstance(adapter_response.data, dict) else {}
|
|
|
|
|
FertilizationRecommendationRequest.objects.create(
|
|
|
|
|
farm=farm,
|
|
|
|
|
crop_id=payload.get("crop_id", ""),
|
|
|
|
|
growth_stage=payload.get("growth_stage", ""),
|
|
|
|
|
task_id=str(response_data.get("data", {}).get("task_id") or response_data.get("task_id") or ""),
|
|
|
|
|
status=str(response_data.get("data", {}).get("status") or response_data.get("status") or ""),
|
|
|
|
|
request_payload=payload,
|
|
|
|
|
response_payload=adapter_response.data if isinstance(adapter_response.data, dict) else {"raw": adapter_response.data},
|
|
|
|
|
)
|
|
|
|
|
return Response(adapter_response.data, status=adapter_response.status_code)
|
2026-03-26 15:39:31 +03:30
|
|
|
|
|
|
|
|
|
2026-04-02 23:25:39 +03:30
|
|
|
class RecommendTaskStatusView(FarmAccessMixin, APIView):
|
2026-03-26 15:39:31 +03:30
|
|
|
@extend_schema(
|
|
|
|
|
tags=["Fertilization Recommendation"],
|
|
|
|
|
parameters=[
|
|
|
|
|
OpenApiParameter(name="task_id", type=OpenApiTypes.STR, location=OpenApiParameter.PATH),
|
2026-04-02 23:25:39 +03:30
|
|
|
OpenApiParameter(name="farm_uuid", type=OpenApiTypes.UUID, location=OpenApiParameter.QUERY, required=True),
|
2026-03-26 15:39:31 +03:30
|
|
|
],
|
|
|
|
|
responses={200: status_response("FertilizationRecommendTaskStatusResponse", data=FertilizationTaskStatusDataSerializer())},
|
|
|
|
|
)
|
|
|
|
|
def get(self, request, task_id):
|
2026-04-02 23:25:39 +03:30
|
|
|
farm = self._get_farm(request, request.query_params.get("farm_uuid"))
|
2026-03-26 15:39:31 +03:30
|
|
|
adapter_response = external_api_request(
|
|
|
|
|
"ai",
|
|
|
|
|
f"/fertilization/status/{task_id}",
|
|
|
|
|
method="GET",
|
2026-04-02 23:25:39 +03:30
|
|
|
query={"farm_uuid": str(farm.farm_uuid)},
|
|
|
|
|
)
|
|
|
|
|
response_data = adapter_response.data if isinstance(adapter_response.data, dict) else {}
|
|
|
|
|
FertilizationRecommendationRequest.objects.filter(farm=farm, task_id=task_id).update(
|
|
|
|
|
status=str(response_data.get("data", {}).get("status") or response_data.get("status") or ""),
|
|
|
|
|
response_payload=adapter_response.data if isinstance(adapter_response.data, dict) else {"raw": adapter_response.data},
|
2026-03-26 15:39:31 +03:30
|
|
|
)
|
|
|
|
|
return Response(adapter_response.data, status=adapter_response.status_code)
|