1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. 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.get_extra_info_json().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.get_extra_info_json()
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 extra_info = book.get_extra_info_json()
104 print(extra_info.get('cover_by'))
105 print(extra_info.get('cover_source'))
106 print(extra_info.get('cover_url'))
110 print("Not from Redakcja or source missing:")
111 print("====================================")
112 for book in not_redakcja:
114 print(full_url(book))
115 extra_info = book.get_extra_info_json()
116 print(extra_info.get('cover_by'))
117 print(extra_info.get('cover_source'))
118 print(extra_info.get('cover_url'))
124 for book in without_cover:
125 print(full_url(book))
127 if with_ancestral_cover:
129 print("With ancestral cover:")
130 print("=====================")
131 for book in with_ancestral_cover:
132 print(full_url(book))