This commit is contained in:
2026-04-28 04:11:09 +03:30
parent 5f0d94b8fd
commit 9b7d412445
17 changed files with 849 additions and 30 deletions
View File
+7
View File
@@ -0,0 +1,7 @@
from django.apps import AppConfig
class PlantsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "plants"
verbose_name = "Plants"
View File
+3
View File
@@ -0,0 +1,3 @@
from farm_hub.models import Product
__all__ = ["Product"]
+33
View File
@@ -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"]
+169
View File
@@ -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
+99
View File
@@ -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)
+10
View File
@@ -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("<int:plant_id>/", PlantDetailView.as_view(), name="plant-detail"),
path("", PlantListView.as_view(), name="plant-list"),
]
+96
View File
@@ -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)