""" 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."))