Source code for django_owm.management.commands.manual_weather_fetch
"""Management command to manually fetch weather data for a specific location."""fromdecimalimportDecimalfromdjango.appsimportappsfromdjango.core.management.baseimportBaseCommandfromdjango.core.management.baseimportCommandErrorfrom...app_settingsimportOWM_MODEL_MAPPINGSfrom...app_settingsimportOWM_USE_UUIDfrom...tasksimportfetch_weather
[docs]classCommand(BaseCommand):"""Management command to manually fetch weather data for a specific location."""help="Manually fetch weather data for a specific location."
[docs]defadd_arguments(self,parser):"""Add arguments to the command."""ifOWM_USE_UUID:parser.add_argument("location_id",type=Decimal,help="ID of the location to fetch weather for")else:parser.add_argument("location_id",type=int,help="ID of the location to fetch weather for")
[docs]defhandle(self,*args,**options):"""Handle the command."""location_id=options["location_id"]WeatherLocationModel=apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation"))try:location=WeatherLocationModel.objects.get(pk=location_id)exceptWeatherLocationModel.DoesNotExistasexc:self.stderr.write(self.style.ERROR(f"Location with ID {location_id} does not exist."))raiseCommandError(f"Location with ID {location_id} does not exist.")fromexc# Fetch weather data for the specific locationfetch_weather(location_ids=[location_id])self.stdout.write(self.style.SUCCESS(f"Successfully fetched weather data for location {location.name!r}."))