Source code for django_owm.management.commands.create_location
"""Management command to create a new weather location."""fromdecimalimportROUND_HALF_UPfromdecimalimportDecimalfromdjango.appsimportappsfromdjango.core.management.baseimportBaseCommandfrom...app_settingsimportOWM_MODEL_MAPPINGS
[docs]classCommand(BaseCommand):"""Management command to create a new weather location."""help="Create a new weather location by entering a location name, latitude, and longitude."
[docs]defhandle(self,*args,**options):"""Handle the command."""WeatherLocationModel=apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation"))name=input("Enter location name: ")latitude=input("Enter latitude: ")longitude=input("Enter longitude: ")# Trim the latitude and longitude to 2 decimal placeslatitude=Decimal(latitude).quantize(Decimal("1e-2"),rounding=ROUND_HALF_UP)longitude=Decimal(longitude).quantize(Decimal("1e-2"),rounding=ROUND_HALF_UP)location=WeatherLocationModel.objects.create(name=name,latitude=latitude,longitude=longitude)self.stdout.write(self.style.SUCCESS(f"Successfully created location {location.name!r} with ID {location.id}."))