From 452b47c5a7bafaba6791010094d85c7c06fdac38 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 22 Aug 2012 12:30:52 +0200 Subject: [PATCH] Move updating covers in children to the end of publishing process, update checkcover report --- apps/catalogue/__init__.py | 3 + .../management/commands/checkcovers.py | 112 +++++++++--------- apps/catalogue/models/book.py | 8 +- lib/librarian | 2 +- 4 files changed, 65 insertions(+), 60 deletions(-) diff --git a/apps/catalogue/__init__.py b/apps/catalogue/__init__.py index 45acadc64..1fbdc2093 100644 --- a/apps/catalogue/__init__.py +++ b/apps/catalogue/__init__.py @@ -19,6 +19,9 @@ class Settings(AppSettings): 'fb2': 'wolnelektury_pl_fb2', } + REDAKCJA_URL = "http://redakcja.wolnelektury.pl" + GOOD_LICENSES = set([r'CC BY \d\.\d', r'CC BY-SA \d\.\d']) + def _more_DONT_BUILD(self, value): for format_ in ['cover', 'pdf', 'epub', 'mobi', 'fb2', 'txt']: attname = 'NO_BUILD_%s' % format_.upper() diff --git a/apps/catalogue/management/commands/checkcovers.py b/apps/catalogue/management/commands/checkcovers.py index ef61702ce..1af39e15a 100644 --- a/apps/catalogue/management/commands/checkcovers.py +++ b/apps/catalogue/management/commands/checkcovers.py @@ -5,6 +5,7 @@ from optparse import make_option from django.contrib.sites.models import Site from django.core.management.base import BaseCommand +from catalogue import app_settings def ancestor_has_cover(book): @@ -39,14 +40,16 @@ class Command(BaseCommand): without_cover = [] with_ancestral_cover = [] - by_flickr_author = defaultdict(list) - not_flickr = [] - by_license = defaultdict(list) + not_redakcja = [] + bad_license = defaultdict(list) no_license = [] - re_flickr = re.compile(ur'https?://(?:www\.|secure\.)?flickr.com/photos/([^/]*)/.*') re_license = re.compile(ur'.*,\s*(CC.*)') + redakcja_url = app_settings.REDAKCJA_URL + good_license = re.compile("(%s)" % ")|(".join( + app_settings.GOOD_LICENSES)) + with transaction.commit_on_success(): for book in Book.objects.all().order_by('slug').iterator(): extra_info = book.extra_info @@ -56,76 +59,71 @@ class Command(BaseCommand): else: without_cover.append(book) else: - match = re_flickr.match(extra_info.get('cover_source', '')) - if match: - by_flickr_author[match.group(1)].append(book) - else: - not_flickr.append(book) + if not extra_info.get('cover_source', '' + ).startswith(redakcja_url): + not_redakcja.append(book) match = re_license.match(extra_info.get('cover_by', '')) if match: - by_license[match.group(1)].append(book) + if not good_license.match(match.group(1)): + bad_license[match.group(1)].append(book) else: no_license.append(book) - print """%d books with no covers, %d with ancestral covers. -Licenses used: %s (%d covers without license). -Flickr authors: %s (%d covers not from flickr). + print """%d books with no covers, %d with inherited covers. +Bad licenses used: %s (%d covers without license). +%d covers not from %s. """ % ( len(without_cover), len(with_ancestral_cover), - ", ".join(sorted(by_license.keys())), + ", ".join(sorted(bad_license.keys())) or "none", len(no_license), - ", ".join(sorted(by_flickr_author.keys())), - len(not_flickr), + len(not_redakcja), + redakcja_url, ) if verbose: - print - print "By license:" - print "===========" - for lic, books in by_license.items(): + if bad_license: + print + print "Bad license:" + print "============" + for lic, books in bad_license.items(): + print + print lic + for book in books: + print full_url(book) + + if no_license: print - print lic - for book in books: + print "No license:" + print "===========" + for book in no_license: + print print full_url(book) + print book.extra_info.get('cover_by') + print book.extra_info.get('cover_source') + print book.extra_info.get('cover_url') - print - print "No license:" - print "===========" - for book in no_license: + if not_redakcja: print - print full_url(book) - print book.extra_info.get('cover_by') - print book.extra_info.get('cover_source') - print book.extra_info.get('cover_url') - - print - print "By Flickr author:" - print "=================" - for author, books in by_flickr_author.items(): + print "Not from Redakcja or source missing:" + print "====================================" + for book in not_redakcja: + print + print full_url(book) + print book.extra_info.get('cover_by') + print book.extra_info.get('cover_source') + print book.extra_info.get('cover_url') + + if without_cover: print - print "author: http://flickr.com/photos/%s/" % author - for book in books: + print "No cover:" + print "=========" + for book in without_cover: print full_url(book) - print - print "Not from Flickr or source missing:" - print "==================================" - for book in not_flickr: + if with_ancestral_cover: print - print full_url(book) - print book.extra_info.get('cover_by') - print book.extra_info.get('cover_source') - print book.extra_info.get('cover_url') - - print - print "No cover:" - print "=========" - for book in without_cover: - print full_url(book) - - print - print "With ancestral cover:" - print "=====================" - for book in with_ancestral_cover: - print full_url(book) + print "With ancestral cover:" + print "=====================" + for book in with_ancestral_cover: + print full_url(book) diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index c2b54e4c5..6a31f4bff 100644 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -274,13 +274,14 @@ class Book(models.Model): cover_changed = old_cover != book.cover_info() obsolete_children = set(b for b in book.children.all() if b not in children) + notify_cover_changed = [] for n, child_book in enumerate(children): new_child = child_book.parent != book child_book.parent = book child_book.parent_number = n child_book.save() if new_child or cover_changed: - child_book.parent_cover_changed() + notify_cover_changed.append(child_book) # Disown unfaithful children and let them cope on their own. for child in obsolete_children: child.parent = None @@ -288,7 +289,7 @@ class Book(models.Model): child.save() tasks.fix_tree_tags.delay(child) if old_cover: - child.parent_cover_changed() + notify_cover_changed.append(child) # delete old fragments when overwriting book.fragments.all().delete() @@ -312,6 +313,9 @@ class Book(models.Model): book.search_index(index_tags=search_index_tags, reuse_index=search_index_reuse) #index_book.delay(book.id, book_info) + for child in notify_cover_changed: + child.parent_cover_changed() + cls.published.send(sender=book) return book diff --git a/lib/librarian b/lib/librarian index cbe81ee35..fb1bbfe62 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit cbe81ee35b07783b4f52c3d3dda83db7aaf82d34 +Subproject commit fb1bbfe622abeaaea2a2dfc459d8b779b2d369a2 -- 2.20.1