2026-04-11 03:54:15 +03:30
|
|
|
from copy import deepcopy
|
|
|
|
|
|
2026-05-05 21:01:58 +03:30
|
|
|
from .defaults import FERTILIZATION_DASHBOARD_TEMPLATE
|
2026-05-02 14:36:26 +03:30
|
|
|
from .models import FertilizationPlan, FertilizationRecommendationRequest
|
2026-04-11 03:54:15 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_result(response_payload):
|
|
|
|
|
if not isinstance(response_payload, dict):
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
data = response_payload.get("data")
|
|
|
|
|
if isinstance(data, dict) and isinstance(data.get("result"), dict):
|
|
|
|
|
return data["result"]
|
|
|
|
|
|
|
|
|
|
result = response_payload.get("result")
|
|
|
|
|
if isinstance(result, dict):
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_latest_result(farm):
|
|
|
|
|
if farm is None:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
for request in FertilizationRecommendationRequest.objects.filter(farm=farm):
|
|
|
|
|
result = _extract_result(request.response_payload)
|
|
|
|
|
if result:
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
2026-05-02 14:36:26 +03:30
|
|
|
def get_active_plan_payload(farm):
|
|
|
|
|
if farm is None:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
plan = (
|
|
|
|
|
FertilizationPlan.objects.filter(farm=farm, is_active=True, is_deleted=False)
|
|
|
|
|
.order_by("-created_at", "-id")
|
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
if plan is None or not isinstance(plan.plan_payload, dict):
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
return deepcopy(plan.plan_payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_active_plan_context(farm):
|
|
|
|
|
plan_payload = get_active_plan_payload(farm)
|
|
|
|
|
if not plan_payload:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
context = {"plan_payload": plan_payload}
|
|
|
|
|
|
|
|
|
|
primary_recommendation = plan_payload.get("primary_recommendation")
|
|
|
|
|
if isinstance(primary_recommendation, dict) and primary_recommendation:
|
|
|
|
|
context["primary_recommendation"] = deepcopy(primary_recommendation)
|
|
|
|
|
|
|
|
|
|
nutrient_analysis = plan_payload.get("nutrient_analysis")
|
|
|
|
|
if isinstance(nutrient_analysis, dict) and nutrient_analysis:
|
|
|
|
|
context["nutrient_analysis"] = deepcopy(nutrient_analysis)
|
|
|
|
|
|
|
|
|
|
application_guide = plan_payload.get("application_guide")
|
|
|
|
|
if isinstance(application_guide, dict) and application_guide:
|
|
|
|
|
context["application_guide"] = deepcopy(application_guide)
|
|
|
|
|
|
|
|
|
|
alternative_recommendations = plan_payload.get("alternative_recommendations")
|
|
|
|
|
if isinstance(alternative_recommendations, list) and alternative_recommendations:
|
|
|
|
|
context["alternative_recommendations"] = deepcopy(alternative_recommendations)
|
|
|
|
|
|
|
|
|
|
sections = plan_payload.get("sections")
|
|
|
|
|
if isinstance(sections, list) and sections:
|
|
|
|
|
context["sections"] = deepcopy(sections)
|
|
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 03:54:15 +03:30
|
|
|
def get_fertilization_dashboard_recommendation(farm=None):
|
2026-05-05 21:01:58 +03:30
|
|
|
default_item = deepcopy(FERTILIZATION_DASHBOARD_TEMPLATE)
|
2026-04-11 03:54:15 +03:30
|
|
|
result = _get_latest_result(farm)
|
2026-05-05 21:01:58 +03:30
|
|
|
plan = result.get("plan") or {}
|
|
|
|
|
if not isinstance(plan, dict) or not plan:
|
|
|
|
|
return default_item
|
2026-04-11 03:54:15 +03:30
|
|
|
|
|
|
|
|
npk_ratio = plan.get("npkRatio") or "20-20-20 (NPK)"
|
|
|
|
|
amount = plan.get("amountPerHectare")
|
|
|
|
|
method = plan.get("applicationMethod")
|
|
|
|
|
interval = plan.get("applicationInterval")
|
|
|
|
|
|
|
|
|
|
subtitle_parts = [part for part in [amount, method, interval] if part]
|
|
|
|
|
|
|
|
|
|
default_item["title"] = f"کود: {npk_ratio}"
|
|
|
|
|
if subtitle_parts:
|
|
|
|
|
default_item["subtitle"] = "، ".join(subtitle_parts)
|
2026-05-05 21:01:58 +03:30
|
|
|
default_item["status"] = "success"
|
|
|
|
|
default_item["source"] = "db"
|
|
|
|
|
default_item["warnings"] = []
|
2026-04-11 03:54:15 +03:30
|
|
|
|
|
|
|
|
return default_item
|