X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/c7587605ff2117ff1004239f8ec0cbd024cdc4ab..d6cda2aae737f7c1f039695572f7bbc22bded124:/apps/api/handlers.py diff --git a/apps/api/handlers.py b/apps/api/handlers.py index 0255c05c5..260384d69 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -4,6 +4,7 @@ from datetime import datetime, timedelta import json +from urlparse import urljoin from django.conf import settings from django.contrib.sites.models import Site @@ -82,7 +83,7 @@ class BookMediaHandler(BaseHandler): """ Responsible for representing media in Books. """ model = BookMedia - fields = ['name', 'type', 'url'] + fields = ['name', 'type', 'url', 'artist', 'director'] @classmethod def url(cls, media): @@ -90,13 +91,22 @@ class BookMediaHandler(BaseHandler): return MEDIA_BASE + media.file.url + @classmethod + def artist(cls, media): + return media.get_extra_info_value().get('artist_name', '') + + @classmethod + def director(cls, media): + return media.get_extra_info_value().get('director_name', '') + + class BookDetails(object): """Custom fields used for representing Books.""" @classmethod def author(cls, book): - return ", ".join(t.name for t in book.tags.filter(category='author')) + return ",".join(t[0] for t in book.related_info()['tags']['author']) @classmethod def href(cls, book): @@ -111,16 +121,19 @@ class BookDetails(object): @classmethod def children(cls, book): - """ Returns all media for a book. """ + """ Returns all children for a book. """ return book.children.all() @classmethod def media(cls, book): """ Returns all media for a book. """ - return book.media.all() + @classmethod + def cover(cls, book): + return MEDIA_BASE + book.cover.url if book.cover else '' + class BookDetailHandler(BaseHandler, BookDetails): @@ -130,7 +143,8 @@ class BookDetailHandler(BaseHandler, BookDetails): """ allowed_methods = ['GET'] fields = ['title', 'parent', 'children'] + Book.formats + [ - 'media', 'url'] + book_tag_categories + 'media', 'url', 'cover'] + [ + category_plural[c] for c in book_tag_categories] @piwik_track def read(self, request, book): @@ -148,10 +162,11 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): """ allowed_methods = ('GET',) model = Book - fields = ['author', 'href', 'title', 'url'] + fields = ['author', 'href', 'title', 'url', 'cover'] @piwik_track - def read(self, request, tags, top_level=False): + def read(self, request, tags, top_level=False, + audiobooks=False, daisy=False): """ Lists all books with given tags. :param tags: filtering tags; should be a path of categories @@ -171,26 +186,32 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails): return books if books else rc.NOT_FOUND else: books = Book.tagged.with_all(tags) - elif top_level: - books = Book.objects.filter(parent=None) else: books = Book.objects.all() + + if top_level: + books = books.filter(parent=None) + if audiobooks: + books = books.filter(media__type='mp3') + if daisy: + books = books.filter(media__type='daisy') if books.exists(): return books else: return rc.NOT_FOUND - def create(self, request, tags, top_level=False): + def create(self, request, *args, **kwargs): return rc.FORBIDDEN class BooksHandler(BookDetailHandler): + allowed_methods = ('GET', 'POST') model = Book fields = ['author', 'href', 'title', 'url'] anonymous = AnonymousBooksHandler - def create(self, request, tags, top_level=False): + def create(self, request, *args, **kwargs): if not request.user.has_perm('catalogue.add_book'): return rc.FORBIDDEN