Source code for django_owm.management.commands.delete_location
"""Management command to delete a weather location."""fromdjango.appsimportappsfromdjango.core.management.baseimportBaseCommandfromdjango.core.management.baseimportCommandErrorfrom...app_settingsimportOWM_MODEL_MAPPINGS
[docs]classCommand(BaseCommand):"""Management command to delete a weather location."""help="Delete a weather location."
[docs]defadd_arguments(self,parser):"""Add arguments to the command."""parser.add_argument("location_id",type=int,help="ID of the location to delete")
[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.")fromexcconfirmation=input(f"Are you sure you want to delete location {location.name!r}? (y/N): ")ifconfirmation.lower()=="y":location.delete()self.stdout.write(self.style.SUCCESS(f"Successfully deleted location {location.name!r}."))else:self.stdout.write("Deletion cancelled.")