From: Radek Czajka Date: Mon, 19 Dec 2011 16:28:24 +0000 (+0100) Subject: cover images; sorl>=10; hook sponsors off sorl X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/d606942f6eb286933eb71f4232db580fcb3a166c cover images; sorl>=10; hook sponsors off sorl --- diff --git a/apps/catalogue/migrations/0019_auto__add_field_book_cover.py b/apps/catalogue/migrations/0019_auto__add_field_book_cover.py new file mode 100644 index 000000000..259d935ba --- /dev/null +++ b/apps/catalogue/migrations/0019_auto__add_field_book_cover.py @@ -0,0 +1,125 @@ +# encoding: utf-8 +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + +class Migration(SchemaMigration): + + def forwards(self, orm): + + # Adding field 'Book.cover' + db.add_column('catalogue_book', 'cover', self.gf('django.db.models.fields.files.FileField')(max_length=100, null=True, blank=True), keep_default=False) + + + def backwards(self, orm): + + # Deleting field 'Book.cover' + db.delete_column('catalogue_book', 'cover') + + + 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',)", 'unique_together': "[['slug', 'language']]", 'object_name': 'Book'}, + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + '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': ('catalogue.fields.JSONField', [], {'default': "'{}'"}), + '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', [], {'max_length': '120', 'db_index': 'True'}), + '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': ('catalogue.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.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'}), + 'main_page': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'db_index': 'True'}), + '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'] diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 359a89e7e..f22c9e19e 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -180,7 +180,7 @@ class Tag(TagBase): tag_sort_key = tag_name if category == 'author': tag_sort_key = tag_name.last_name - tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name + tag_name = tag_name.readable() tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name), category=category) if created: tag.name = tag_name @@ -356,6 +356,8 @@ class Book(models.Model): wiki_link = models.CharField(blank=True, max_length=240) # files generated during publication + cover = models.FileField(_('cover'), upload_to=book_upload_path('png'), + null=True, blank=True) ebook_formats = ['pdf', 'epub', 'mobi', 'txt'] formats = ebook_formats + ['html', 'xml'] @@ -571,6 +573,20 @@ 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_pdf(self, customizations=None, file_name=None): """ (Re)builds the pdf file. customizations - customizations which are passed to LaTeX class file. @@ -789,6 +805,8 @@ class Book(models.Model): if not settings.NO_BUILD_TXT and build_txt: book.build_txt() + book.build_cover(book_info) + if not settings.NO_BUILD_EPUB and build_epub: book.build_epub() diff --git a/apps/catalogue/test_utils.py b/apps/catalogue/test_utils.py index 70aae02d4..58ef58acd 100644 --- a/apps/catalogue/test_utils.py +++ b/apps/catalogue/test_utils.py @@ -24,8 +24,16 @@ class PersonStub(object): self.first_names = first_names self.last_name = last_name + def readable(self): + return " ".join(self.first_names + (self.last_name,)) + class BookInfoStub(object): + _empty_fields = ['cover_url'] + # allow single definition for multiple-value fields + _salias = { + 'authors': 'author', + } def __init__(self, **kwargs): self.__dict = kwargs @@ -36,7 +44,15 @@ class BookInfoStub(object): return object.__setattr__(self, key, value) def __getattr__(self, key): - return self.__dict[key] + try: + return self.__dict[key] + except KeyError: + if key in self._empty_fields: + return None + elif key in self._salias: + return [getattr(self, self._salias[key])] + else: + raise def to_dict(self): return dict((key, unicode(value)) for key, value in self.__dict.items()) diff --git a/apps/sponsors/models.py b/apps/sponsors/models.py index d91c8f420..4357d7863 100644 --- a/apps/sponsors/models.py +++ b/apps/sponsors/models.py @@ -9,7 +9,6 @@ from django.utils.translation import ugettext_lazy as _ from django.template.loader import render_to_string from PIL import Image -from sorl.thumbnail.fields import ImageWithThumbnailsField from sponsors.fields import JSONField from django.core.files.base import ContentFile @@ -19,14 +18,7 @@ THUMB_HEIGHT=120 class Sponsor(models.Model): name = models.CharField(_('name'), max_length=120) _description = models.CharField(_('description'), blank=True, max_length=255) - logo = ImageWithThumbnailsField( - _('logo'), - upload_to='sponsorzy/sponsor/logo', - thumbnail={ - 'size': (THUMB_WIDTH, THUMB_HEIGHT), - 'extension': 'png', - 'options': ['pad', 'detail'], - }) + logo = models.ImageField(_('logo'), upload_to='sponsorzy/sponsor/logo') url = models.URLField(_('url'), blank=True, verify_exists=False) def __unicode__(self): @@ -67,8 +59,19 @@ class SponsorPage(models.Model): sponsors = Sponsor.objects.in_bulk(sponsor_ids) sprite = Image.new('RGBA', (THUMB_WIDTH, len(sponsors)*THUMB_HEIGHT)) for i, sponsor_id in enumerate(sponsor_ids): - simg = Image.open(sponsors[sponsor_id].logo.thumbnail.dest) - sprite.paste(simg, (0, i*THUMB_HEIGHT)) + simg = Image.open(sponsors[sponsor_id].logo.path) + if simg.size[0] > THUMB_WIDTH or simg.size[1] > THUMB_HEIGHT: + size = ( + min(THUMB_WIDTH, + simg.size[0] * THUMB_HEIGHT / simg.size[1]), + min(THUMB_HEIGHT, + simg.size[1] * THUMB_WIDTH / simg.size[0]) + ) + simg = simg.resize(size, Image.ANTIALIAS) + sprite.paste(simg, ( + (THUMB_WIDTH - simg.size[0]) / 2, + i * THUMB_HEIGHT + (THUMB_HEIGHT - simg.size[1]) / 2, + )) imgstr = StringIO() sprite.save(imgstr, 'png') diff --git a/apps/sponsors/processors.py b/apps/sponsors/processors.py deleted file mode 100644 index 112241d96..000000000 --- a/apps/sponsors/processors.py +++ /dev/null @@ -1,18 +0,0 @@ -# -*- coding: utf-8 -*- -# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# -from PIL import Image, ImageFilter, ImageChops - - -def add_padding(image, requested_size, opts): - if 'pad' in opts: - padded_image = Image.new('RGBA', requested_size, '#fff') - width, height = image.size - requested_width, requested_height = requested_size - print 'whatever' - padded_image.paste(image, (0, (requested_height - height) / 2)) - return padded_image - return image - -add_padding.valid_options = ('pad',) diff --git a/apps/sponsors/static/sponsors/css/footer_admin.css b/apps/sponsors/static/sponsors/css/footer_admin.css index 1277bd2d3..fe19d30c9 100644 --- a/apps/sponsors/static/sponsors/css/footer_admin.css +++ b/apps/sponsors/static/sponsors/css/footer_admin.css @@ -66,3 +66,8 @@ background-color: #EEE; cursor: default; } + +.sponsors-sponsor img { + max-width: 120px; + max-height: 120px; +} diff --git a/apps/sponsors/widgets.py b/apps/sponsors/widgets.py index e4b30bbbc..fc1387323 100644 --- a/apps/sponsors/widgets.py +++ b/apps/sponsors/widgets.py @@ -23,7 +23,7 @@ class SponsorPageWidget(forms.Textarea): def render(self, name, value, attrs=None): output = [super(SponsorPageWidget, self).render(name, value, attrs)] - sponsors = [(unicode(obj), obj.pk, obj.logo.thumbnail) for obj in models.Sponsor.objects.all()] + sponsors = [(unicode(obj), obj.pk, obj.logo.url) for obj in models.Sponsor.objects.all()] sponsors_js = ', '.join('{name: "%s", id: %d, image: "%s"}' % sponsor for sponsor in sponsors) output.append(u' - {% compressed_js "jquery" %} - {% compressed_js "all" %} + {% block extrahead %} {% endblock %} @@ -167,6 +165,11 @@ {% endblock bodycontent %} + + + + {% compressed_js "base" %} +