New design for book-detail template (downloading books etc).
[wolnelektury.git] / apps / api / handlers.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.shortcuts import get_object_or_404
6 from django.contrib.auth.decorators import login_required, user_passes_test
7 from piston.handler import BaseHandler
8 from piston.utils import rc, validate
9 from catalogue.models import Book
10 from catalogue.forms import BookImportForm
11
12
13 staff_required = user_passes_test(lambda user: user.is_staff)
14
15
16 class BookHandler(BaseHandler):
17     model = Book
18     fields = ('slug', 'title')
19
20     @staff_required
21     def read(self, request, slug=None):
22         if slug:
23             return get_object_or_404(Book, slug=slug)
24         else:
25             return Book.objects.all()
26
27     @staff_required
28     def create(self, request):
29         form = BookImportForm(request.POST, request.FILES)
30         if form.is_valid():
31             form.save()
32             return rc.CREATED
33         else:
34             return rc.BAD_REQUEST
35