add to api: keyset pagination, recommended, newest
authorJan Szejko <janek37@gmail.com>
Wed, 25 Oct 2017 14:10:40 +0000 (16:10 +0200)
committerJan Szejko <janek37@gmail.com>
Wed, 25 Oct 2017 14:27:14 +0000 (16:27 +0200)
src/api/handlers.py
src/api/urls.py

index 827cd7c..6b1d808 100644 (file)
@@ -163,7 +163,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
     """
     allowed_methods = ('GET',)
     model = Book
-    fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb']
+    fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug']
 
     @classmethod
     def genres(cls, book):
@@ -171,7 +171,9 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
         return book.tags.filter(category='genre')
 
     @piwik_track
-    def read(self, request, tags=None, top_level=False, audiobooks=False, daisy=False, pk=None):
+    def read(self, request, tags=None, top_level=False, audiobooks=False, daisy=False, pk=None,
+             recommended=False, newest=False,
+             after=None, before=None, count=None):
         """ Lists all books with given tags.
 
         :param tags: filtering tags; should be a path of categories
@@ -199,6 +201,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
                 books = Book.tagged.with_all(tags)
         else:
             books = Book.objects.all()
+        books = books.order_by('slug')
 
         if top_level:
             books = books.filter(parent=None)
@@ -206,10 +209,26 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
             books = books.filter(media__type='mp3').distinct()
         if daisy:
             books = books.filter(media__type='daisy').distinct()
+        if recommended:
+            books = books.filter(recommended=True)
+        if newest:
+            books = books.order_by('-created_at')
+
+        if after:
+            books = books.filter(slug__gt=after)
+        if before:
+            books = books.filter(slug__lt=before)
 
         books = books.only('slug', 'title', 'cover', 'cover_thumb')
         for category in book_tag_categories:
             books = prefetch_relations(books, category)
+
+        if count:
+            if before:
+                books = list(reversed(books.order_by('-slug')[:count]))
+            else:
+                books = books[:count]
+
         if books:
             return books
         else:
@@ -222,7 +241,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
 class BooksHandler(BookDetailHandler):
     allowed_methods = ('GET', 'POST')
     model = Book
-    fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb']
+    fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug']
     anonymous = AnonymousBooksHandler
 
     def create(self, request, *args, **kwargs):
@@ -239,7 +258,7 @@ class BooksHandler(BookDetailHandler):
 
 
 class EBooksHandler(AnonymousBooksHandler):
-    fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats)
+    fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats) + ('slug',)
 
 
 # add categorized tags fields for Book
index 4452aa2..7d98463 100644 (file)
@@ -30,6 +30,10 @@ fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
 picture_resource = CsrfExemptResource(handler=handlers.PictureHandler, authentication=auth)
 
 
+tags_re = r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})'
+paginate_re = r'(?:before/(?P<before>[a-z0-9-]+)/)?(?:after/(?P<after>[a-z0-9-]+)/)?(?:count/(?P<count>[0-9]+)/)?$'
+
+
 @ssi_included
 def incl(request, model, pk, emitter_format):
     resource = {
@@ -73,19 +77,22 @@ urlpatterns = patterns(
         fragment_resource, name="api_fragment"),
 
     # books by tags
-    url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})books/$',
+    url(tags_re + r'books/' + paginate_re,
         book_list_resource, name='api_book_list'),
-    url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})ebooks/$',
+    url(tags_re + r'ebooks/' + paginate_re,
         ebook_list_resource, name='api_ebook_list'),
-    url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})parent_books/$',
+    url(tags_re + r'parent_books/' + paginate_re,
         book_list_resource, {"top_level": True}, name='api_parent_book_list'),
-    url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})parent_ebooks/$',
+    url(tags_re + r'parent_ebooks/' + paginate_re,
         ebook_list_resource, {"top_level": True}, name='api_parent_ebook_list'),
-    url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})audiobooks/$',
+    url(tags_re + r'audiobooks/' + paginate_re,
         book_list_resource, {"audiobooks": True}, name='api_audiobook_list'),
-    url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})daisy/$',
+    url(tags_re + r'daisy/' + paginate_re,
         book_list_resource, {"daisy": True}, name='api_daisy_list'),
 
+    url(r'^recommended/' + paginate_re, book_list_resource, {"recommended": True}, name='api_recommended_list'),
+    url(r'^newest/', book_list_resource, {"newest": True, "count": 20}, name='api_newest_list'),
+
     url(r'^pictures/$', picture_resource),
 
     # fragments by book, tags, themes