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.contrib.sites.models import Site
5 from django.core.management.base import BaseCommand
6 from django.utils.functional import lazy
7 from catalogue import app_settings
10 def ancestor_has_cover(book):
13 if book.extra_info.get('cover_url'):
18 current_domain = lazy(lambda: Site.objects.get_current().domain, str)()
22 return 'http://%s%s' % (
24 obj.get_absolute_url())
27 class Command(BaseCommand):
28 help = 'Checks cover sources and licenses.'
30 def add_arguments(self, parser):
32 '-q', '--quiet', action='store_false', dest='verbose',
33 default=True, help='Suppress output')
35 def handle(self, **options):
36 from collections import defaultdict
38 from django.db import transaction
39 from catalogue.models import Book
41 verbose = options['verbose']
44 with_ancestral_cover = []
46 bad_license = defaultdict(list)
49 re_license = re.compile(r'.*,\s*(CC.*)')
51 redakcja_url = app_settings.REDAKCJA_URL
52 good_license = re.compile("(%s)" % ")|(".join(
53 app_settings.GOOD_LICENSES))
55 with transaction.atomic():
56 for book in Book.objects.all().order_by('slug').iterator():
57 extra_info = book.extra_info
58 if not extra_info.get('cover_url'):
59 if ancestor_has_cover(book):
60 with_ancestral_cover.append(book)
62 without_cover.append(book)
64 if not extra_info.get('cover_source', '').startswith(redakcja_url):
65 not_redakcja.append(book)
66 match = re_license.match(extra_info.get('cover_by', ''))
68 if not good_license.match(match.group(1)):
69 bad_license[match.group(1)].append(book)
71 no_license.append(book)
73 print("""%d books with no covers, %d with inherited covers.
74 Bad licenses used: %s (%d covers without license).
75 %d covers not from %s.
78 len(with_ancestral_cover),
79 ", ".join(sorted(bad_license.keys())) or "none",
90 for lic, books in bad_license.items():
100 for book in no_license:
102 print(full_url(book))
103 print(book.extra_info.get('cover_by'))
104 print(book.extra_info.get('cover_source'))
105 print(book.extra_info.get('cover_url'))
109 print("Not from Redakcja or source missing:")
110 print("====================================")
111 for book in not_redakcja:
113 print(full_url(book))
114 print(book.extra_info.get('cover_by'))
115 print(book.extra_info.get('cover_source'))
116 print(book.extra_info.get('cover_url'))
122 for book in without_cover:
123 print(full_url(book))
125 if with_ancestral_cover:
127 print("With ancestral cover:")
128 print("=====================")
129 for book in with_ancestral_cover:
130 print(full_url(book))