Simplified login/register page for mobile devices.
[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 from django.shortcuts import get_object_or_404
5 from django.contrib.auth.decorators import login_required, user_passes_test
6 from piston.handler import BaseHandler
7 from piston.utils import rc, validate
8 from catalogue.models import Book, Tag
9 from catalogue.forms import BookImportForm
10 from catalogue.views import tagged_object_list 
11
12 class TagHandler(BaseHandler):
13    allowed_methods = ('GET',)
14    model = Tag   
15
16    def read(self, request, tags=''):
17       if tags == '':
18         return Tag.objects.all()
19       else:
20         return tagged_object_list(request, tags, api=True)
21
22 class BookHandler(BaseHandler):
23     model = Book
24     #fields = ('slug', 'title')
25
26     def read(self, request, slug=None):
27         if slug:
28             return get_object_or_404(Book, slug=slug)
29         else:
30             return Book.objects.all()
31
32 # old
33 """
34 staff_required = user_passes_test(lambda user: user.is_staff)
35
36 class BookHandler(BaseHandler):
37     model = Book
38     fields = ('slug', 'title')
39
40     @staff_required
41     def read(self, request, slug=None):
42         if slug:
43             return get_object_or_404(Book, slug=slug)
44         else:
45             return Book.objects.all()
46
47     @staff_required
48     def create(self, request):
49         form = BookImportForm(request.POST, request.FILES)
50         if form.is_valid():
51             form.save()
52             return rc.CREATED
53         else:
54             return rc.BAD_REQUEST
55 """