From 3c949051e5f0d93dde78fa5f101584e6766f6022 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 17 Aug 2012 15:46:34 +0200 Subject: [PATCH] no fb2 on empty parents, cover generation in celery --- .../migrations/0004_remove_parent_fb2.py | 135 ++++++++++++++++++ apps/catalogue/models/book.py | 36 ++--- apps/catalogue/tasks.py | 19 ++- .../templates/catalogue/book_detail.html | 2 +- lib/librarian | 2 +- 5 files changed, 168 insertions(+), 26 deletions(-) create mode 100644 apps/catalogue/migrations/0004_remove_parent_fb2.py diff --git a/apps/catalogue/migrations/0004_remove_parent_fb2.py b/apps/catalogue/migrations/0004_remove_parent_fb2.py new file mode 100644 index 000000000..f048a4599 --- /dev/null +++ b/apps/catalogue/migrations/0004_remove_parent_fb2.py @@ -0,0 +1,135 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import DataMigration +from django.db import models + +class Migration(DataMigration): + """Deletes empty FB2 files from books which have no real own content.""" + + def forwards(self, orm): + "Write your forwards methods here." + # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..." + for book in orm['catalogue.Book'].objects.filter(html_file=''): + if book.fb2_file: + book.fb2_file.delete() + + def backwards(self, orm): + "Write your backwards methods here." + pass + + models = { + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'catalogue.book': { + 'Meta': {'ordering': "('sort_key',)", 'object_name': 'Book'}, + '_related_info': ('jsonfield.fields.JSONField', [], {'null': 'True', 'blank': 'True'}), + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + 'common_slug': ('django.db.models.fields.SlugField', [], {'max_length': '120'}), + 'cover': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'epub_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'extra_info': ('jsonfield.fields.JSONField', [], {'default': "'{}'"}), + 'fb2_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'html_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language': ('django.db.models.fields.CharField', [], {'default': "'pol'", 'max_length': '3', 'db_index': 'True'}), + 'mobi_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['catalogue.Book']"}), + 'parent_number': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'pdf_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120'}), + 'sort_key': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'txt_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'xml_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}) + }, + 'catalogue.bookmedia': { + 'Meta': {'ordering': "('type', 'name')", 'object_name': 'BookMedia'}, + 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'media'", 'to': "orm['catalogue.Book']"}), + 'extra_info': ('jsonfield.fields.JSONField', [], {'default': "'{}'"}), + 'file': ('catalogue.fields.OverwritingFileField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': "'100'"}), + 'source_sha1': ('django.db.models.fields.CharField', [], {'max_length': '40', 'null': 'True', 'blank': 'True'}), + 'type': ('django.db.models.fields.CharField', [], {'max_length': "'100'"}), + 'uploaded_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}) + }, + 'catalogue.collection': { + 'Meta': {'ordering': "('title',)", 'object_name': 'Collection'}, + 'book_slugs': ('django.db.models.fields.TextField', [], {}), + 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'primary_key': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}) + }, + 'catalogue.fragment': { + 'Meta': {'ordering': "('book', 'anchor')", 'object_name': 'Fragment'}, + 'anchor': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'fragments'", 'to': "orm['catalogue.Book']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'short_text': ('django.db.models.fields.TextField', [], {}), + 'text': ('django.db.models.fields.TextField', [], {}) + }, + 'catalogue.tag': { + 'Meta': {'ordering': "('sort_key',)", 'unique_together': "(('slug', 'category'),)", 'object_name': 'Tag'}, + 'book_count': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), + 'category': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '120'}), + 'sort_key': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}), + 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}) + }, + 'catalogue.tagrelation': { + 'Meta': {'unique_together': "(('tag', 'content_type', 'object_id'),)", 'object_name': 'TagRelation', 'db_table': "'catalogue_tag_relation'"}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'items'", 'to': "orm['catalogue.Tag']"}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + } + } + + complete_apps = ['catalogue'] + symmetrical = True diff --git a/apps/catalogue/models/book.py b/apps/catalogue/models/book.py index a8b342d74..5672853f6 100644 --- a/apps/catalogue/models/book.py +++ b/apps/catalogue/models/book.py @@ -160,20 +160,6 @@ class Book(models.Model): provider=ORMDocProvider(self), parse_dublincore=parse_dublincore) - def build_cover(self, book_info=None): - """(Re)builds the cover image.""" - from StringIO import StringIO - from django.core.files.base import ContentFile - from librarian.cover import WLCover - - if book_info is None: - book_info = self.wldocument().book_info - - cover = WLCover(book_info).image() - imgstr = StringIO() - cover.save(imgstr, 'png') - self.cover.save(None, ContentFile(imgstr.getvalue())) - def build_html(self): from django.core.files.base import ContentFile from slughifi import slughifi @@ -233,6 +219,9 @@ class Book(models.Model): return False # Thin wrappers for builder tasks + def build_cover(self): + """(Re)builds the cover image.""" + return tasks.build_cover.delay(self.pk) def build_pdf(self, *args, **kwargs): """(Re)builds PDF.""" return tasks.build_pdf.delay(self.pk, *args, **kwargs) @@ -315,7 +304,6 @@ class Book(models.Model): raise Book.DoesNotExist(_('Book "%s" does not exist.') % part_url.slug) - # Read book metadata book_slug = book_info.url.slug if re.search(r'[^a-z0-9-]', book_slug): @@ -356,17 +344,23 @@ class Book(models.Model): child.save() tasks.fix_tree_tags.delay(child) - # Save XML and HTML files + # Save XML file book.xml_file.save('%s.xml' % book.slug, raw_file, save=False) - book.build_cover(book_info) # delete old fragments when overwriting book.fragments.all().delete() + # Build HTML, fix the tree tags, build cover. + has_own_text = bool(book.build_html()) + tasks.fix_tree_tags.delay(book) + book.build_cover(book_info) + + # No saves behind this point. - if book.build_html(): - # No direct saves behind this point. + if has_own_text: if not settings.NO_BUILD_TXT and build_txt: book.build_txt() + if not settings.NO_BUILD_FB2 and build_fb2: + book.build_fb2() if not settings.NO_BUILD_EPUB and build_epub: book.build_epub() @@ -377,14 +371,10 @@ class Book(models.Model): if not settings.NO_BUILD_MOBI and build_mobi: book.build_mobi() - if not settings.NO_BUILD_FB2 and build_fb2: - book.build_fb2() - if not settings.NO_SEARCH_INDEX and search_index: book.search_index(index_tags=search_index_tags, reuse_index=search_index_reuse) #index_book.delay(book.id, book_info) - tasks.fix_tree_tags.delay(book) cls.published.send(sender=book) return book diff --git a/apps/catalogue/tasks.py b/apps/catalogue/tasks.py index 8cba04fd7..f1d8a9005 100644 --- a/apps/catalogue/tasks.py +++ b/apps/catalogue/tasks.py @@ -103,7 +103,7 @@ def build_fb2(book_id, *args, **kwargs): remove_zip(settings.ALL_FB2_ZIP) -@task(rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT) +@task(ignore_result=True, rate_limit=settings.CATALOGUE_CUSTOMPDF_RATE_LIMIT) def build_custom_pdf(book_id, customizations, file_name): """Builds a custom PDF file.""" from django.core.files import File @@ -116,3 +116,20 @@ def build_custom_pdf(book_id, customizations, file_name): customizations=customizations, morefloats=settings.LIBRARIAN_PDF_MOREFLOATS) DefaultStorage().save(file_name, File(open(pdf.get_filename()))) + + +@task(ignore_result=True) +def build_cover(book_id): + """(Re)builds the cover image.""" + from StringIO import StringIO + from django.core.files.base import ContentFile + from librarian.cover import WLCover + from catalogue.models import Book + + book = Book.objects.get(pk=book_id) + book_info = book.wldocument().book_info + cover = WLCover(book_info).image() + imgstr = StringIO() + cover.save(imgstr, 'png') + book.cover.save(None, ContentFile(imgstr.getvalue()), save=False) + Book.objects.filter(pk=book_id).update(cover=book.cover) diff --git a/apps/catalogue/templates/catalogue/book_detail.html b/apps/catalogue/templates/catalogue/book_detail.html index 783b688d2..4c14158ef 100644 --- a/apps/catalogue/templates/catalogue/book_detail.html +++ b/apps/catalogue/templates/catalogue/book_detail.html @@ -4,7 +4,7 @@ {% load common_tags catalogue_tags pagination_tags %} {% block titleextra %}{{ book.pretty_title }}{% endblock %} -{% block ogimage %}{{ book.cover.url|build_absolute_uri:request }}{% endblock %} +{% block ogimage %}{% if book.cover %}{{ book.cover.url|build_absolute_uri:request }}{% endif %}{% endblock %} {% block metadescription %}{% book_title book %}. {{ block.super }}{% endblock %} diff --git a/lib/librarian b/lib/librarian index d183a3c15..17a9ed3b7 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit d183a3c15dee46ea00ac15c77e72903adfce8d1a +Subproject commit 17a9ed3b7ef12e0786ddf46bf8a52b1087224762 -- 2.20.1