From 7628e1246f2c6c65fd359a20a1c57e79b279d0d4 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Wed, 30 May 2018 17:49:40 +0200 Subject: [PATCH] epub preview in api --- src/api/emitters.py | 8 ++++++++ src/api/handlers.py | 14 ++++++++++++++ src/api/urls.py | 4 ++++ 3 files changed, 26 insertions(+) diff --git a/src/api/emitters.py b/src/api/emitters.py index 36babb2bf..531ae19bb 100644 --- a/src/api/emitters.py +++ b/src/api/emitters.py @@ -69,3 +69,11 @@ class SsiXmlEmitter(SsiEmitterMixin, XMLEmitter): ''.join(self.construct().get_ssis('xml')) Emitter.register('xml', SsiXmlEmitter, 'text/xml; charset=utf-8') + + +# hack +class EpubEmitter(Emitter): + def render(self, request): + return self.data + +Emitter.register('epub', EpubEmitter, 'application/epub+zip') diff --git a/src/api/handlers.py b/src/api/handlers.py index 199f030ce..b37a6d9dc 100644 --- a/src/api/handlers.py +++ b/src/api/handlers.py @@ -6,6 +6,7 @@ import json from django.contrib.sites.models import Site from django.core.urlresolvers import reverse +from django.http.response import HttpResponse from django.utils.functional import lazy from django.db import models from piston.handler import AnonymousBaseHandler, BaseHandler @@ -15,6 +16,7 @@ from sorl.thumbnail import default from catalogue.forms import BookImportForm from catalogue.models import Book, Tag, BookMedia, Fragment, Collection from catalogue.models.tag import prefetch_relations +from catalogue.utils import is_subscribed from picture.models import Picture from picture.forms import PictureImportForm @@ -278,6 +280,18 @@ class BooksHandler(BookDetailHandler): return rc.NOT_FOUND +class EpubHandler(BookDetailHandler): + def read(self, request, slug): + if not is_subscribed(request.user): + return rc.FORBIDDEN + try: + book = Book.objects.get(slug=slug) + except Book.DoesNotExist: + return rc.NOT_FOUND + response = HttpResponse(book.get_media('epub')) + return response + + class EBooksHandler(AnonymousBooksHandler): fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats) + ('slug',) diff --git a/src/api/urls.py b/src/api/urls.py index 29e7a752f..28dc51c13 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -20,6 +20,7 @@ ebook_list_resource = Resource(handler=handlers.EBooksHandler) # book_list_resource = Resource(handler=handlers.BooksHandler) book_resource = Resource(handler=handlers.BookDetailHandler) filter_book_resource = Resource(handler=handlers.FilterBooksHandler) +epub_resource = Resource(handler=handlers.EpubHandler) collection_resource = Resource(handler=handlers.CollectionDetailHandler) collection_list_resource = Resource(handler=handlers.CollectionsHandler) @@ -69,6 +70,9 @@ urlpatterns = [ url(r'^collections/$', collection_list_resource, name="api_collections"), url(r'^collections/(?P[^/]+)/$', collection_resource, name="api_collection"), + # epub preview + url(r'^epub/(?P[a-z0-9-]+)/$', epub_resource, name='api_epub'), + # objects details url(r'^books/(?P[a-z0-9-]+)/$', book_resource, name="api_book"), url(r'^(?P[a-z0-9-]+)/(?P[a-z0-9-]+)/$', -- 2.20.1