1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.core.management.base import BaseCommand
5 from librarian import ParseError
6 from catalogue.models import Book
9 class Command(BaseCommand):
10 help = 'Checks integrity of catalogue data.'
12 def add_arguments(self, parser):
14 '-q', '--quiet', action='store_false', dest='verbose',
15 default=True, help='Suppress output')
17 '-d', '--dry-run', action='store_true', dest='dry_run',
18 default=False, help="Just check for problems, don't fix them")
20 def handle(self, **options):
21 from django.db import transaction
23 verbose = options['verbose']
25 with transaction.atomic():
26 for book in Book.objects.all().iterator():
28 info = book.wldocument().book_info
31 print("ERROR! Bad XML for book:", book.slug)
32 print("To resolve: republish.")
35 should_be = [p.slug for p in info.parts]
36 is_now = [p.slug for p in book.children.all().order_by('parent_number')]
37 if should_be != is_now:
39 print("ERROR! Wrong children for book:", book.slug)
40 # print("Is: ", is_now)
41 # print("Should be:", should_be)
42 from difflib import ndiff
43 print('\n'.join(ndiff(is_now, should_be)))
44 print("To resolve: republish parent book.")
51 parents.append(parent)
52 parent = parent.parent
53 ancestors = list(book.ancestor.all())
54 if set(ancestors) != set(parents):
55 if options['verbose']:
56 print("Wrong ancestry for book:", book)
57 print("Is: ", ", ".join(ancestors))
58 print("Should be:", ", ".join(parents))
59 if not options['dry_run']:
60 book.repopulate_ancestors()
61 if options['verbose']:
63 if options['verbose']:
66 # TODO: check metadata tags, reset counters