From c0a89c266a5302279907527ffc5a0dac0f58b367 Mon Sep 17 00:00:00 2001 From: moanos Date: Fri, 31 May 2024 15:03:33 +0200 Subject: [PATCH] feat: Add command to clean locations --- .../management/commands/clean_locations.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/fellchensammlung/management/commands/clean_locations.py diff --git a/src/fellchensammlung/management/commands/clean_locations.py b/src/fellchensammlung/management/commands/clean_locations.py new file mode 100644 index 0000000..21d9546 --- /dev/null +++ b/src/fellchensammlung/management/commands/clean_locations.py @@ -0,0 +1,32 @@ +from django.core.management import BaseCommand +from fellchensammlung.models import AdoptionNotice, Location + + +class Command(BaseCommand): + help = 'Clean up empty locations by re-querying them' + + def add_arguments(self, parser): + # Named (optional) arguments + parser.add_argument( + "--all", + action="store_true", + help="Re-query all locations, not only where they are empty", + ) + + def handle(self, *args, **options): + adoption_notices_without_location = AdoptionNotice.objects.filter(location__isnull=True) + num_of_all = AdoptionNotice.objects.count() + num_without_location = adoption_notices_without_location.count() + print(f"From {num_of_all} there are {num_without_location} adoption notices without location " + f"({num_without_location/num_of_all*100:.2f}%)") + for adoption_notice in adoption_notices_without_location: + print(f"Searching {adoption_notice.location_string} in Nominatim") + location = Location.get_location_from_string(adoption_notice.location_string) + if location: + adoption_notice.location = location + adoption_notice.save() + + adoption_notices_without_location_new = AdoptionNotice.objects.filter(location__isnull=True) + num_without_location_new = adoption_notices_without_location_new.count() + num_new = num_without_location - num_without_location_new + print(f"Added {num_new} new locations")