X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/f906193db2ec44590f57beae2e8ee2df76117b7b..6c071c6cbb739209e9d8488ff7080f8ad7c7d2d0:/apps/api/handlers.py diff --git a/apps/api/handlers.py b/apps/api/handlers.py index e10a4b5a2..d34806f0a 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -15,6 +15,8 @@ from api.helpers import timestamp from api.models import Deleted from catalogue.forms import BookImportForm from catalogue.models import Book, Tag, BookMedia, Fragment +from picture.models import Picture +from picture.forms import PictureImportForm from stats.utils import piwik_track @@ -249,9 +251,8 @@ class TagsHandler(BaseHandler): except KeyError, e: return rc.NOT_FOUND - tags = Tag.objects.filter(category=category_sng) - tags = [t for t in tags if t.get_count() > 0] - if tags: + tags = Tag.objects.filter(category=category_sng).exclude(book_count=0) + if tags.exists(): return tags else: return rc.NOT_FOUND @@ -518,7 +519,7 @@ class CatalogueHandler(BaseHandler): changed_at__gte=since, changed_at__lt=until): # only serve non-empty tags - if tag.get_count(): + if tag.book_count: tag_d = cls.tag_dict(tag, fields) updated.append(tag_d) elif tag.created_at < since: @@ -581,3 +582,21 @@ class ChangesHandler(CatalogueHandler): @piwik_track def read(self, request, since): return self.changes(request, since) + + +class PictureHandler(BaseHandler): + model = Picture + fields = ('slug', 'title') + allowed_methods = ('POST',) + + def create(self, request): + if not request.user.has_perm('picture.add_picture'): + return rc.FORBIDDEN + + data = json.loads(request.POST.get('data')) + form = PictureImportForm(data) + if form.is_valid(): + form.save() + return rc.CREATED + else: + return rc.NOT_FOUND