1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from optparse import make_option
6 from django.contrib.sites.models import Site
7 from django.core.management.base import BaseCommand
8 from catalogue import app_settings
9 from django.utils.functional import lazy
12 def ancestor_has_cover(book):
15 if book.extra_info.get('cover_url'):
20 current_domain = lazy(lambda: Site.objects.get_current().domain, str)()
24 return 'http://%s%s' % (
26 obj.get_absolute_url())
29 class Command(BaseCommand):
30 option_list = BaseCommand.option_list + (
31 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
32 help='Suppress output'),
34 help = 'Checks cover sources and licenses.'
36 def handle(self, **options):
37 from collections import defaultdict
39 from django.db import transaction
40 from catalogue.models import Book
42 verbose = options['verbose']
45 with_ancestral_cover = []
47 bad_license = defaultdict(list)
50 re_license = re.compile(ur'.*,\s*(CC.*)')
52 redakcja_url = app_settings.REDAKCJA_URL
53 good_license = re.compile("(%s)" % ")|(".join(
54 app_settings.GOOD_LICENSES))
56 with transaction.atomic():
57 for book in Book.objects.all().order_by('slug').iterator():
58 extra_info = book.extra_info
59 if not extra_info.get('cover_url'):
60 if ancestor_has_cover(book):
61 with_ancestral_cover.append(book)
63 without_cover.append(book)
65 if not extra_info.get('cover_source', '').startswith(redakcja_url):
66 not_redakcja.append(book)
67 match = re_license.match(extra_info.get('cover_by', ''))
69 if not good_license.match(match.group(1)):
70 bad_license[match.group(1)].append(book)
72 no_license.append(book)
74 print """%d books with no covers, %d with inherited covers.
75 Bad licenses used: %s (%d covers without license).
76 %d covers not from %s.
79 len(with_ancestral_cover),
80 ", ".join(sorted(bad_license.keys())) or "none",
91 for lic, books in bad_license.items():
101 for book in no_license:
104 print book.extra_info.get('cover_by')
105 print book.extra_info.get('cover_source')
106 print book.extra_info.get('cover_url')
110 print "Not from Redakcja or source missing:"
111 print "===================================="
112 for book in not_redakcja:
115 print book.extra_info.get('cover_by')
116 print book.extra_info.get('cover_source')
117 print book.extra_info.get('cover_url')
123 for book in without_cover:
126 if with_ancestral_cover:
128 print "With ancestral cover:"
129 print "====================="
130 for book in with_ancestral_cover: