2021-03-08 16:49:10 +00:00
|
|
|
""" PROCEED WITH CAUTION: this permanently deletes book data """
|
2021-01-31 02:30:07 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.db.models import Count, Q
|
|
|
|
from bookwyrm import models
|
|
|
|
|
|
|
|
|
|
|
|
def remove_editions():
|
2021-04-26 16:15:42 +00:00
|
|
|
"""combine duplicate editions and update related models"""
|
2021-01-31 02:30:07 +00:00
|
|
|
# not in use
|
2021-03-08 16:49:10 +00:00
|
|
|
filters = {
|
|
|
|
"%s__isnull" % r.name: True for r in models.Edition._meta.related_objects
|
|
|
|
}
|
2021-01-31 02:30:07 +00:00
|
|
|
# no cover, no identifying fields
|
2021-03-08 16:49:10 +00:00
|
|
|
filters["cover"] = ""
|
|
|
|
null_fields = {
|
|
|
|
"%s__isnull" % f: True for f in ["isbn_10", "isbn_13", "oclc_number"]
|
|
|
|
}
|
2021-01-31 02:30:07 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
editions = (
|
|
|
|
models.Edition.objects.filter(
|
|
|
|
Q(languages=[]) | Q(languages__contains=["English"]),
|
|
|
|
**filters,
|
|
|
|
**null_fields
|
|
|
|
)
|
|
|
|
.annotate(Count("parent_work__editions"))
|
|
|
|
.filter(
|
|
|
|
# mustn't be the only edition for the work
|
|
|
|
parent_work__editions__count__gt=1
|
|
|
|
)
|
2021-01-31 02:30:07 +00:00
|
|
|
)
|
|
|
|
print(editions.count())
|
|
|
|
editions.delete()
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""dedplucate allllll the book data models"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
help = "merges duplicate book data"
|
2021-01-31 02:30:07 +00:00
|
|
|
# pylint: disable=no-self-use,unused-argument
|
|
|
|
def handle(self, *args, **options):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""run deudplications"""
|
2021-01-31 02:30:07 +00:00
|
|
|
remove_editions()
|