From 963c137c837058edfa66e97c49682e2d0f437923 Mon Sep 17 00:00:00 2001
From: Radek Czajka
Date: Wed, 21 Mar 2012 10:31:30 +0100
Subject: [PATCH] moar api
---
apps/api/handlers.py | 30 +++++++++++++++++++++++-------
apps/api/templates/api/main.html | 22 ++++++++++++++++------
apps/api/urls.py | 12 +++++++++---
3 files changed, 48 insertions(+), 16 deletions(-)
diff --git a/apps/api/handlers.py b/apps/api/handlers.py
index 98a57426c..260384d69 100644
--- a/apps/api/handlers.py
+++ b/apps/api/handlers.py
@@ -83,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):
@@ -91,6 +91,15 @@ 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."""
@@ -134,7 +143,8 @@ class BookDetailHandler(BaseHandler, BookDetails):
"""
allowed_methods = ['GET']
fields = ['title', 'parent', 'children'] + Book.formats + [
- 'media', 'url', 'cover'] + book_tag_categories
+ 'media', 'url', 'cover'] + [
+ category_plural[c] for c in book_tag_categories]
@piwik_track
def read(self, request, book):
@@ -155,7 +165,8 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
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
@@ -175,17 +186,22 @@ 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
@@ -195,7 +211,7 @@ class BooksHandler(BookDetailHandler):
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
diff --git a/apps/api/templates/api/main.html b/apps/api/templates/api/main.html
index 8b6805945..5dd8229f8 100755
--- a/apps/api/templates/api/main.html
+++ b/apps/api/templates/api/main.html
@@ -37,14 +37,24 @@ The URLs in WolneLektury.pl API are:
diff --git a/apps/api/urls.py b/apps/api/urls.py
index 7b96337e8..a22f3b772 100644
--- a/apps/api/urls.py
+++ b/apps/api/urls.py
@@ -54,8 +54,14 @@ urlpatterns = patterns(
fragment_resource, name="api_fragment"),
# books by tags
- url(r'^(?P(?:(?:[a-z0-9-]+/){2}){0,6})books/$', book_list_resource),
- url(r'^(?P(?:(?:[a-z0-9-]+/){2}){0,6})parent_books/$', book_list_resource, {"top_level": True}),
+ url(r'^(?P(?:(?:[a-z0-9-]+/){2}){0,6})books/$',
+ book_list_resource, name='api_book_list'),
+ url(r'^(?P(?:(?:[a-z0-9-]+/){2}){0,6})parent_books/$',
+ book_list_resource, {"top_level": True}, name='api_parent_book_list'),
+ url(r'^(?P(?:(?:[a-z0-9-]+/){2}){0,6})audiobooks/$',
+ book_list_resource, {"audiobooks": True}, name='api_audiobook_list'),
+ url(r'^(?P(?:(?:[a-z0-9-]+/){2}){0,6})daisy/$',
+ book_list_resource, {"daisy": True}, name='api_daisy_list'),
url(r'^pictures/$', picture_resource),
@@ -64,5 +70,5 @@ urlpatterns = patterns(
url(r'^(?P(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),
# tags by category
- url(r'^(?P[a-z0-9-]+)/$', tag_list_resource),
+ url(r'^(?P[a-z0-9-]+)/$', tag_list_resource, name='api_tag_list'),
)
--
2.20.1