epub preview in api
authorJan Szejko <janek37@gmail.com>
Wed, 30 May 2018 15:49:40 +0000 (17:49 +0200)
committerJan Szejko <janek37@gmail.com>
Wed, 30 May 2018 15:49:40 +0000 (17:49 +0200)
src/api/emitters.py
src/api/handlers.py
src/api/urls.py

index 36babb2..531ae19 100644 (file)
@@ -69,3 +69,11 @@ class SsiXmlEmitter(SsiEmitterMixin, XMLEmitter):
                 '</resource><resource>'.join(self.construct().get_ssis('xml'))
 
 Emitter.register('xml', SsiXmlEmitter, 'text/xml; charset=utf-8')
                 '</resource><resource>'.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')
index 199f030..b37a6d9 100644 (file)
@@ -6,6 +6,7 @@ import json
 
 from django.contrib.sites.models import Site
 from django.core.urlresolvers import reverse
 
 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
 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.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
 
 from picture.models import Picture
 from picture.forms import PictureImportForm
 
@@ -278,6 +280,18 @@ class BooksHandler(BookDetailHandler):
             return rc.NOT_FOUND
 
 
             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',)
 
 class EBooksHandler(AnonymousBooksHandler):
     fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats) + ('slug',)
 
index 29e7a75..28dc51c 100644 (file)
@@ -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)
 # 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)
 
 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<slug>[^/]+)/$', collection_resource, name="api_collection"),
 
     url(r'^collections/$', collection_list_resource, name="api_collections"),
     url(r'^collections/(?P<slug>[^/]+)/$', collection_resource, name="api_collection"),
 
+    # epub preview
+    url(r'^epub/(?P<slug>[a-z0-9-]+)/$', epub_resource, name='api_epub'),
+
     # objects details
     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),
     url(r'^(?P<category>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
     # objects details
     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),
     url(r'^(?P<category>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',