This commit is contained in:
2026-05-10 02:02:48 +03:30
parent cead7dafe2
commit 2d1f7da89e
30 changed files with 1195 additions and 320 deletions
+26 -2
View File
@@ -9,6 +9,7 @@ from django.conf import settings
from django.apps import apps
from django.db import transaction
from django.utils.dateparse import parse_datetime
from django.utils import timezone
import requests
@@ -576,10 +577,33 @@ def resolve_weather_for_location(location: SoilLocation) -> WeatherForecast | No
def ensure_location_and_weather_data(location: SoilLocation) -> tuple[SoilLocation, WeatherForecast | None]:
"""
در فاز فعلی برای location_data و بلوک‌ها هیچ ریکوئست خارجی زده نمی‌شود
و فقط دادههای محلی موجود برگردانده می‌شوند.
forecast آب‌وهوا را در صورت نبود/قدیمی بودن refresh می‌کند تا
سرویس‌های downstream بهجای دیتای seed/mock از داده provider فعال استفاده کنند.
"""
weather_forecast = resolve_weather_for_location(location)
needs_refresh = weather_forecast is None
if weather_forecast is not None:
today = timezone.localdate()
has_upcoming_forecast = WeatherForecast.objects.filter(
location=location,
forecast_date__gte=today,
).exists()
fetched_at = getattr(weather_forecast, "fetched_at", None)
is_stale = fetched_at is None or (timezone.now() - fetched_at).total_seconds() >= 3 * 60 * 60
needs_refresh = (not has_upcoming_forecast) or is_stale
if needs_refresh:
try:
weather_result = apps.get_app_config("weather").update_weather_for_location(location)
except Exception as exc:
raise ExternalDataSyncError(f"Weather sync failed: {exc}") from exc
if weather_result.get("status") == "error":
raise ExternalDataSyncError(weather_result.get("error") or "Weather sync failed.")
weather_forecast = resolve_weather_for_location(location)
return location, weather_forecast