bookwyrm/bookwyrm/management/commands/deduplicate_book_data.py

47 lines
1.5 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" PROCEED WITH CAUTION: uses deduplication fields to permanently
merge book data objects """
from django.core.management.base import BaseCommand
from django.db.models import Count
from bookwyrm import models
2021-01-01 00:30:04 +00:00
def dedupe_model(model):
2021-04-26 16:15:42 +00:00
"""combine duplicate editions and update related models"""
print(f"deduplicating {model.__name__}:")
fields = model._meta.get_fields()
2021-03-08 16:49:10 +00:00
dedupe_fields = [
f for f in fields if hasattr(f, "deduplication_field") and f.deduplication_field
]
for field in dedupe_fields:
2021-03-08 16:49:10 +00:00
dupes = (
model.objects.values(field.name)
.annotate(Count(field.name))
.filter(**{f"{field.name}__count__gt": 1})
.exclude(**{field.name: ""})
.exclude(**{f"{field.name}__isnull": True})
2021-03-08 16:49:10 +00:00
)
for dupe in dupes:
value = dupe[field.name]
2021-03-08 16:49:10 +00:00
print("----------")
objs = model.objects.filter(**{field.name: value}).order_by("id")
canonical = objs.first()
print(f"merging into {canonical.remote_id} based on {field.name} {value}:")
for obj in objs[1:]:
print(f"- {obj.remote_id}")
obj.merge_into(canonical)
class Command(BaseCommand):
"""deduplicate allllll the book data models"""
2021-03-08 16:49:10 +00:00
help = "merges duplicate book data"
2021-01-01 00:30:04 +00:00
# pylint: disable=no-self-use,unused-argument
def handle(self, *args, **options):
"""run deduplications"""
dedupe_model(models.Edition)
dedupe_model(models.Work)
dedupe_model(models.Author)