UPDATE
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
# Plant Names API
|
||||
|
||||
این API فقط لیست نام گیاهها را به همراه آیکون و مراحل رشد برمیگرداند.
|
||||
|
||||
## Endpoint
|
||||
|
||||
- `GET /api/plants/names/`
|
||||
|
||||
## کاربرد
|
||||
|
||||
- گرفتن لیست سبک برای dropdown یا selector فرانت
|
||||
- نمایش نام گیاه
|
||||
- نمایش `icon`
|
||||
- نمایش مراحل رشد هر گیاه
|
||||
|
||||
## رفتار API
|
||||
|
||||
- فقط فیلدهای `name`، `icon` و `growth_stages` را برمیگرداند
|
||||
- اگر `growth_stage` برای یک گیاه خالی باشد، API به صورت خودکار این مراحل پیشفرض را اضافه و در دیتابیس ذخیره میکند:
|
||||
- `initial`
|
||||
- `vegetative`
|
||||
- `flowering`
|
||||
- `fruiting`
|
||||
- `maturity`
|
||||
- اگر `icon` خالی باشد، مقدار پیشفرض `leaf` ذخیره و برگردانده میشود
|
||||
- اگر در `growth_profile.stage_thresholds` مرحلهای وجود داشته باشد، آن مرحله هم در خروجی `growth_stages` لحاظ میشود
|
||||
|
||||
## نمونه درخواست
|
||||
|
||||
```bash
|
||||
curl -X GET http://localhost:8000/api/plants/names/
|
||||
```
|
||||
|
||||
## نمونه پاسخ
|
||||
|
||||
```json
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": [
|
||||
{
|
||||
"name": "Tomato",
|
||||
"icon": "leaf",
|
||||
"growth_stages": [
|
||||
"vegetative",
|
||||
"flowering",
|
||||
"fruiting"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Pepper",
|
||||
"icon": "leaf",
|
||||
"growth_stages": [
|
||||
"initial",
|
||||
"vegetative",
|
||||
"flowering",
|
||||
"fruiting",
|
||||
"maturity"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## فیلدهای خروجی
|
||||
|
||||
- `name`: نام گیاه
|
||||
- `icon`: آیکون گیاه برای فرانت
|
||||
- `growth_stages`: آرایهای از مراحل رشد گیاه
|
||||
|
||||
## نکته برای فرانت
|
||||
|
||||
- این endpoint برای لیست سبک طراحی شده و مناسب صفحههای انتخاب گیاه است
|
||||
- اگر جزئیات کامل گیاه لازم دارید، از `GET /api/plants/` یا `GET /api/plants/{id}/` استفاده کنید
|
||||
@@ -0,0 +1,20 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("plant", "0005_plant_growth_stage"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="plant",
|
||||
name="icon",
|
||||
field=models.CharField(
|
||||
blank=True,
|
||||
default="leaf",
|
||||
help_text="آیکون گیاه برای نمایش در فرانت",
|
||||
max_length=255,
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -12,6 +12,12 @@ class Plant(models.Model):
|
||||
db_index=True,
|
||||
help_text="نام گیاه",
|
||||
)
|
||||
icon = models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
default="leaf",
|
||||
help_text="آیکون گیاه برای نمایش در فرانت",
|
||||
)
|
||||
light = models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
|
||||
@@ -3,6 +3,37 @@ from rest_framework import serializers
|
||||
from .models import Plant
|
||||
|
||||
|
||||
DEFAULT_PLANT_GROWTH_STAGES = [
|
||||
"initial",
|
||||
"vegetative",
|
||||
"flowering",
|
||||
"fruiting",
|
||||
"maturity",
|
||||
]
|
||||
|
||||
|
||||
def normalize_growth_stage_values(plant: Plant) -> list[str]:
|
||||
stages: list[str] = []
|
||||
|
||||
raw_stage = (plant.growth_stage or "").replace("،", ",")
|
||||
for part in raw_stage.split(","):
|
||||
value = part.strip()
|
||||
if value and value not in stages:
|
||||
stages.append(value)
|
||||
|
||||
stage_thresholds = plant.growth_profile.get("stage_thresholds", {})
|
||||
if isinstance(stage_thresholds, dict):
|
||||
for stage_name in stage_thresholds.keys():
|
||||
value = str(stage_name).strip()
|
||||
if value and value not in stages:
|
||||
stages.append(value)
|
||||
|
||||
if not stages:
|
||||
stages = list(DEFAULT_PLANT_GROWTH_STAGES)
|
||||
|
||||
return stages
|
||||
|
||||
|
||||
class PlantSerializer(serializers.ModelSerializer):
|
||||
"""سریالایزر خروجی / ورودی برای Plant."""
|
||||
|
||||
@@ -11,6 +42,7 @@ class PlantSerializer(serializers.ModelSerializer):
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"icon",
|
||||
"light",
|
||||
"watering",
|
||||
"soil",
|
||||
@@ -24,3 +56,9 @@ class PlantSerializer(serializers.ModelSerializer):
|
||||
"updated_at",
|
||||
]
|
||||
read_only_fields = ["id", "created_at", "updated_at"]
|
||||
|
||||
|
||||
class PlantNameStageSerializer(serializers.Serializer):
|
||||
name = serializers.CharField()
|
||||
icon = serializers.CharField()
|
||||
growth_stages = serializers.ListField(child=serializers.CharField())
|
||||
|
||||
+7
-1
@@ -1,9 +1,15 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import PlantDetailView, PlantFetchInfoView, PlantListCreateView
|
||||
from .views import (
|
||||
PlantDetailView,
|
||||
PlantFetchInfoView,
|
||||
PlantListCreateView,
|
||||
PlantNameStageListView,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path("", PlantListCreateView.as_view(), name="plant-list-create"),
|
||||
path("names/", PlantNameStageListView.as_view(), name="plant-name-stage-list"),
|
||||
path("<int:pk>/", PlantDetailView.as_view(), name="plant-detail"),
|
||||
path("fetch-info/", PlantFetchInfoView.as_view(), name="plant-fetch-info"),
|
||||
]
|
||||
|
||||
+59
-1
@@ -11,7 +11,11 @@ from rest_framework.views import APIView
|
||||
|
||||
from config.openapi import build_envelope_serializer, build_response
|
||||
from .models import Plant
|
||||
from .serializers import PlantSerializer
|
||||
from .serializers import (
|
||||
PlantNameStageSerializer,
|
||||
PlantSerializer,
|
||||
normalize_growth_stage_values,
|
||||
)
|
||||
from .services import fetch_plant_info_from_api
|
||||
|
||||
|
||||
@@ -33,6 +37,11 @@ PlantFetchInfoResponseSerializer = build_envelope_serializer(
|
||||
"PlantFetchInfoResponseSerializer",
|
||||
PlantSerializer,
|
||||
)
|
||||
PlantNameStageListResponseSerializer = build_envelope_serializer(
|
||||
"PlantNameStageListResponseSerializer",
|
||||
PlantNameStageSerializer,
|
||||
many=True,
|
||||
)
|
||||
|
||||
|
||||
class PlantListCreateView(APIView):
|
||||
@@ -105,6 +114,55 @@ class PlantListCreateView(APIView):
|
||||
)
|
||||
|
||||
|
||||
class PlantNameStageListView(APIView):
|
||||
"""لیست سبک از نام گیاه، آیکون و مراحل رشد."""
|
||||
|
||||
@extend_schema(
|
||||
tags=["Plant"],
|
||||
summary="لیست نام گیاهان با مراحل رشد",
|
||||
description=(
|
||||
"فقط نام گیاه، آیکون و مراحل رشد را برمیگرداند. "
|
||||
"اگر برای گیاهی مرحله رشد ثبت نشده باشد، مراحل پیشفرض به آن اضافه و ذخیره میشود."
|
||||
),
|
||||
responses={
|
||||
200: build_response(
|
||||
PlantNameStageListResponseSerializer,
|
||||
"لیست نام گیاهان به همراه مراحل رشد و آیکون.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def get(self, request):
|
||||
payload = []
|
||||
for plant in Plant.objects.all():
|
||||
growth_stages = normalize_growth_stage_values(plant)
|
||||
serialized_stages = ", ".join(growth_stages)
|
||||
update_fields: list[str] = []
|
||||
|
||||
if plant.growth_stage != serialized_stages:
|
||||
plant.growth_stage = serialized_stages
|
||||
update_fields.append("growth_stage")
|
||||
if not plant.icon:
|
||||
plant.icon = "leaf"
|
||||
update_fields.append("icon")
|
||||
if update_fields:
|
||||
update_fields.append("updated_at")
|
||||
plant.save(update_fields=update_fields)
|
||||
|
||||
payload.append(
|
||||
{
|
||||
"name": plant.name,
|
||||
"icon": plant.icon,
|
||||
"growth_stages": growth_stages,
|
||||
}
|
||||
)
|
||||
|
||||
serializer = PlantNameStageSerializer(payload, many=True)
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": serializer.data},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class PlantDetailView(APIView):
|
||||
"""دریافت، ویرایش و حذف یک گیاه."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user