UPDATE
This commit is contained in:
@@ -27,5 +27,80 @@ class FertilizationSectionSerializer(serializers.Serializer):
|
||||
expandableExplanation = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class NpkRatioSerializer(serializers.Serializer):
|
||||
n = serializers.FloatField(required=False)
|
||||
p = serializers.FloatField(required=False)
|
||||
k = serializers.FloatField(required=False)
|
||||
label = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class ApplicationMethodSerializer(serializers.Serializer):
|
||||
id = serializers.CharField(required=False, allow_blank=True)
|
||||
label = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class ApplicationIntervalSerializer(serializers.Serializer):
|
||||
value = serializers.FloatField(required=False)
|
||||
unit = serializers.CharField(required=False, allow_blank=True)
|
||||
label = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class DosageSerializer(serializers.Serializer):
|
||||
base_amount_per_hectare = serializers.FloatField(required=False)
|
||||
base_amount_per_square_meter = serializers.FloatField(required=False)
|
||||
unit = serializers.CharField(required=False, allow_blank=True)
|
||||
label = serializers.CharField(required=False, allow_blank=True)
|
||||
calculation_basis = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class PrimaryRecommendationSerializer(serializers.Serializer):
|
||||
fertilizer_code = serializers.CharField(required=False, allow_blank=True)
|
||||
fertilizer_name = serializers.CharField(required=False, allow_blank=True)
|
||||
display_title = serializers.CharField(required=False, allow_blank=True)
|
||||
fertilizer_type = serializers.CharField(required=False, allow_blank=True)
|
||||
npk_ratio = NpkRatioSerializer(required=False)
|
||||
application_method = ApplicationMethodSerializer(required=False)
|
||||
application_interval = ApplicationIntervalSerializer(required=False)
|
||||
dosage = DosageSerializer(required=False)
|
||||
reasoning = serializers.CharField(required=False, allow_blank=True)
|
||||
summary = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class NutrientItemSerializer(serializers.Serializer):
|
||||
key = serializers.CharField(required=False, allow_blank=True)
|
||||
name = serializers.CharField(required=False, allow_blank=True)
|
||||
value = serializers.FloatField(required=False)
|
||||
unit = serializers.CharField(required=False, allow_blank=True)
|
||||
description = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class NutrientAnalysisSerializer(serializers.Serializer):
|
||||
macro = NutrientItemSerializer(many=True, read_only=True)
|
||||
micro = NutrientItemSerializer(many=True, read_only=True)
|
||||
|
||||
|
||||
class ApplicationGuideStepSerializer(serializers.Serializer):
|
||||
step_number = serializers.IntegerField(required=False)
|
||||
title = serializers.CharField(required=False, allow_blank=True)
|
||||
description = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class ApplicationGuideSerializer(serializers.Serializer):
|
||||
safety_warning = serializers.CharField(required=False, allow_blank=True)
|
||||
steps = ApplicationGuideStepSerializer(many=True, read_only=True)
|
||||
|
||||
|
||||
class AlternativeRecommendationSerializer(serializers.Serializer):
|
||||
fertilizer_code = serializers.CharField(required=False, allow_blank=True)
|
||||
fertilizer_name = serializers.CharField(required=False, allow_blank=True)
|
||||
fertilizer_type = serializers.CharField(required=False, allow_blank=True)
|
||||
usage_method = serializers.CharField(required=False, allow_blank=True)
|
||||
description = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
|
||||
class FertilizationRecommendResponseDataSerializer(serializers.Serializer):
|
||||
primary_recommendation = PrimaryRecommendationSerializer(read_only=True)
|
||||
nutrient_analysis = NutrientAnalysisSerializer(read_only=True)
|
||||
application_guide = ApplicationGuideSerializer(read_only=True)
|
||||
alternative_recommendations = AlternativeRecommendationSerializer(many=True, read_only=True)
|
||||
sections = FertilizationSectionSerializer(many=True, read_only=True)
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from rest_framework.test import APIRequestFactory, force_authenticate
|
||||
from unittest.mock import patch
|
||||
|
||||
from external_api_adapter.adapter import AdapterResponse
|
||||
from farm_hub.models import FarmHub, FarmType
|
||||
from .views import RecommendView
|
||||
|
||||
|
||||
class FertilizationRecommendViewTests(TestCase):
|
||||
def setUp(self):
|
||||
self.factory = APIRequestFactory()
|
||||
self.user = get_user_model().objects.create_user(
|
||||
username="fert-user",
|
||||
password="secret123",
|
||||
email="fert@example.com",
|
||||
phone_number="09125556677",
|
||||
)
|
||||
self.farm_type = FarmType.objects.create(name="زراعی")
|
||||
self.farm = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name="fert-farm")
|
||||
|
||||
@patch("fertilization_recommendation.views.external_api_request")
|
||||
def test_recommend_returns_updated_response_shape(self, mock_external_api_request):
|
||||
mock_external_api_request.return_value = AdapterResponse(
|
||||
status_code=200,
|
||||
data={
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"primary_recommendation": {
|
||||
"fertilizer_code": "npk-202020",
|
||||
"fertilizer_name": "NPK 20-20-20",
|
||||
"display_title": "کود کامل متعادل",
|
||||
"fertilizer_type": "NPK",
|
||||
"npk_ratio": {"n": 20, "p": 20, "k": 20, "label": "20-20-20"},
|
||||
"application_method": {"id": "fertigation", "label": "کودآبیاری"},
|
||||
"application_interval": {"value": 14, "unit": "day", "label": "هر 14 روز"},
|
||||
"dosage": {
|
||||
"base_amount_per_hectare": 65,
|
||||
"base_amount_per_square_meter": 0.0065,
|
||||
"unit": "kg",
|
||||
"label": "65 کیلوگرم در هکتار",
|
||||
"calculation_basis": "engine-v2",
|
||||
},
|
||||
"reasoning": "متعادل برای فاز رشد",
|
||||
"summary": "مصرف منظم در این مرحله توصیه می شود",
|
||||
},
|
||||
"nutrient_analysis": {
|
||||
"macro": [
|
||||
{"key": "n", "name": "Nitrogen", "value": 20, "unit": "percent", "description": "تقویت رشد رویشی"}
|
||||
],
|
||||
"micro": [
|
||||
{"key": "zn", "name": "Zinc", "value": 2.5, "unit": "percent", "description": "بهبود رشد"}
|
||||
],
|
||||
},
|
||||
"application_guide": {
|
||||
"safety_warning": "در ساعات خنک مصرف شود",
|
||||
"steps": [
|
||||
{"step_number": 1, "title": "حل کردن", "description": "کود را در آب حل کنید"}
|
||||
],
|
||||
},
|
||||
"alternative_recommendations": [
|
||||
{
|
||||
"fertilizer_code": "npk-121236",
|
||||
"fertilizer_name": "NPK 12-12-36",
|
||||
"fertilizer_type": "NPK",
|
||||
"usage_method": "fertigation",
|
||||
"description": "برای نیاز پتاس بالا",
|
||||
}
|
||||
],
|
||||
"sections": [
|
||||
{"type": "recommendation", "title": "پیشنهاد اصلی", "icon": "leaf", "content": "NPK 20-20-20"}
|
||||
],
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
request = self.factory.post(
|
||||
"/api/fertilization/recommend/",
|
||||
{"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گندم", "growth_stage": "vegetative"},
|
||||
format="json",
|
||||
)
|
||||
force_authenticate(request, user=self.user)
|
||||
|
||||
response = RecommendView.as_view()(request)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data["code"], 200)
|
||||
self.assertIn("primary_recommendation", response.data["data"])
|
||||
self.assertIn("nutrient_analysis", response.data["data"])
|
||||
self.assertIn("application_guide", response.data["data"])
|
||||
self.assertIn("alternative_recommendations", response.data["data"])
|
||||
self.assertEqual(response.data["data"]["primary_recommendation"]["fertilizer_code"], "npk-202020")
|
||||
self.assertEqual(response.data["data"]["primary_recommendation"]["application_interval"]["value"], 14.0)
|
||||
self.assertEqual(response.data["data"]["alternative_recommendations"][0]["usage_method"], "fertigation")
|
||||
self.assertEqual(response.data["data"]["sections"][0]["type"], "recommendation")
|
||||
mock_external_api_request.assert_called_once_with(
|
||||
"ai",
|
||||
"/api/fertilization/recommend/",
|
||||
method="POST",
|
||||
payload={
|
||||
"farm_uuid": str(self.farm.farm_uuid),
|
||||
"plant_name": "گندم",
|
||||
"growth_stage": "vegetative",
|
||||
},
|
||||
)
|
||||
@@ -9,7 +9,7 @@ from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
from drf_spectacular.utils import extend_schema
|
||||
|
||||
from config.swagger import status_response
|
||||
from config.swagger import code_response, status_response
|
||||
from external_api_adapter import request as external_api_request
|
||||
from farm_hub.models import FarmHub
|
||||
from .mock_data import CONFIG_RESPONSE_DATA
|
||||
@@ -47,6 +47,30 @@ class ConfigView(FarmAccessMixin, APIView):
|
||||
|
||||
|
||||
class RecommendView(FarmAccessMixin, APIView):
|
||||
@staticmethod
|
||||
def _to_string(value):
|
||||
if value is None:
|
||||
return ""
|
||||
return str(value)
|
||||
|
||||
@staticmethod
|
||||
def _to_float(value):
|
||||
if value in (None, ""):
|
||||
return None
|
||||
try:
|
||||
return float(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _to_int(value):
|
||||
if value in (None, ""):
|
||||
return None
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _normalize_sections(raw_sections):
|
||||
if not isinstance(raw_sections, list):
|
||||
@@ -86,27 +110,195 @@ class RecommendView(FarmAccessMixin, APIView):
|
||||
normalized_sections.append(normalized_section)
|
||||
return normalized_sections
|
||||
|
||||
def _extract_public_sections(self, adapter_data):
|
||||
@staticmethod
|
||||
def _extract_public_payload(adapter_data):
|
||||
if not isinstance(adapter_data, dict):
|
||||
return []
|
||||
return {}
|
||||
|
||||
data = adapter_data.get("data")
|
||||
if isinstance(data, dict) and isinstance(data.get("sections"), list):
|
||||
return self._normalize_sections(data.get("sections"))
|
||||
if isinstance(data, dict):
|
||||
result = data.get("result")
|
||||
if isinstance(result, dict):
|
||||
return result
|
||||
return data
|
||||
|
||||
result = data.get("result") if isinstance(data, dict) else None
|
||||
if isinstance(result, dict) and isinstance(result.get("sections"), list):
|
||||
return self._normalize_sections(result.get("sections"))
|
||||
result = adapter_data.get("result")
|
||||
if isinstance(result, dict):
|
||||
return result
|
||||
|
||||
if isinstance(adapter_data.get("sections"), list):
|
||||
return self._normalize_sections(adapter_data.get("sections"))
|
||||
return adapter_data
|
||||
|
||||
return []
|
||||
def _normalize_npk_ratio(self, raw_ratio):
|
||||
if not isinstance(raw_ratio, dict):
|
||||
return {}
|
||||
|
||||
normalized = {}
|
||||
for key in ("n", "p", "k"):
|
||||
numeric_value = self._to_float(raw_ratio.get(key))
|
||||
if numeric_value is not None:
|
||||
normalized[key] = numeric_value
|
||||
|
||||
label = self._to_string(raw_ratio.get("label")).strip()
|
||||
if label:
|
||||
normalized["label"] = label
|
||||
|
||||
return normalized
|
||||
|
||||
def _normalize_named_object(self, raw_object):
|
||||
if not isinstance(raw_object, dict):
|
||||
return {}
|
||||
|
||||
normalized = {}
|
||||
for key in ("id", "label", "unit", "calculation_basis"):
|
||||
value = self._to_string(raw_object.get(key)).strip()
|
||||
if value:
|
||||
normalized[key] = value
|
||||
|
||||
for key in ("value", "base_amount_per_hectare", "base_amount_per_square_meter"):
|
||||
numeric_value = self._to_float(raw_object.get(key))
|
||||
if numeric_value is not None:
|
||||
normalized[key] = numeric_value
|
||||
|
||||
return normalized
|
||||
|
||||
def _normalize_primary_recommendation(self, payload):
|
||||
raw_data = payload.get("primary_recommendation")
|
||||
if not isinstance(raw_data, dict):
|
||||
return {}
|
||||
|
||||
normalized = {}
|
||||
for key in (
|
||||
"fertilizer_code",
|
||||
"fertilizer_name",
|
||||
"display_title",
|
||||
"fertilizer_type",
|
||||
"reasoning",
|
||||
"summary",
|
||||
):
|
||||
value = self._to_string(raw_data.get(key)).strip()
|
||||
if value:
|
||||
normalized[key] = value
|
||||
|
||||
npk_ratio = self._normalize_npk_ratio(raw_data.get("npk_ratio"))
|
||||
if npk_ratio:
|
||||
normalized["npk_ratio"] = npk_ratio
|
||||
|
||||
application_method = self._normalize_named_object(raw_data.get("application_method"))
|
||||
if application_method:
|
||||
normalized["application_method"] = {
|
||||
key: value for key, value in application_method.items() if key in {"id", "label"}
|
||||
}
|
||||
|
||||
application_interval = self._normalize_named_object(raw_data.get("application_interval"))
|
||||
if application_interval:
|
||||
normalized["application_interval"] = {
|
||||
key: value for key, value in application_interval.items() if key in {"value", "unit", "label"}
|
||||
}
|
||||
|
||||
dosage = self._normalize_named_object(raw_data.get("dosage"))
|
||||
if dosage:
|
||||
dosage_label = self._to_string(raw_data.get("dosage", {}).get("label")).strip()
|
||||
if dosage_label:
|
||||
dosage["label"] = dosage_label
|
||||
normalized["dosage"] = {
|
||||
key: value
|
||||
for key, value in dosage.items()
|
||||
if key in {"base_amount_per_hectare", "base_amount_per_square_meter", "unit", "label", "calculation_basis"}
|
||||
}
|
||||
|
||||
return normalized
|
||||
|
||||
def _normalize_nutrient_items(self, items):
|
||||
if not isinstance(items, list):
|
||||
return []
|
||||
|
||||
normalized_items = []
|
||||
for item in items:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
normalized_item = {}
|
||||
for key in ("key", "name", "unit", "description"):
|
||||
value = self._to_string(item.get(key)).strip()
|
||||
if value:
|
||||
normalized_item[key] = value
|
||||
numeric_value = self._to_float(item.get("value"))
|
||||
if numeric_value is not None:
|
||||
normalized_item["value"] = numeric_value
|
||||
if normalized_item:
|
||||
normalized_items.append(normalized_item)
|
||||
return normalized_items
|
||||
|
||||
def _normalize_application_guide(self, payload):
|
||||
raw_data = payload.get("application_guide")
|
||||
if not isinstance(raw_data, dict):
|
||||
return {}
|
||||
|
||||
normalized = {}
|
||||
safety_warning = self._to_string(raw_data.get("safety_warning")).strip()
|
||||
if safety_warning:
|
||||
normalized["safety_warning"] = safety_warning
|
||||
|
||||
raw_steps = raw_data.get("steps")
|
||||
if isinstance(raw_steps, list):
|
||||
steps = []
|
||||
for step in raw_steps:
|
||||
if not isinstance(step, dict):
|
||||
continue
|
||||
normalized_step = {}
|
||||
step_number = self._to_int(step.get("step_number"))
|
||||
if step_number is not None:
|
||||
normalized_step["step_number"] = step_number
|
||||
for key in ("title", "description"):
|
||||
value = self._to_string(step.get(key)).strip()
|
||||
if value:
|
||||
normalized_step[key] = value
|
||||
if normalized_step:
|
||||
steps.append(normalized_step)
|
||||
normalized["steps"] = steps
|
||||
|
||||
return normalized
|
||||
|
||||
def _normalize_alternatives(self, payload):
|
||||
raw_items = payload.get("alternative_recommendations")
|
||||
if not isinstance(raw_items, list):
|
||||
return []
|
||||
|
||||
alternatives = []
|
||||
for item in raw_items:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
normalized_item = {}
|
||||
for key in ("fertilizer_code", "fertilizer_name", "fertilizer_type", "usage_method", "description"):
|
||||
value = self._to_string(item.get(key)).strip()
|
||||
if value:
|
||||
normalized_item[key] = value
|
||||
if normalized_item:
|
||||
alternatives.append(normalized_item)
|
||||
return alternatives
|
||||
|
||||
def _normalize_response_payload(self, adapter_data):
|
||||
payload = self._extract_public_payload(adapter_data)
|
||||
if not isinstance(payload, dict):
|
||||
payload = {}
|
||||
|
||||
normalized_sections = self._normalize_sections(payload.get("sections"))
|
||||
nutrient_analysis = payload.get("nutrient_analysis") if isinstance(payload.get("nutrient_analysis"), dict) else {}
|
||||
|
||||
return {
|
||||
"primary_recommendation": self._normalize_primary_recommendation(payload),
|
||||
"nutrient_analysis": {
|
||||
"macro": self._normalize_nutrient_items(nutrient_analysis.get("macro")),
|
||||
"micro": self._normalize_nutrient_items(nutrient_analysis.get("micro")),
|
||||
},
|
||||
"application_guide": self._normalize_application_guide(payload),
|
||||
"alternative_recommendations": self._normalize_alternatives(payload),
|
||||
"sections": normalized_sections,
|
||||
}
|
||||
|
||||
@extend_schema(
|
||||
tags=["Fertilization Recommendation"],
|
||||
request=FertilizationRecommendRequestSerializer,
|
||||
responses={200: status_response("FertilizationRecommendResponse", data=FertilizationRecommendResponseDataSerializer())},
|
||||
responses={200: code_response("FertilizationRecommendResponse", data=FertilizationRecommendResponseDataSerializer())},
|
||||
)
|
||||
def post(self, request):
|
||||
serializer = FertilizationRecommendRequestSerializer(data=request.data)
|
||||
@@ -125,14 +317,14 @@ class RecommendView(FarmAccessMixin, APIView):
|
||||
)
|
||||
|
||||
response_data = adapter_response.data if isinstance(adapter_response.data, dict) else {}
|
||||
public_sections = self._extract_public_sections(response_data)
|
||||
public_data = self._normalize_response_payload(response_data)
|
||||
|
||||
logger.warning(
|
||||
"Fertilization recommendation response parsed: farm_uuid=%s status_code=%s response_keys=%s sections_count=%s",
|
||||
str(farm.farm_uuid),
|
||||
adapter_response.status_code,
|
||||
sorted(response_data.keys()) if isinstance(response_data, dict) else None,
|
||||
len(public_sections),
|
||||
len(public_data.get("sections", [])),
|
||||
)
|
||||
|
||||
FertilizationRecommendationRequest.objects.create(
|
||||
@@ -158,9 +350,7 @@ class RecommendView(FarmAccessMixin, APIView):
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"sections": public_sections,
|
||||
},
|
||||
"data": public_data,
|
||||
},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user