change workshop form title
[wolnelektury.git] / src / api / handlers.py
index 5fe931e..27477b3 100644 (file)
@@ -13,12 +13,13 @@ from sorl.thumbnail import default
 
 from catalogue.forms import BookImportForm
 from catalogue.models import Book, Tag, BookMedia, Fragment, Collection
 
 from catalogue.forms import BookImportForm
 from catalogue.models import Book, Tag, BookMedia, Fragment, Collection
+from catalogue.models.tag import prefetch_relations
 from picture.models import Picture
 from picture.forms import PictureImportForm
 
 from stats.utils import piwik_track
 
 from picture.models import Picture
 from picture.forms import PictureImportForm
 
 from stats.utils import piwik_track
 
-from . import emitters # Register our emitters
+from . import emitters  # Register our emitters
 
 API_BASE = WL_BASE = MEDIA_BASE = lazy(
     lambda: u'http://' + Site.objects.get_current().domain, unicode)()
 
 API_BASE = WL_BASE = MEDIA_BASE = lazy(
     lambda: u'http://' + Site.objects.get_current().domain, unicode)()
@@ -39,7 +40,6 @@ for k, v in category_singular.items():
 book_tag_categories = ['author', 'epoch', 'kind', 'genre']
 
 
 book_tag_categories = ['author', 'epoch', 'kind', 'genre']
 
 
-
 def read_tags(tags, allowed):
     """ Reads a path of filtering tags.
 
 def read_tags(tags, allowed):
     """ Reads a path of filtering tags.
 
@@ -62,7 +62,7 @@ def read_tags(tags, allowed):
         except KeyError:
             raise ValueError('Unknown category.')
 
         except KeyError:
             raise ValueError('Unknown category.')
 
-        if not category in allowed:
+        if category not in allowed:
             raise ValueError('Category not allowed.')
 
         if category == 'book':
             raise ValueError('Category not allowed.')
 
         if category == 'book':
@@ -137,7 +137,6 @@ class BookDetails(object):
                     book.cover, "139x193").url if book.cover else ''
 
 
                     book.cover, "139x193").url if book.cover else ''
 
 
-
 class BookDetailHandler(BaseHandler, BookDetails):
     """ Main handler for Book objects.
 
 class BookDetailHandler(BaseHandler, BookDetails):
     """ Main handler for Book objects.
 
@@ -164,7 +163,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
     """
     allowed_methods = ('GET',)
     model = Book
     """
     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):
 
     @classmethod
     def genres(cls, book):
@@ -172,8 +171,9 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
         return book.tags.filter(category='genre')
 
     @piwik_track
         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, books=None,
+             after=None, before=None, count=None):
         """ Lists all books with given tags.
 
         :param tags: filtering tags; should be a path of categories
         """ Lists all books with given tags.
 
         :param tags: filtering tags; should be a path of categories
@@ -200,7 +200,8 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
             else:
                 books = Book.tagged.with_all(tags)
         else:
             else:
                 books = Book.tagged.with_all(tags)
         else:
-            books = Book.objects.all()
+            books = books if books is not None else Book.objects.all()
+        books = books.order_by('slug')
 
         if top_level:
             books = books.filter(parent=None)
 
         if top_level:
             books = books.filter(parent=None)
@@ -208,8 +209,27 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
             books = books.filter(media__type='mp3').distinct()
         if daisy:
             books = books.filter(media__type='daisy').distinct()
             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.exists():
+        if books:
             return books
         else:
             return rc.NOT_FOUND
             return books
         else:
             return rc.NOT_FOUND
@@ -221,7 +241,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler, BookDetails):
 class BooksHandler(BookDetailHandler):
     allowed_methods = ('GET', 'POST')
     model = Book
 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):
     anonymous = AnonymousBooksHandler
 
     def create(self, request, *args, **kwargs):
@@ -238,7 +258,35 @@ class BooksHandler(BookDetailHandler):
 
 
 class EBooksHandler(AnonymousBooksHandler):
 
 
 class EBooksHandler(AnonymousBooksHandler):
-    fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats)
+    fields = ('author', 'href', 'title', 'cover') + tuple(Book.ebook_formats) + ('slug',)
+
+
+class FilterBooksHandler(AnonymousBooksHandler):
+    fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'slug']
+
+    def read(self, request, title_part=None, author_part=None, is_lektura=None, is_audiobook=None,
+             after=None, before=None, count=None):
+        if count is None:
+            count = 50
+        if is_lektura in ('true', 'false'):
+            is_lektura = is_lektura == 'true'
+        else:
+            is_lektura = None
+        if is_audiobook in ('true', 'false'):
+            is_audiobook = is_audiobook == 'true'
+        books = Book.objects.distinct()
+        if title_part:
+            books = books.filter(title__iregex='\m' + title_part)
+        if author_part is not None:
+            books = books.filter(cached_author__iregex='\m' + author_part)
+        if is_lektura is not None:
+            books = books.filter(has_audience=is_lektura)
+        if is_audiobook is not None:
+            if is_audiobook:
+                books = books.filter(media__type='mp3')
+            else:
+                books = books.exclude(media__type='mp3')
+        return super(FilterBooksHandler, self).read(request, books=books, after=after, before=before, count=count)
 
 
 # add categorized tags fields for Book
 
 
 # add categorized tags fields for Book
