This commit is contained in:
2026-05-11 03:27:21 +03:30
parent cf7cbb937c
commit d0e68a1a56
854 changed files with 102985 additions and 76 deletions
@@ -0,0 +1 @@
@@ -0,0 +1 @@
@@ -0,0 +1,89 @@
"""
Management command to seed fixed weather forecasts for the demo farm location.
Run: python manage.py seed_weather_data
"""
from datetime import timedelta
from django.core.management.base import BaseCommand
from django.utils import timezone
from location_data.models import SoilLocation
from weather.models import WeatherForecast
DEMO_LATITUDE = "50.000000"
DEMO_LONGITUDE = "50.000000"
DEMO_FORECASTS = [
{
"day_offset": 0,
"temperature_min": 14.0,
"temperature_max": 24.5,
"temperature_mean": 19.3,
"precipitation": 0.0,
"precipitation_probability": 5.0,
"humidity_mean": 48.0,
"wind_speed_max": 12.0,
"et0": 4.2,
"weather_code": 1,
},
{
"day_offset": 1,
"temperature_min": 13.5,
"temperature_max": 22.0,
"temperature_mean": 17.8,
"precipitation": 2.4,
"precipitation_probability": 60.0,
"humidity_mean": 61.0,
"wind_speed_max": 18.0,
"et0": 3.7,
"weather_code": 61,
},
{
"day_offset": 2,
"temperature_min": 12.8,
"temperature_max": 20.5,
"temperature_mean": 16.4,
"precipitation": 4.8,
"precipitation_probability": 78.0,
"humidity_mean": 68.0,
"wind_speed_max": 20.0,
"et0": 3.1,
"weather_code": 63,
},
]
class Command(BaseCommand):
help = "Seed weather forecast rows for the fixed 50.00, 50.00 demo location."
def handle(self, *args, **options):
location, _ = SoilLocation.objects.get_or_create(
latitude=DEMO_LATITUDE,
longitude=DEMO_LONGITUDE,
)
today = timezone.now().date()
self.stdout.write(
self.style.SUCCESS(
f"Using SoilLocation id={location.id} at ({location.latitude}, {location.longitude})"
)
)
for item in DEMO_FORECASTS:
forecast_date = today + timedelta(days=item["day_offset"])
defaults = {
key: value
for key, value in item.items()
if key != "day_offset"
}
_, created = WeatherForecast.objects.update_or_create(
location=location,
forecast_date=forecast_date,
defaults=defaults,
)
status_text = "Created" if created else "Updated"
self.stdout.write(
self.style.SUCCESS(f" {status_text} WeatherForecast for {forecast_date}")
)
self.stdout.write(self.style.SUCCESS("\nDone seeding weather_data demo records."))
@@ -0,0 +1,42 @@
"""
Management command to seed weather parameters.
Run: python manage.py seed_weather_parameters
"""
from django.core.management.base import BaseCommand
from weather.models import WeatherParameter
INITIAL_PARAMETERS = [
("temperature_min", "حداقل دمای هوا", "°C"),
("temperature_max", "حداکثر دمای هوا", "°C"),
("temperature_mean", "میانگین دمای هوا", "°C"),
("precipitation", "مجموع بارش", "mm"),
("precipitation_probability", "احتمال بارش", "%"),
("humidity_mean", "میانگین رطوبت نسبی", "%"),
("wind_speed_max", "حداکثر سرعت باد", "km/h"),
("et0", "تبخیر-تعرق مرجع (ET₀)", "mm/day"),
("weather_code", "کد وضعیت آب‌وهوا (WMO)", ""),
]
class Command(BaseCommand):
help = "Seed weather parameters (temperature, precipitation, ET0, etc.)"
def handle(self, *args, **options):
created_count = 0
for code, name_fa, unit in INITIAL_PARAMETERS:
_, created = WeatherParameter.objects.get_or_create(
code=code,
defaults={"name_fa": name_fa, "unit": unit},
)
if created:
created_count += 1
self.stdout.write(
self.style.SUCCESS(f" Created: {code} ({name_fa})")
)
self.stdout.write(
self.style.SUCCESS(
f"\nDone. Created {created_count} new weather parameters."
)
)