03115d894f58f698b8fa8b544fc2f86c206aa778
[wolnelektury.git] / apps / api / handlers.py
1 from django.shortcuts import get_object_or_404
2 from django.contrib.auth.decorators import login_required, user_passes_test
3 from piston.handler import BaseHandler
4 from piston.utils import rc, validate
5 from catalogue.models import Book
6 from catalogue.forms import BookImportForm
7
8
9 staff_required = user_passes_test(lambda user: user.is_staff)
10
11
12 class BookHandler(BaseHandler):
13     model = Book
14     fields = ('slug', 'title')
15     
16     @staff_required
17     def read(self, request, slug=None):
18         if slug:
19             return get_object_or_404(Book, slug=slug)
20         else:
21             return Book.objects.all()
22     
23     @staff_required
24     def create(self, request):
25         form = BookImportForm(request.POST, request.FILES)
26         if form.is_valid():
27             form.save()
28             return rc.CREATED
29         else:
30             return rc.BAD_REQUEST
31