UPDATE
This commit is contained in:
+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