UPDATE
This commit is contained in:
+30
-5
@@ -42,6 +42,35 @@ WaterStressEnvelopeSerializer = build_envelope_serializer(
|
||||
WaterStressResponseSerializer,
|
||||
)
|
||||
|
||||
IRRIGATION_RECOMMENDATION_INTERNAL_KEYS = {
|
||||
"raw_response",
|
||||
"water_balance",
|
||||
"simulation_optimizer",
|
||||
"selected_irrigation_method",
|
||||
}
|
||||
|
||||
|
||||
def _prepare_irrigation_recommendation_response(value):
|
||||
if isinstance(value, dict):
|
||||
cleaned = {}
|
||||
for key, item in value.items():
|
||||
if key in IRRIGATION_RECOMMENDATION_INTERNAL_KEYS or item is None:
|
||||
continue
|
||||
normalized = _prepare_irrigation_recommendation_response(item)
|
||||
if normalized is not None:
|
||||
cleaned[key] = normalized
|
||||
return cleaned
|
||||
|
||||
if isinstance(value, list):
|
||||
cleaned_items = []
|
||||
for item in value:
|
||||
normalized = _prepare_irrigation_recommendation_response(item)
|
||||
if normalized is not None:
|
||||
cleaned_items.append(normalized)
|
||||
return cleaned_items
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class IrrigationMethodListCreateView(APIView):
|
||||
"""لیست تمام روشهای آبیاری و ایجاد روش جدید."""
|
||||
@@ -171,15 +200,12 @@ class IrrigationRecommendView(APIView):
|
||||
plant_name = validated.get("plant_name")
|
||||
growth_stage = validated.get("growth_stage")
|
||||
irrigation_method_name = validated.get("irrigation_method_name")
|
||||
query = validated.get("query")
|
||||
|
||||
try:
|
||||
result = get_irrigation_recommendation(
|
||||
farm_uuid=farm_uuid,
|
||||
plant_name=plant_name,
|
||||
growth_stage=growth_stage,
|
||||
irrigation_method_name=irrigation_method_name,
|
||||
query=query,
|
||||
)
|
||||
except Exception as exc:
|
||||
return Response(
|
||||
@@ -187,8 +213,7 @@ class IrrigationRecommendView(APIView):
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
# Public API exposes only the final farmer-facing recommendation object.
|
||||
final_result = {"sections": result.get("sections", [])}
|
||||
final_result = _prepare_irrigation_recommendation_response(result) or {}
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": final_result},
|
||||
status=status.HTTP_200_OK,
|
||||
|
||||
Reference in New Issue
Block a user