This commit is contained in:
2026-05-11 03:27:21 +03:30
parent cf7cbb937c
commit d0e68a1a56
854 changed files with 102985 additions and 76 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"
+3
View File
@@ -0,0 +1,3 @@
from farm_hub.models import Product
__all__ = ["Product"]
+38
View File
@@ -0,0 +1,38 @@
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",
"description",
"metadata",
"light",
"watering",
"soil",
"temperature",
"growth_stage",
"growth_stages",
"icon",
"planting_season",
"harvest_time",
"spacing",
"fertilizer",
"health_profile",
"irrigation_profile",
"growth_profile",
"created_at",
"updated_at",
]
class PlantNameSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ["name", "icon", "growth_stages"]
+152
View File
@@ -0,0 +1,152 @@
from django.db import transaction
from django.conf import settings
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_FARM_DATA_PLANT_SYNC_PATH = "/api/farm-data/plants/sync/"
class PlantSyncError(Exception):
pass
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
def serialize_products_for_ai(products=None):
products = list(products if products is not None else Product.objects.select_related("farm_type").all().order_by("name"))
ensure_plant_defaults(products)
payload = []
for product in products:
payload.append(
{
"id": product.id,
"name": product.name,
"slug": "",
"icon": product.icon,
"description": product.description,
"metadata": product.metadata if isinstance(product.metadata, dict) else {},
"light": product.light,
"watering": product.watering,
"soil": product.soil,
"temperature": product.temperature,
"growth_stage": product.growth_stage,
"growth_stages": product.growth_stages or [],
"planting_season": product.planting_season,
"harvest_time": product.harvest_time,
"spacing": product.spacing,
"fertilizer": product.fertilizer,
"health_profile": product.health_profile if isinstance(product.health_profile, dict) else {},
"irrigation_profile": product.irrigation_profile if isinstance(product.irrigation_profile, dict) else {},
"growth_profile": product.growth_profile if isinstance(product.growth_profile, dict) else {},
"is_active": True,
"updated_at": product.updated_at.isoformat() if product.updated_at else None,
"farm_type": product.farm_type.name if product.farm_type_id else DEFAULT_FARM_TYPE_NAME,
}
)
return payload
def push_plants_to_ai(products=None):
api_key = getattr(settings, "FARM_DATA_API_KEY", "")
if not api_key:
raise PlantSyncError("FARM_DATA_API_KEY is not configured.")
payload = serialize_products_for_ai(products)
try:
adapter_response = external_api_request(
"ai",
AI_FARM_DATA_PLANT_SYNC_PATH,
method="POST",
payload=payload,
headers={
"Accept": "application/json",
"Content-Type": "application/json",
"X-API-Key": api_key,
"Authorization": f"Api-Key {api_key}",
},
)
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}.")
return payload
+121
View File
@@ -0,0 +1,121 @@
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 farm_hub.models import FarmHub, FarmType, Product
from .services import PlantSyncError
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.views.push_plants_to_ai")
def test_list_returns_backend_catalog_with_sync_metadata(self, mock_push_plants_to_ai):
mock_push_plants_to_ai.return_value = []
Product.objects.create(
farm_type=self.farm_type,
name="Tomato",
icon="tomato",
growth_stage="vegetative",
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"])
self.assertEqual(response.data["meta"]["flow_type"], "backend_owned_data_with_ai_enrichment")
self.assertEqual(response.data["meta"]["source_type"], "db")
self.assertEqual(response.data["meta"]["sync_status"], "synced")
mock_push_plants_to_ai.assert_called_once()
@patch("plants.views.push_plants_to_ai")
def test_names_endpoint_fills_default_icon_and_growth_stages(self, mock_push_plants_to_ai):
mock_push_plants_to_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"])
self.assertEqual(response.data["meta"]["flow_type"], "backend_owned_data")
self.assertEqual(response.data["meta"]["source_type"], "db")
self.assertEqual(response.data["meta"]["sync_status"], "synced")
@patch("plants.views.push_plants_to_ai")
def test_selected_endpoint_returns_farmer_products(self, mock_push_plants_to_ai):
mock_push_plants_to_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)
self.assertEqual(response.data["meta"]["ownership"], "backend")
self.assertEqual(response.data["meta"]["sync_status"], "synced")
@patch("plants.views.push_plants_to_ai")
def test_list_exposes_backend_ownership_even_when_ai_sync_fails(self, mock_push_plants_to_ai):
mock_push_plants_to_ai.side_effect = PlantSyncError("sync failed")
Product.objects.create(farm_type=self.farm_type, name="Tomato", icon="leaf", growth_stages=["vegetative"])
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["meta"]["flow_type"], "backend_owned_data_with_ai_enrichment")
self.assertEqual(response.data["meta"]["sync_status"], "failed")
def test_selected_endpoint_reads_seeded_backend_products_without_runtime_mock_data(self):
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="seeded-farm")
farm.products.add(tomato, pepper)
request = self.factory.get(f"/api/plants/selected/?farm_uuid={farm.farm_uuid}")
force_authenticate(request, user=self.user)
with patch("plants.views.push_plants_to_ai", return_value=[]):
response = SelectedPlantListView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertCountEqual([item["name"] for item in response.data["data"]], ["Tomato", "Pepper"])
self.assertEqual(response.data["meta"]["source_type"], "db")
+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"),
]
+151
View File
@@ -0,0 +1,151 @@
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.integration_contract import build_integration_meta
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, push_plants_to_ai
class PlantBaseView(APIView):
permission_classes = [IsAuthenticated]
@staticmethod
def _attempt_ai_catalog_sync():
try:
push_plants_to_ai()
except PlantSyncError:
return False, "failed"
return True, "synced"
@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):
products = ensure_plant_defaults(Product.objects.order_by("name"))
sync_attempted = True
sync_status = "synced"
try:
push_plants_to_ai(products)
except PlantSyncError as exc:
sync_status = "failed"
if not products:
return Response({"code": 503, "msg": str(exc)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
data = PlantSerializer(products, many=True).data
return Response(
{
"code": 200,
"msg": "success",
"data": data,
"meta": build_integration_meta(
flow_type="backend_owned_data_with_ai_enrichment",
source_type="db",
source_service="backend_plants",
ownership="backend",
live=False,
cached=False,
sync_attempted=sync_attempted,
sync_status=sync_status,
notes=["Backend plant catalog is canonical; AI receives sync snapshots only."],
),
},
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):
sync_attempted, sync_status = self._attempt_ai_catalog_sync()
products = ensure_plant_defaults(Product.objects.order_by("name"))
data = PlantNameSerializer(products, many=True).data
return Response(
{
"code": 200,
"msg": "success",
"data": data,
"meta": build_integration_meta(
flow_type="backend_owned_data",
source_type="db",
source_service="backend_plants",
ownership="backend",
live=False,
cached=False,
sync_attempted=sync_attempted,
sync_status=sync_status,
),
},
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):
sync_attempted, sync_status = self._attempt_ai_catalog_sync()
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,
"meta": build_integration_meta(
flow_type="backend_owned_data",
source_type="db",
source_service="backend_plants",
ownership="backend",
live=False,
cached=False,
sync_attempted=sync_attempted,
sync_status=sync_status,
),
},
status=status.HTTP_200_OK,
)