Files
Ai/plant/serializers.py
T

65 lines
1.6 KiB
Python
Raw Normal View History

2026-03-19 22:54:29 +03:30
from rest_framework import serializers
from .models import Plant
2026-04-28 04:11:49 +03:30
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
2026-03-19 22:54:29 +03:30
class PlantSerializer(serializers.ModelSerializer):
"""سریالایزر خروجی / ورودی برای Plant."""
class Meta:
model = Plant
fields = [
"id",
"name",
2026-04-28 04:11:49 +03:30
"icon",
2026-03-19 22:54:29 +03:30
"light",
"watering",
"soil",
"temperature",
2026-04-24 02:50:27 +03:30
"growth_stage",
2026-03-19 22:54:29 +03:30
"planting_season",
"harvest_time",
"spacing",
"fertilizer",
"created_at",
"updated_at",
]
read_only_fields = ["id", "created_at", "updated_at"]
2026-04-28 04:11:49 +03:30
class PlantNameStageSerializer(serializers.Serializer):
name = serializers.CharField()
icon = serializers.CharField()
growth_stages = serializers.ListField(child=serializers.CharField())