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
11 def ancestor_has_cover(book):
14 if book.extra_info.get('cover_url'):
19 current_domain = Site.objects.get_current().domain
21 return 'http://%s%s' % (
23 obj.get_absolute_url())
26 class Command(BaseCommand):
27 option_list = BaseCommand.option_list + (
28 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
29 help='Suppress output'),
31 help = 'Checks cover sources and licenses.'
33 def handle(self, **options):
34 from collections import defaultdict
36 from django.db import transaction
37 from catalogue.models import Book
39 verbose = options['verbose']
42 with_ancestral_cover = []
44 bad_license = defaultdict(list)
47 re_license = re.compile(ur'.*,\s*(CC.*)')
49 redakcja_url = app_settings.REDAKCJA_URL
50 good_license = re.compile("(%s)" % ")|(".join(
51 app_settings.GOOD_LICENSES))
53 with transaction.commit_on_success():
54 for book in Book.objects.all().order_by('slug').iterator():
55 extra_info = book.extra_info
56 if not extra_info.get('cover_url'):
57 if ancestor_has_cover(book):
58 with_ancestral_cover.append(book)
60 without_cover.append(book)
62 if not extra_info.get('cover_source', ''
63 ).startswith(redakcja_url):
64 not_redakcja.append(book)
65 match = re_license.match(extra_info.get('cover_by', ''))
67 if not good_license.match(match.group(1)):
68 bad_license[match.group(1)].append(book)
70 no_license.append(book)
72 print """%d books with no covers, %d with inherited covers.
73 Bad licenses used: %s (%d covers without license).
74 %d covers not from %s.
77 len(with_ancestral_cover),
78 ", ".join(sorted(bad_license.keys())) or "none",
89 for lic, books in bad_license.items():
99 for book in no_license:
102 print book.extra_info.get('cover_by')
103 print book.extra_info.get('cover_source')
104 print book.extra_info.get('cover_url')
108 print "Not from Redakcja or source missing:"
109 print "===================================="
110 for book in not_redakcja:
113 print book.extra_info.get('cover_by')
114 print book.extra_info.get('cover_source')
115 print book.extra_info.get('cover_url')
121 for book in without_cover:
124 if with_ancestral_cover:
126 print "With ancestral cover:"
127 print "====================="
128 for book in with_ancestral_cover: