feat: add location deduplication

This commit is contained in:
2025-05-23 18:13:23 +02:00
parent c50e0b18b5
commit 19d9dea8b1
4 changed files with 41 additions and 4 deletions

View File

@@ -39,7 +39,8 @@ dependencies = [
"djangorestframework",
"celery[redis]",
"drf-spectacular[sidecar]",
"django-widget-tweaks"
"django-widget-tweaks",
"django-super-deduper"
]
dynamic = ["version", "readme"]

View File

@@ -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()

View File

@@ -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)

View File

@@ -60,6 +60,10 @@ LOGGING = {
'handlers': ['console'],
'level': APP_LOG_LEVEL,
},
'django_super_deduper': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}