UPDATE
This commit is contained in:
@@ -5,7 +5,14 @@ from django.apps import apps
|
||||
from farm_data.models import SensorData
|
||||
|
||||
class WaterStressService:
|
||||
def get_water_stress(self, *, farm_uuid: str, plant_name: str | None = None) -> dict[str, Any]:
|
||||
def get_water_stress(
|
||||
self,
|
||||
*,
|
||||
farm_uuid: str,
|
||||
plant_name: str | None = None,
|
||||
irrigation_recommendation: dict | None = None,
|
||||
fertilization_recommendation: dict | None = None,
|
||||
) -> dict[str, Any]:
|
||||
sensor = SensorData.objects.filter(farm_uuid=farm_uuid).first()
|
||||
if sensor is None:
|
||||
raise ValueError("Farm not found.")
|
||||
@@ -14,6 +21,8 @@ class WaterStressService:
|
||||
return simulation_service.get_water_stress(
|
||||
farm_uuid=str(sensor.farm_uuid),
|
||||
plant_name=plant_name,
|
||||
irrigation_recommendation=irrigation_recommendation,
|
||||
fertilization_recommendation=fertilization_recommendation,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise RuntimeError(
|
||||
|
||||
@@ -66,6 +66,9 @@ class IrrigationRecommendResponseSerializer(serializers.Serializer):
|
||||
class WaterStressRequestSerializer(serializers.Serializer):
|
||||
farm_uuid = serializers.CharField(required=False, help_text="شناسه یکتای مزرعه")
|
||||
sensor_uuid = serializers.CharField(required=False, help_text="نام قدیمی برای farm_uuid")
|
||||
plant_name = serializers.CharField(required=False, allow_blank=True, help_text="نام گیاه")
|
||||
irrigation_recommendation = serializers.JSONField(required=False)
|
||||
fertilization_recommendation = serializers.JSONField(required=False)
|
||||
|
||||
def validate(self, attrs):
|
||||
farm_uuid = attrs.get("farm_uuid") or attrs.get("sensor_uuid")
|
||||
|
||||
@@ -30,7 +30,13 @@ class WaterStressApiTests(TestCase):
|
||||
|
||||
response = self.client.post(
|
||||
"/water-stress/",
|
||||
data={"farm_uuid": "550e8400-e29b-41d4-a716-446655440000"},
|
||||
data={
|
||||
"farm_uuid": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"irrigation_recommendation": {"events": [{"date": "2026-04-25", "amount": 2.5}]},
|
||||
"fertilization_recommendation": {
|
||||
"events": [{"date": "2026-04-20", "N_amount": 45, "N_recovery": 0.7}]
|
||||
},
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
|
||||
+13
-2
@@ -354,7 +354,13 @@ class WaterStressView(APIView):
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست water stress",
|
||||
value={"farm_uuid": "11111111-1111-1111-1111-111111111111"},
|
||||
value={
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
"irrigation_recommendation": {"events": [{"date": "2026-04-25", "amount": 2.5}]},
|
||||
"fertilization_recommendation": {
|
||||
"events": [{"date": "2026-04-20", "N_amount": 45, "N_recovery": 0.7}]
|
||||
},
|
||||
},
|
||||
request_only=True,
|
||||
)
|
||||
],
|
||||
@@ -368,7 +374,12 @@ class WaterStressView(APIView):
|
||||
)
|
||||
service = apps.get_app_config("irrigation").get_water_stress_service()
|
||||
try:
|
||||
data = service.get_water_stress(farm_uuid=serializer.validated_data["farm_uuid"])
|
||||
data = service.get_water_stress(
|
||||
farm_uuid=serializer.validated_data["farm_uuid"],
|
||||
plant_name=serializer.validated_data.get("plant_name"),
|
||||
irrigation_recommendation=serializer.validated_data.get("irrigation_recommendation"),
|
||||
fertilization_recommendation=serializer.validated_data.get("fertilization_recommendation"),
|
||||
)
|
||||
except ValueError as exc:
|
||||
return Response(
|
||||
{"code": 404, "msg": str(exc), "data": None},
|
||||
|
||||
Reference in New Issue
Block a user