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
10 def ancestor_has_cover(book):
13 if book.extra_info.get('cover_url'):
18 current_domain = Site.objects.get_current().domain
20 return 'http://%s%s' % (
22 obj.get_absolute_url())
25 class Command(BaseCommand):
26 option_list = BaseCommand.option_list + (
27 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
28 help='Suppress output'),
30 help = 'Checks cover sources and licenses.'
32 def handle(self, **options):
33 from collections import defaultdict
35 from django.db import transaction
36 from catalogue.models import Book
38 verbose = options['verbose']
41 with_ancestral_cover = []
42 by_flickr_author = defaultdict(list)
44 by_license = defaultdict(list)
47 re_flickr = re.compile(ur'https?://(?:www\.|secure\.)?flickr.com/photos/([^/]*)/.*')
48 re_license = re.compile(ur'.*,\s*(CC.*)')
50 with transaction.commit_on_success():
51 for book in Book.objects.all().order_by('slug').iterator():
52 extra_info = book.extra_info
53 if not extra_info.get('cover_url'):
54 if ancestor_has_cover(book):
55 with_ancestral_cover.append(book)
57 without_cover.append(book)
59 match = re_flickr.match(extra_info.get('cover_source', ''))
61 by_flickr_author[match.group(1)].append(book)
63 not_flickr.append(book)
64 match = re_license.match(extra_info.get('cover_by', ''))
66 by_license[match.group(1)].append(book)
68 no_license.append(book)
70 print """%d books with no covers, %d with ancestral covers.
71 Licenses used: %s (%d covers without license).
72 Flickr authors: %s (%d covers not from flickr).
75 len(with_ancestral_cover),
76 ", ".join(sorted(by_license.keys())),
78 ", ".join(sorted(by_flickr_author.keys())),
86 for lic, books in by_license.items():
95 for book in no_license:
98 print book.extra_info.get('cover_by')
99 print book.extra_info.get('cover_source')
100 print book.extra_info.get('cover_url')
103 print "By Flickr author:"
104 print "================="
105 for author, books in by_flickr_author.items():
107 print "author: http://flickr.com/photos/%s/" % author
112 print "Not from Flickr or source missing:"
113 print "=================================="
114 for book in not_flickr:
117 print book.extra_info.get('cover_by')
118 print book.extra_info.get('cover_source')
119 print book.extra_info.get('cover_url')
124 for book in without_cover:
128 print "With ancestral cover:"
129 print "====================="
130 for book in with_ancestral_cover: