From 19d9dea8b190698dff5519f9872a9a68ae263111 Mon Sep 17 00:00:00 2001 From: moanos Date: Fri, 23 May 2025 18:13:23 +0200 Subject: [PATCH] feat: add location deduplication --- pyproject.toml | 3 +- .../management/commands/dedup_locations.py | 10 +++++++ src/fellchensammlung/tools/admin.py | 28 +++++++++++++++++-- src/notfellchen/settings.py | 4 +++ 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/fellchensammlung/management/commands/dedup_locations.py diff --git a/pyproject.toml b/pyproject.toml index 6c0422e..c59c58b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,8 @@ dependencies = [ "djangorestframework", "celery[redis]", "drf-spectacular[sidecar]", - "django-widget-tweaks" + "django-widget-tweaks", + "django-super-deduper" ] dynamic = ["version", "readme"] diff --git a/src/fellchensammlung/management/commands/dedup_locations.py b/src/fellchensammlung/management/commands/dedup_locations.py new file mode 100644 index 0000000..9b36f68 --- /dev/null +++ b/src/fellchensammlung/management/commands/dedup_locations.py @@ -0,0 +1,10 @@ +from django.core.management import BaseCommand +from fellchensammlung.tools.admin import dedup_locations + + +class Command(BaseCommand): + help = 'Deduplicate locations based on place_id' + + def handle(self, *args, **options): + dedup_locations() + diff --git a/src/fellchensammlung/tools/admin.py b/src/fellchensammlung/tools/admin.py index c6301a9..13ffcc7 100644 --- a/src/fellchensammlung/tools/admin.py +++ b/src/fellchensammlung/tools/admin.py @@ -3,6 +3,8 @@ import logging from django.utils import timezone from datetime import timedelta +from django_super_deduper.merge import MergedModelInstance + from fellchensammlung.models import AdoptionNotice, Location, RescueOrganization, AdoptionNoticeStatus, Log, \ AdoptionNoticeNotification from fellchensammlung.tools.misc import is_404 @@ -87,6 +89,26 @@ def deactivate_404_adoption_notices(): deactivation_message = f'Die Vermittlung [{adoption_notice.name}]({adoption_notice.get_absolute_url()}) wurde automatisch deaktiviert, da die Website unter "Mehr Informationen" nicht mehr online ist.' for subscription in adoption_notice.get_subscriptions(): AdoptionNoticeNotification.objects.create(user=subscription.owner, - title="Vermittlung deaktiviert", - adoption_notice=adoption_notice, - text=deactivation_message) + title="Vermittlung deaktiviert", + adoption_notice=adoption_notice, + text=deactivation_message) + + +def dedup_location(location: Location, destructive=False): + duplicates = Location.objects.filter(place_id=location.place_id).exclude(id=location.id) + merged_object = MergedModelInstance.create(location, duplicates) + if destructive: + duplicates.delete() + print("Deleted duplicate locations") + return merged_object + + +def dedup_locations(): + location_ids = list(Location.objects.values_list("id", flat=True)) + for location_id in location_ids: + try: + location = Location.objects.get(id=location_id) + except Location.DoesNotExist: + # Already deleted as a duplicate + continue + dedup_location(location, destructive=True) diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index 816b480..b8d7271 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -60,6 +60,10 @@ LOGGING = { 'handlers': ['console'], 'level': APP_LOG_LEVEL, }, + 'django_super_deduper': { + 'handlers': ['console'], + 'level': 'DEBUG', + }, }, }