X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/5a15f6f4162ddda647b1ca9eec4e36f85f457056..6bea6e8c1db99c6ed22cdc010ec2552fd473a649:/apps/api/handlers.py?ds=sidebyside diff --git a/apps/api/handlers.py b/apps/api/handlers.py index 32a3ce300..508659e75 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -135,11 +135,23 @@ class BooksHandler(BaseHandler): tags = read_tags(tags, allowed=self.categories) if tags: if top_level: - return Book.tagged_top_level(tags) + books = Book.tagged_top_level(tags) + return books if books else rc.NOT_FOUND else: - return Book.tagged.with_all(tags) + books = Book.tagged.with_all(tags) else: - return Book.objects.all() + books = Book.objects.all() + + if books.exists(): + return books + else: + return rc.NOT_FOUND + + @classmethod + def media(self, book): + """ Returns all media for a book. """ + + return book.media.all() # add categorized tags fields for Book @@ -201,7 +213,13 @@ class TagsHandler(BaseHandler): except KeyError, e: return rc.NOT_FOUND - return Tag.objects.filter(category=category_sng) + tags = Tag.objects.filter(category=category_sng) + tags = [t for t in tags if t.get_count() > 0] + if tags: + return tags + else: + return rc.NOT_FOUND + @classmethod def href(cls, tag): @@ -240,7 +258,11 @@ class FragmentsHandler(BaseHandler): """ tags = read_tags(tags, allowed=self.categories) - return Fragment.tagged.with_all(tags).select_related('book') + fragments = Fragment.tagged.with_all(tags).select_related('book') + if fragments.exists(): + return fragments + else: + return rc.NOT_FOUND @classmethod def href(cls, fragment):