cover images; sorl>=10; hook sponsors off sorl
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 19 Dec 2011 16:28:24 +0000 (17:28 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 19 Dec 2011 16:28:24 +0000 (17:28 +0100)
15 files changed:
apps/catalogue/migrations/0019_auto__add_field_book_cover.py [new file with mode: 0644]
apps/catalogue/models.py
apps/catalogue/test_utils.py
apps/sponsors/models.py
apps/sponsors/processors.py [deleted file]
apps/sponsors/static/sponsors/css/footer_admin.css
apps/sponsors/widgets.py
lib/librarian
requirements.txt
wolnelektury/settings.py
wolnelektury/static/css/sponsors.css
wolnelektury/static/js/sponsors.js [new file with mode: 0755]
wolnelektury/static/sponsors/css/footer_admin.css
wolnelektury/templates/base.html
wolnelektury/templates/catalogue/book_mini_box.html

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 (file)
index 0000000..259d935
--- /dev/null
@@ -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']
index 359a89e..f22c9e1 100644 (file)
@@ -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()
 
index 70aae02..58ef58a 100644 (file)
@@ -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())
index d91c8f4..4357d78 100644 (file)
@@ -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 (file)
index 112241d..0000000
+++ /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',)
index 1277bd2..fe19d30 100644 (file)
@@ -66,3 +66,8 @@
     background-color: #EEE;
     cursor: default;
 }
+
+.sponsors-sponsor img {
+    max-width: 120px;
+    max-height: 120px;
+}
index e4b30bb..fc13873 100644 (file)
@@ -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'<script type="text/javascript">addEvent(window, "load", function(e) {')
         # TODO: "id_" is hard-coded here. This should instead use the correct
index 05b19da..808e7c2 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 05b19dad8ca44136e064abcc6beff5dac4489c60
+Subproject commit 808e7c2967440cacbc15af82cde171e10aea8a6a
index 4fd263e..5f99441 100644 (file)
@@ -17,7 +17,7 @@ Feedparser>=4.1
 # PIL 
 PIL>=1.1.6
 mutagen>=1.17
-sorl-thumbnail>=3.2,<10
+sorl-thumbnail>=10,<12
 
 # home-brewed & dependencies
 lxml>=2.2.2
index 0692cc7..91254ab 100644 (file)
@@ -167,6 +167,7 @@ COMPRESS_CSS = {
             'css/header.css',
             'css/main_page.css',
             'css/book_box.css',
+            'css/sponsors.css',
         ],
         'output_filename': 'css/all.min?.css',
     },
@@ -181,23 +182,21 @@ COMPRESS_CSS = {
 }
 
 COMPRESS_JS = {
-    #~ 'jquery': {
-        #~ #'source_filenames': ('js/jquery.js',),
-        #~ 'source_filenames': [],
-        #~ 'output_filename': 'js/jquery.min.js',
-    #~ },
-    #~ 'all': {
-        #~ 'source_filenames': ('js/jquery.autocomplete.js', 'js/jquery.form.js',
+    'base': {
+        'source_filenames': (
+            'js/jquery.cycle.min.js',
+
+            'js/sponsors.js',
+        
+            #~ 'js/jquery.autocomplete.js', 'js/jquery.form.js',
             #~ 'js/jquery.countdown.js', 'js/jquery.countdown-pl.js',
             #~ 'js/jquery.countdown-de.js', 'js/jquery.countdown-uk.js',
             #~ 'js/jquery.countdown-es.js', 'js/jquery.countdown-lt.js',
             #~ 'js/jquery.countdown-ru.js', 'js/jquery.countdown-fr.js',
-            #~ 'js/jquery.cycle.min.js',
             #~ 'js/jquery.jqmodal.js', 'js/jquery.labelify.js', 'js/catalogue.js',
-            #~ ),
-        #~ 'source_filenames': [],
-        #~ 'output_filename': 'js/all?.min.js',
-    #~ },
+            ),
+        'output_filename': 'js/base?.min.js',
+    },
     #~ 'book': {
         #~ 'source_filenames': ('js/jquery.eventdelegation.js', 'js/jquery.scrollto.js', 'js/jquery.highlightfade.js', 'js/book.js',),
         #~ 'source_filenames': [],
index a1798b1..1624f5f 100644 (file)
@@ -1,4 +1,5 @@
 .sponsors-page {
+    background: white;
     margin-top: 6px;
 }
 
@@ -7,6 +8,10 @@
     width: 150px;
 }
 
+.sponsor-logos {
+    height: 130px;
+}
+
 .sponsors-page img {
     float: left;
 }
diff --git a/wolnelektury/static/js/sponsors.js b/wolnelektury/static/js/sponsors.js
new file mode 100755 (executable)
index 0000000..7674379
--- /dev/null
@@ -0,0 +1,8 @@
+(function($) {
+    $(function() {
+
+        $('.sponsor-logos').cycle({timeout: 3000});
+
+    });
+})(jQuery)
+
index 1277bd2..fe19d30 100644 (file)
@@ -66,3 +66,8 @@
     background-color: #EEE;
     cursor: default;
 }
+
+.sponsors-sponsor img {
+    max-width: 120px;
+    max-height: 120px;
+}
index 9d490ae..b7f6993 100644 (file)
@@ -12,9 +12,7 @@
         <link rel="icon" href="{{ STATIC_URL }}img/favicon.png" type="image/png" />
         <link rel="search" type="application/opensearchdescription+xml" title="Wolne Lektury" href="{{ STATIC_URL }}opensearch.xml" />
         {% compressed_css "all" %}
-        <script type="text/javascript">var LANGUAGE_CODE = "{{ LANGUAGE_CODE }}";</script>
-        {% compressed_js "jquery" %}
-        {% compressed_js "all" %}
+
         {% block extrahead %}
         {% endblock %}
     </head>
 
         {% endblock bodycontent %}
 
+
+        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+        <script type="text/javascript">var LANGUAGE_CODE = "{{ LANGUAGE_CODE }}";</script>
+        {% compressed_js "base" %}
+
         <!--{{ piwik_tag|safe }}
         <script type="text/javascript">
         var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
index 4ef82e1..43ca2a5 100755 (executable)
@@ -1,8 +1,14 @@
 {% load i18n %}
+{% load thumbnail %}
 <div class="book-mini-box">
     <a href="{{ book.get_absolute_url }}">
-        <img src="{{ STATIC_URL }}img/cover.png"
-                alt="{% trans "Book cover" %}" />
+        <img src="
+            {% thumbnail book.cover "216x288" as thumb %}
+                {{ thumb.url }}
+            {% empty %}
+                {{ book.cover.url }}
+            {% endthumbnail %}
+        " alt="{% trans "Book cover" %}" />
         {% for author in authors %}
             <div class="mono author">{{ author }}</div>
             {{ book.title }}