@@ -247,18 +295,27 @@ def _tags_getter(category):
     def get_tags(cls, book):
         return book.tags.filter(category=category)
     return get_tags
     def get_tags(cls, book):
         return book.tags.filter(category=category)
     return get_tags
+
+
 def _tag_getter(category):
     @classmethod
     def get_tag(cls, book):
 def _tag_getter(category):
     @classmethod
     def get_tag(cls, book):
-        return ', '.join(tag.name for tag in book.tags.filter(category=category))
+        return book.tag_unicode(category)
     return get_tag
     return get_tag
-for plural, singular in category_singular.items():
-    setattr(BookDetails, plural, _tags_getter(singular))
-    setattr(BookDetails, singular, _tag_getter(singular))
+
+
+def add_tag_getters():
+    for plural, singular in category_singular.items():
+        setattr(BookDetails, plural, _tags_getter(singular))
+        setattr(BookDetails, singular, _tag_getter(singular))
+
+add_tag_getters()
+
 
 # add fields for files in Book
 
 # add fields for files in Book
-def _file_getter(format):
-    field = "%s_file" % format
+def _file_getter(book_format):
+    field = "%s_file" % book_format
+
     @classmethod
     def get_file(cls, book):
         f = getattr(book, field)
     @classmethod
     def get_file(cls, book):
         f = getattr(book, field)
@@ -267,8 +324,13 @@ def _file_getter(format):
         else:
             return ''
     return get_file
         else:
             return ''
     return get_file
-for format in Book.formats:
-    setattr(BookDetails, format, _file_getter(format))
+
+
+def add_file_getters():
+    for book_format in Book.formats:
+        setattr(BookDetails, book_format, _file_getter(book_format))
+
+add_file_getters()
 
 
 class CollectionDetails(object):
 
 
 class CollectionDetails(object):
@@ -291,7 +353,6 @@ class CollectionDetails(object):
         return Book.objects.filter(collection.get_query())
 
 
         return Book.objects.filter(collection.get_query())
 
 
-
 class CollectionDetailHandler(BaseHandler, CollectionDetails):
     allowed_methods = ('GET',)
     fields = ['url', 'title', 'description', 'books']
 class CollectionDetailHandler(BaseHandler, CollectionDetails):
     allowed_methods = ('GET',)
     fields = ['url', 'title', 'description', 'books']
@@ -343,7 +404,7 @@ class TagDetailHandler(BaseHandler, TagDetails):
 
         try:
             category_sng = category_singular[category]
 
         try:
             category_sng = category_singular[category]
-        except KeyError, e:
+        except KeyError:
             return rc.NOT_FOUND
 
         try:
             return rc.NOT_FOUND
 
         try:
@@ -374,7 +435,7 @@ class TagsHandler(BaseHandler, TagDetails):
 
         try:
             category_sng = category_singular[category]
 
         try:
             category_sng = category_singular[category]
-        except KeyError, e:
+        except KeyError:
             return rc.NOT_FOUND
 
         tags = Tag.objects.filter(category=category_sng).exclude(items=None)
             return rc.NOT_FOUND
 
         tags = Tag.objects.filter(category=category_sng).exclude(items=None)
@@ -391,8 +452,7 @@ class FragmentDetails(object):
     def href(cls, fragment):
         """ Returns URI in the API for the fragment. """
 
     def href(cls, fragment):
         """ Returns URI in the API for the fragment. """
 
-        return API_BASE + reverse("api_fragment",
-            args=[fragment.book.slug, fragment.anchor])
+        return API_BASE + reverse("api_fragment", args=[fragment.book.slug, fragment.anchor])
 
     @classmethod
     def url(cls, fragment):
 
     @classmethod
     def url(cls, fragment):
@@ -430,7 +490,7 @@ class FragmentsHandler(BaseHandler, FragmentDetails):
     fields = ['book', 'url', 'anchor', 'href']
     allowed_methods = ('GET',)
 
     fields = ['book', 'url', 'anchor', 'href']
     allowed_methods = ('GET',)
 
-    categories = set(['author', 'epoch', 'kind', 'genre', 'book', 'theme'])
+    categories = {'author', 'epoch', 'kind', 'genre', 'book', 'theme'}
 
     @piwik_track
     def read(self, request, tags):
 
     @piwik_track
     def read(self, request, tags):