diff --git a/config/settings.py b/config/settings.py index 0f891ea..3244bfa 100644 --- a/config/settings.py +++ b/config/settings.py @@ -55,6 +55,7 @@ INSTALLED_APPS = [ "fertilization_recommendation", "farm_ai_assistant", "notifications.apps.NotificationsConfig", + "plants.apps.PlantsConfig", "external_api_adapter.apps.ExternalApiAdapterConfig", "sensor_external_api.apps.SensorExternalApiConfig", "rest_framework", diff --git a/config/urls.py b/config/urls.py index a3f8cbd..c674a1f 100644 --- a/config/urls.py +++ b/config/urls.py @@ -33,6 +33,7 @@ urlpatterns = [ path("api/farm-ai-assistant/", include("farm_ai_assistant.urls")), path("api/notifications/", include("notifications.urls")), path("api/farm-alerts/", include("farm_alerts.urls")), + path("api/plants/", include("plants.urls")), path("api/sensor-external-api/", include("sensor_external_api.urls")), ] diff --git a/farm_hub/catalog.py b/farm_hub/catalog.py index 762e945..a36a36a 100644 --- a/farm_hub/catalog.py +++ b/farm_hub/catalog.py @@ -1,23 +1,23 @@ CATALOG_SEED_DATA = { "زراعی": [ - {"name": "گندم", "planting_season": "پاییز", "harvest_time": "اواخر بهار", "soil": "لومی"}, - {"name": "ذرت", "planting_season": "بهار", "harvest_time": "تابستان", "soil": "لومی شنی"}, - {"name": "جو", "planting_season": "پاییز", "harvest_time": "اواخر بهار", "soil": "لومی"}, - {"name": "کلزا", "planting_season": "پاییز", "harvest_time": "بهار", "soil": "لومی رسی"}, - {"name": "پنبه", "planting_season": "بهار", "harvest_time": "پاییز", "soil": "لومی"}, + {"name": "گندم", "planting_season": "پاییز", "harvest_time": "اواخر بهار", "soil": "لومی", "icon": "wheat", "growth_stage": "vegetative", "growth_stages": ["initial", "vegetative", "flowering", "maturity"]}, + {"name": "ذرت", "planting_season": "بهار", "harvest_time": "تابستان", "soil": "لومی شنی", "icon": "corn", "growth_stage": "vegetative", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, + {"name": "جو", "planting_season": "پاییز", "harvest_time": "اواخر بهار", "soil": "لومی", "icon": "leaf", "growth_stage": "vegetative", "growth_stages": ["initial", "vegetative", "flowering", "maturity"]}, + {"name": "کلزا", "planting_season": "پاییز", "harvest_time": "بهار", "soil": "لومی رسی", "icon": "leaf", "growth_stage": "flowering", "growth_stages": ["initial", "vegetative", "flowering", "maturity"]}, + {"name": "پنبه", "planting_season": "بهار", "harvest_time": "پاییز", "soil": "لومی", "icon": "leaf", "growth_stage": "fruiting", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, ], "درختی": [ - {"name": "سیب", "planting_season": "زمستان", "harvest_time": "پاییز", "soil": "لومی"}, - {"name": "پسته", "planting_season": "زمستان", "harvest_time": "اواخر تابستان", "soil": "شنی لومی"}, - {"name": "انگور", "planting_season": "اواخر زمستان", "harvest_time": "تابستان", "soil": "لومی"}, - {"name": "انار", "planting_season": "اواخر زمستان", "harvest_time": "پاییز", "soil": "لومی شنی"}, + {"name": "سیب", "planting_season": "زمستان", "harvest_time": "پاییز", "soil": "لومی", "icon": "apple", "growth_stage": "flowering", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, + {"name": "پسته", "planting_season": "زمستان", "harvest_time": "اواخر تابستان", "soil": "شنی لومی", "icon": "leaf", "growth_stage": "fruiting", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, + {"name": "انگور", "planting_season": "اواخر زمستان", "harvest_time": "تابستان", "soil": "لومی", "icon": "grape", "growth_stage": "fruiting", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, + {"name": "انار", "planting_season": "اواخر زمستان", "harvest_time": "پاییز", "soil": "لومی شنی", "icon": "leaf", "growth_stage": "flowering", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, ], "غرقابی": [ - {"name": "برنج", "planting_season": "بهار", "harvest_time": "اواخر تابستان", "soil": "رسی"}, + {"name": "برنج", "planting_season": "بهار", "harvest_time": "اواخر تابستان", "soil": "رسی", "icon": "leaf", "growth_stage": "vegetative", "growth_stages": ["initial", "vegetative", "flowering", "maturity"]}, ], "گلخانه ای": [ - {"name": "گوجه فرنگی", "planting_season": "چهار فصل", "harvest_time": "چند مرحله ای", "soil": "کوکوپیت"}, - {"name": "خیار", "planting_season": "چهار فصل", "harvest_time": "چند مرحله ای", "soil": "پرلیت"}, - {"name": "فلفل دلمه ای", "planting_season": "چهار فصل", "harvest_time": "چند مرحله ای", "soil": "بستر هیدروپونیک"}, + {"name": "گوجه فرنگی", "planting_season": "چهار فصل", "harvest_time": "چند مرحله ای", "soil": "کوکوپیت", "icon": "tomato", "growth_stage": "fruiting", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, + {"name": "خیار", "planting_season": "چهار فصل", "harvest_time": "چند مرحله ای", "soil": "پرلیت", "icon": "leaf", "growth_stage": "fruiting", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, + {"name": "فلفل دلمه ای", "planting_season": "چهار فصل", "harvest_time": "چند مرحله ای", "soil": "بستر هیدروپونیک", "icon": "pepper", "growth_stage": "flowering", "growth_stages": ["initial", "vegetative", "flowering", "fruiting", "maturity"]}, ], } diff --git a/farm_hub/migrations/0008_product_plant_selector_fields.py b/farm_hub/migrations/0008_product_plant_selector_fields.py new file mode 100644 index 0000000..2b687d6 --- /dev/null +++ b/farm_hub/migrations/0008_product_plant_selector_fields.py @@ -0,0 +1,25 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("farm_hub", "0007_farmhub_subscription_plan"), + ] + + operations = [ + migrations.AddField( + model_name="product", + name="growth_stage", + field=models.CharField(blank=True, default="", help_text="مرحله رشد فعلی", max_length=255), + ), + migrations.AddField( + model_name="product", + name="growth_stages", + field=models.JSONField(blank=True, default=list, help_text="فهرست مراحل رشد محصول"), + ), + migrations.AddField( + model_name="product", + name="icon", + field=models.CharField(blank=True, default="", help_text="آیکون محصول برای فرانت", max_length=100), + ), + ] diff --git a/farm_hub/models.py b/farm_hub/models.py index b31fecb..6f49853 100644 --- a/farm_hub/models.py +++ b/farm_hub/models.py @@ -35,6 +35,9 @@ class Product(models.Model): watering = models.CharField(max_length=255, blank=True, default="", help_text="آبیاری") soil = models.CharField(max_length=255, blank=True, default="", help_text="خاک مناسب") temperature = models.CharField(max_length=255, blank=True, default="", help_text="دمای مناسب") + growth_stage = models.CharField(max_length=255, blank=True, default="", help_text="مرحله رشد فعلی") + growth_stages = models.JSONField(blank=True, default=list, help_text="فهرست مراحل رشد محصول") + icon = models.CharField(max_length=100, blank=True, default="", help_text="آیکون محصول برای فرانت") planting_season = models.CharField(max_length=255, blank=True, default="", help_text="فصل کاشت") harvest_time = models.CharField(max_length=255, blank=True, default="", help_text="زمان برداشت") spacing = models.CharField(max_length=255, blank=True, default="", help_text="فاصله کاشت") diff --git a/fertilization_recommendation/serializers.py b/fertilization_recommendation/serializers.py index d8a018e..8e569fc 100644 --- a/fertilization_recommendation/serializers.py +++ b/fertilization_recommendation/serializers.py @@ -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) diff --git a/fertilization_recommendation/tests.py b/fertilization_recommendation/tests.py new file mode 100644 index 0000000..fdc6a64 --- /dev/null +++ b/fertilization_recommendation/tests.py @@ -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", + }, + ) diff --git a/fertilization_recommendation/views.py b/fertilization_recommendation/views.py index 656b8d9..7fc10cb 100644 --- a/fertilization_recommendation/views.py +++ b/fertilization_recommendation/views.py @@ -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, ) diff --git a/plants/__init__.py b/plants/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plants/apps.py b/plants/apps.py new file mode 100644 index 0000000..d9027af --- /dev/null +++ b/plants/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig + + +class PlantsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "plants" + verbose_name = "Plants" diff --git a/plants/migrations/__init__.py b/plants/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plants/models.py b/plants/models.py new file mode 100644 index 0000000..76d018f --- /dev/null +++ b/plants/models.py @@ -0,0 +1,3 @@ +from farm_hub.models import Product + +__all__ = ["Product"] diff --git a/plants/serializers.py b/plants/serializers.py new file mode 100644 index 0000000..ff7742c --- /dev/null +++ b/plants/serializers.py @@ -0,0 +1,33 @@ +from rest_framework import serializers + +from farm_hub.models import Product + + +class PlantSerializer(serializers.ModelSerializer): + id = serializers.IntegerField(read_only=True) + + class Meta: + model = Product + fields = [ + "id", + "name", + "light", + "watering", + "soil", + "temperature", + "growth_stage", + "growth_stages", + "icon", + "planting_season", + "harvest_time", + "spacing", + "fertilizer", + "created_at", + "updated_at", + ] + + +class PlantNameSerializer(serializers.ModelSerializer): + class Meta: + model = Product + fields = ["name", "icon", "growth_stages"] diff --git a/plants/services.py b/plants/services.py new file mode 100644 index 0000000..2ff7637 --- /dev/null +++ b/plants/services.py @@ -0,0 +1,169 @@ +from django.db import transaction + +from external_api_adapter import request as external_api_request +from external_api_adapter.exceptions import ExternalAPIRequestError +from farm_hub.models import FarmType, Product + + +DEFAULT_FARM_TYPE_NAME = "زراعی" +DEFAULT_ICON = "leaf" +DEFAULT_GROWTH_STAGES = [ + "initial", + "vegetative", + "flowering", + "fruiting", + "maturity", +] +AI_PLANTS_PATH = "/api/plants/" + + +class PlantSyncError(Exception): + pass + + +def _extract_plant_items(adapter_data): + if isinstance(adapter_data, list): + return adapter_data + if not isinstance(adapter_data, dict): + return [] + + data = adapter_data.get("data") + if isinstance(data, list): + return data + if isinstance(data, dict): + result = data.get("result") + if isinstance(result, list): + return result + + result = adapter_data.get("result") + if isinstance(result, list): + return result + return [] + + +def _clean_stage_name(value): + stage = str(value or "").strip() + return stage + + +def _merge_growth_stages(product, supplied_stages=None): + stages = [] + seen = set() + has_explicit_stage_data = False + + for stage in supplied_stages or []: + normalized = _clean_stage_name(stage) + if normalized and normalized not in seen: + has_explicit_stage_data = True + seen.add(normalized) + stages.append(normalized) + + current_stage = _clean_stage_name(getattr(product, "growth_stage", "")) + if current_stage and current_stage not in seen: + has_explicit_stage_data = True + seen.add(current_stage) + stages.append(current_stage) + + if not has_explicit_stage_data: + for stage in DEFAULT_GROWTH_STAGES: + seen.add(stage) + stages.append(stage) + + thresholds = product.growth_profile.get("stage_thresholds", {}) if isinstance(product.growth_profile, dict) else {} + if isinstance(thresholds, dict): + for stage_name in thresholds.keys(): + normalized = _clean_stage_name(stage_name) + if normalized and normalized not in seen: + seen.add(normalized) + stages.append(normalized) + + return stages + + +@transaction.atomic +def ensure_plant_defaults(queryset=None): + products = list(queryset if queryset is not None else Product.objects.all()) + updated_products = [] + + for product in products: + changed = False + + if not product.icon: + product.icon = DEFAULT_ICON + changed = True + + normalized_stages = _merge_growth_stages(product, product.growth_stages) + if normalized_stages != (product.growth_stages or []): + product.growth_stages = normalized_stages + changed = True + + if not product.growth_stage and product.growth_stages: + product.growth_stage = product.growth_stages[0] + changed = True + + if changed: + updated_products.append(product) + + if updated_products: + Product.objects.bulk_update(updated_products, ["icon", "growth_stage", "growth_stages"]) + + return products + + +@transaction.atomic +def sync_plants_from_ai(): + try: + adapter_response = external_api_request("ai", AI_PLANTS_PATH, method="GET") + except ExternalAPIRequestError as exc: + raise PlantSyncError(str(exc)) from exc + + if adapter_response.status_code >= 400: + raise PlantSyncError(f"AI service returned status {adapter_response.status_code}.") + + products = [] + for item in _extract_plant_items(adapter_response.data): + if not isinstance(item, dict): + continue + + name = str(item.get("name") or "").strip() + if not name: + continue + + farm_type_name = str(item.get("farm_type") or DEFAULT_FARM_TYPE_NAME).strip() or DEFAULT_FARM_TYPE_NAME + farm_type, _ = FarmType.objects.get_or_create(name=farm_type_name) + + growth_profile = item.get("growth_profile") if isinstance(item.get("growth_profile"), dict) else {} + growth_stages = item.get("growth_stages") if isinstance(item.get("growth_stages"), list) else [] + normalized_growth_stages = [] + for stage in growth_stages: + normalized = _clean_stage_name(stage) + if normalized: + normalized_growth_stages.append(normalized) + + defaults = { + "description": str(item.get("description") or "").strip(), + "metadata": item.get("metadata") if isinstance(item.get("metadata"), dict) else {}, + "light": str(item.get("light") or "").strip(), + "watering": str(item.get("watering") or "").strip(), + "soil": str(item.get("soil") or "").strip(), + "temperature": str(item.get("temperature") or "").strip(), + "growth_stage": str(item.get("growth_stage") or "").strip(), + "growth_stages": normalized_growth_stages, + "planting_season": str(item.get("planting_season") or "").strip(), + "harvest_time": str(item.get("harvest_time") or "").strip(), + "spacing": str(item.get("spacing") or "").strip(), + "fertilizer": str(item.get("fertilizer") or "").strip(), + "icon": str(item.get("icon") or "").strip(), + "health_profile": item.get("health_profile") if isinstance(item.get("health_profile"), dict) else {}, + "irrigation_profile": item.get("irrigation_profile") if isinstance(item.get("irrigation_profile"), dict) else {}, + "growth_profile": growth_profile, + } + product, _ = Product.objects.update_or_create( + farm_type=farm_type, + name=name, + defaults=defaults, + ) + products.append(product) + + ensure_plant_defaults(products) + return products diff --git a/plants/tests.py b/plants/tests.py new file mode 100644 index 0000000..f742e0b --- /dev/null +++ b/plants/tests.py @@ -0,0 +1,99 @@ +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, Product +from .views import PlantListView, PlantNameListView, SelectedPlantListView + + +class PlantApiTests(TestCase): + def setUp(self): + self.factory = APIRequestFactory() + self.user = get_user_model().objects.create_user( + username="plant-user", + password="secret123", + email="plant@example.com", + phone_number="09123334455", + ) + self.farm_type = FarmType.objects.create(name="زراعی") + + @patch("plants.services.external_api_request") + def test_list_syncs_plants_from_ai_and_returns_full_payload(self, mock_external_api_request): + mock_external_api_request.return_value = AdapterResponse( + status_code=200, + data={ + "code": 200, + "msg": "success", + "data": [ + { + "name": "Tomato", + "light": "full sun", + "watering": "regular", + "soil": "loam", + "temperature": "20-30", + "growth_stage": "vegetative", + "planting_season": "spring", + "harvest_time": "70-90 days", + "spacing": "45-60 cm", + "fertilizer": "NPK", + "icon": "tomato", + "growth_profile": {"stage_thresholds": {"flowering": 300, "fruiting": 500}}, + } + ], + }, + ) + request = self.factory.get("/api/plants/") + force_authenticate(request, user=self.user) + + response = PlantListView.as_view()(request) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data["code"], 200) + self.assertEqual(response.data["data"][0]["name"], "Tomato") + self.assertEqual(response.data["data"][0]["icon"], "tomato") + self.assertIn("flowering", response.data["data"][0]["growth_stages"]) + mock_external_api_request.assert_called_once_with("ai", "/api/plants/", method="GET") + + @patch("plants.views.sync_plants_from_ai") + def test_names_endpoint_fills_default_icon_and_growth_stages(self, mock_sync_plants_from_ai): + mock_sync_plants_from_ai.return_value = [] + product = Product.objects.create( + farm_type=self.farm_type, + name="Pepper", + growth_profile={"stage_thresholds": {"fruiting": 450}}, + ) + request = self.factory.get("/api/plants/names/") + force_authenticate(request, user=self.user) + + response = PlantNameListView.as_view()(request) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data["data"][0]["icon"], "leaf") + self.assertEqual( + response.data["data"][0]["growth_stages"], + ["initial", "vegetative", "flowering", "fruiting", "maturity"], + ) + product.refresh_from_db() + self.assertEqual(product.icon, "leaf") + self.assertEqual(product.growth_stages, ["initial", "vegetative", "flowering", "fruiting", "maturity"]) + + @patch("plants.views.sync_plants_from_ai") + def test_selected_endpoint_returns_farmer_products(self, mock_sync_plants_from_ai): + mock_sync_plants_from_ai.return_value = [] + tomato = Product.objects.create(farm_type=self.farm_type, name="Tomato", icon="leaf", growth_stages=["vegetative"]) + pepper = Product.objects.create(farm_type=self.farm_type, name="Pepper", icon="leaf", growth_stages=["flowering"]) + farm = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name="farm-a") + farm.products.add(pepper) + + request = self.factory.get(f"/api/plants/selected/?farm_uuid={farm.farm_uuid}") + force_authenticate(request, user=self.user) + + response = SelectedPlantListView.as_view()(request) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.data["data"]), 1) + self.assertEqual(response.data["data"][0]["name"], "Pepper") + self.assertEqual(set(response.data["data"][0].keys()), {"name", "icon", "growth_stages"}) + self.assertNotEqual(response.data["data"][0]["name"], tomato.name) diff --git a/plants/urls.py b/plants/urls.py new file mode 100644 index 0000000..7b7ab27 --- /dev/null +++ b/plants/urls.py @@ -0,0 +1,10 @@ +from django.urls import path + +from .views import PlantDetailView, PlantListView, PlantNameListView, SelectedPlantListView + +urlpatterns = [ + path("names/", PlantNameListView.as_view(), name="plant-name-list"), + path("selected/", SelectedPlantListView.as_view(), name="selected-plant-list"), + path("/", PlantDetailView.as_view(), name="plant-detail"), + path("", PlantListView.as_view(), name="plant-list"), +] diff --git a/plants/views.py b/plants/views.py new file mode 100644 index 0000000..082e97c --- /dev/null +++ b/plants/views.py @@ -0,0 +1,96 @@ +from rest_framework import serializers, status +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response +from rest_framework.views import APIView +from drf_spectacular.types import OpenApiTypes +from drf_spectacular.utils import OpenApiParameter, extend_schema + +from config.swagger import code_response, farm_uuid_query_param +from farm_hub.models import FarmHub, Product +from .serializers import PlantNameSerializer, PlantSerializer +from .services import PlantSyncError, ensure_plant_defaults, sync_plants_from_ai + + +class PlantBaseView(APIView): + permission_classes = [IsAuthenticated] + + @staticmethod + def _sync_plants_if_possible(): + try: + sync_plants_from_ai() + except PlantSyncError: + return False + return True + + @staticmethod + def _get_farm(request, farm_uuid): + if not farm_uuid: + raise serializers.ValidationError({"farm_uuid": ["This field is required."]}) + try: + return FarmHub.objects.prefetch_related("products").get(farm_uuid=farm_uuid, owner=request.user) + except FarmHub.DoesNotExist as exc: + raise serializers.ValidationError({"farm_uuid": ["Farm not found."]}) from exc + + +class PlantListView(PlantBaseView): + @extend_schema( + tags=["Plants"], + responses={200: code_response("PlantListResponse", data=PlantSerializer(many=True))}, + ) + def get(self, request): + try: + sync_plants_from_ai() + except PlantSyncError as exc: + if not Product.objects.exists(): + return Response({"code": 503, "msg": str(exc)}, status=status.HTTP_503_SERVICE_UNAVAILABLE) + + products = ensure_plant_defaults(Product.objects.order_by("name")) + data = PlantSerializer(products, many=True).data + return Response({"code": 200, "msg": "success", "data": data}, status=status.HTTP_200_OK) + + +class PlantDetailView(PlantBaseView): + @extend_schema( + tags=["Plants"], + parameters=[ + OpenApiParameter(name="plant_id", type=OpenApiTypes.INT, location=OpenApiParameter.PATH), + ], + responses={200: code_response("PlantDetailResponse", data=PlantSerializer())}, + ) + def get(self, request, plant_id): + try: + product = Product.objects.get(id=plant_id) + except Product.DoesNotExist: + return Response({"code": 404, "msg": "Plant not found."}, status=status.HTTP_404_NOT_FOUND) + + ensure_plant_defaults([product]) + product.refresh_from_db() + data = PlantSerializer(product).data + return Response({"code": 200, "msg": "success", "data": data}, status=status.HTTP_200_OK) + + +class PlantNameListView(PlantBaseView): + @extend_schema( + tags=["Plants"], + responses={200: code_response("PlantNameListResponse", data=PlantNameSerializer(many=True))}, + ) + def get(self, request): + self._sync_plants_if_possible() + products = ensure_plant_defaults(Product.objects.order_by("name")) + data = PlantNameSerializer(products, many=True).data + return Response({"code": 200, "msg": "success", "data": data}, status=status.HTTP_200_OK) + + +class SelectedPlantListView(PlantBaseView): + @extend_schema( + tags=["Plants"], + parameters=[farm_uuid_query_param(required=True, description="UUID of the farm to read selected plants from.")], + responses={200: code_response("SelectedPlantListResponse", data=PlantNameSerializer(many=True))}, + ) + def get(self, request): + self._sync_plants_if_possible() + farm = self._get_farm(request, request.query_params.get("farm_uuid")) + ensure_plant_defaults(farm.products.all()) + products = farm.products.order_by("name") + data = PlantNameSerializer(products, many=True).data + return Response({"code": 200, "msg": "success", "data": data}, status=status.HTTP_200_OK)