From: Jan Szejko Date: Thu, 21 Dec 2017 13:23:54 +0000 (+0100) Subject: cover thumb with no box for api X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/7255df395ced6145a654fb6cfc1e673000008a25 cover thumb with no box for api --- diff --git a/lib/librarian b/lib/librarian index fa42894ec..f4fdb08e0 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit fa42894ec06a78183d736608613c9a96fec2c400 +Subproject commit f4fdb08e0b0ff0da9faed1ac5d89a1523cad7271 diff --git a/src/api/handlers.py b/src/api/handlers.py index a1e9f6a23..f7b30864d 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -145,6 +145,10 @@ class BookDetails(object): return MEDIA_BASE + default.backend.get_thumbnail( book.cover, "139x193").url if book.cover else '' + @classmethod + def simple_thumb(cls, book): + return MEDIA_BASE + book.cover_api_thumb.url if book.cover_api_thumb else '' + @classmethod def cover_source_image(cls, book): url = book.cover_source() @@ -158,7 +162,7 @@ class BookDetailHandler(BaseHandler, BookDetails): """ allowed_methods = ['GET'] fields = ['title', 'parent', 'children'] + Book.formats + [ - 'media', 'url', 'cover', 'cover_thumb', 'fragment_data'] + [ + 'media', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'fragment_data'] + [ category_plural[c] for c in book_tag_categories] @piwik_track @@ -177,7 +181,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): """ allowed_methods = ('GET',) model = Book - fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug'] + fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug', 'simple_thumb'] @classmethod def genres(cls, book): @@ -301,7 +305,7 @@ class QuerySetProxy(models.QuerySet): class FilterBooksHandler(AnonymousBooksHandler): fields = book_tag_categories + [ - 'href', 'title', 'url', 'cover', 'cover_thumb', 'slug', 'key', 'cover_source_image'] + 'href', 'title', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'slug', 'key', 'cover_source_image'] def read(self, request): key_sep = '$' diff --git a/src/catalogue/fields.py b/src/catalogue/fields.py index 372cf2735..8b28f1c43 100644 --- a/src/catalogue/fields.py +++ b/src/catalogue/fields.py @@ -244,6 +244,15 @@ class BuildCoverThumb(BuildEbook): return WLCover(wldoc.book_info, height=193).output_file() +@BuildEbook.register('cover_api_thumb') +@task(ignore_result=True) +class BuildCoverApiThumb(BuildEbook): + @classmethod + def transform(cls, wldoc, fieldfile): + from librarian.cover import WLNoBoxCover + return WLNoBoxCover(wldoc.book_info, height=500).output_file() + + # not used, but needed for migrations class OverwritingFieldFile(FieldFile): """ diff --git a/src/catalogue/migrations/0020_book_cover_api_thumb.py b/src/catalogue/migrations/0020_book_cover_api_thumb.py new file mode 100644 index 000000000..45db58d13 --- /dev/null +++ b/src/catalogue/migrations/0020_book_cover_api_thumb.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import catalogue.fields +import catalogue.models.book + + +class Migration(migrations.Migration): + + dependencies = [ + ('catalogue', '0019_auto_20171221_1107'), + ] + + operations = [ + migrations.AddField( + model_name='book', + name='cover_api_thumb', + field=catalogue.fields.EbookField(b'cover_api_thumb', max_length=255, upload_to=catalogue.models.book.UploadToPath(b'book/cover_api_thumb/%s.jpg'), null=True, verbose_name='cover thumbnail for API', blank=True), + ), + ] diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 44ee02ecc..cdb110045 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -14,6 +14,7 @@ import django.dispatch from django.contrib.contenttypes.fields import GenericRelation from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _, get_language +from django.utils.deconstruct import deconstructible import jsonfield from fnpdjango.storage import BofhFileSystemStorage from ssify import flush_ssi_includes @@ -30,18 +31,22 @@ from wolnelektury.utils import makedirs bofh_storage = BofhFileSystemStorage() -def _make_upload_to(path): - def _upload_to(i, n): - return path % i.slug - return _upload_to +@deconstructible +class UploadToPath(object): + def __init__(self, path): + self.path = path + def __call__(self, instance, filename): + return self.path % instance.slug -_cover_upload_to = _make_upload_to('book/cover/%s.jpg') -_cover_thumb_upload_to = _make_upload_to('book/cover_thumb/%s.jpg') + +_cover_upload_to = UploadToPath('book/cover/%s.jpg') +_cover_thumb_upload_to = UploadToPath('book/cover_thumb/%s.jpg') +_cover_api_thumb_opload_to = UploadToPath('book/cover_api_thumb/%s.jpg') def _ebook_upload_to(upload_path): - return _make_upload_to(upload_path) + return UploadToPath(upload_path) class Book(models.Model): @@ -75,6 +80,11 @@ class Book(models.Model): null=True, blank=True, upload_to=_cover_thumb_upload_to, max_length=255) + cover_api_thumb = EbookField( + 'cover_api_thumb', _('cover thumbnail for API'), + null=True, blank=True, + upload_to=_cover_api_thumb_opload_to, + max_length=255) ebook_formats = constants.EBOOK_FORMATS formats = ebook_formats + ['html', 'xml'] @@ -429,6 +439,7 @@ class Book(models.Model): if 'cover' not in dont_build: book.cover.build_delay() book.cover_thumb.build_delay() + book.cover_api_thumb.build_delay() # Build HTML and ebooks. book.html_file.build_delay() @@ -531,6 +542,7 @@ class Book(models.Model): if 'cover' not in app_settings.DONT_BUILD: self.cover.build_delay() self.cover_thumb.build_delay() + self.cover_api_thumb.build_delay() for format_ in constants.EBOOK_FORMATS_WITH_COVERS: if format_ not in app_settings.DONT_BUILD: getattr(self, '%s_file' % format_).build_delay()