/
views.py
142 lines (102 loc) · 3.79 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import base64
from restless.views import Endpoint
from restless.models import serialize
from restless.http import Http201, Http403, Http404, Http400, HttpError
from restless.auth import (AuthenticateEndpoint, BasicHttpAuthMixin,
login_required)
from restless.modelviews import ListEndpoint, DetailEndpoint, ActionEndpoint
from .models import *
from .forms import *
__all__ = ['AuthorList', 'AuthorDetail', 'FailsIntentionally', 'TestLogin',
'TestBasicAuth', 'WildcardHandler', 'EchoView', 'ErrorRaisingView',
'PublisherAutoList', 'PublisherAutoDetail', 'ReadOnlyPublisherAutoList',
'PublisherAction', 'BookDetail', 'TestCustomAuthMethod']
class AuthorList(Endpoint):
def get(self, request):
return serialize(Author.objects.all())
def post(self, request):
form = AuthorForm(request.data)
if form.is_valid():
author = form.save()
return Http201(serialize(author))
else:
return Http400(reason='invalid author data',
details=form.errors)
class AuthorDetail(Endpoint):
def get(self, request, author_id=None):
author_id = int(author_id)
try:
return serialize(Author.objects.get(id=author_id))
except Author.DoesNotExist:
return Http404(reason='no such author')
def delete(self, request, author_id=None):
author_id = int(author_id)
Author.objects.get(id=author_id).delete()
return 'ok'
def put(self, request, author_id=None):
author_id = int(author_id)
try:
author = Author.objects.get(id=author_id)
except Author.DoesNotExist:
return Http404(reason='no such author')
form = AuthorForm(request.data, instance=author)
if form.is_valid():
author = form.save()
return serialize(author)
else:
return Http400(reason='invalid author data',
details=form.errors)
class FailsIntentionally(Endpoint):
def get(self, request):
raise Exception("I'm being a bad view")
class TestLogin(AuthenticateEndpoint):
pass
class TestBasicAuth(Endpoint, BasicHttpAuthMixin):
@login_required
def get(self, request):
return serialize(request.user)
class TestCustomAuthMethod(Endpoint):
def authenticate(self, request):
user = request.params.get('user')
if user == 'friend':
return None
elif user == 'foe':
return Http403('you shall not pass')
elif user == 'exceptional-foe':
raise HttpError(403, 'with exception')
else:
# this is an illegal return value for this function
return 42
def get(self, request):
return 'OK'
class WildcardHandler(Endpoint):
def dispatch(self, request, *args, **kwargs):
return Http404('no such resource: %s %s' % (
request.method, request.path))
class EchoView(Endpoint):
def post(self, request):
return {
'headers': dict((k, str(v)) for k, v in request.META.items()),
'raw_data': base64.b64encode(request.raw_data).decode('ascii')
}
def get(self, request):
return self.post(request)
def put(self, request):
return self.post(request)
class ErrorRaisingView(Endpoint):
def get(self, request):
raise HttpError(400, 'raised error', extra_data='foo')
class PublisherAutoList(ListEndpoint):
model = Publisher
class PublisherAutoDetail(DetailEndpoint):
model = Publisher
class ReadOnlyPublisherAutoList(ListEndpoint):
model = Publisher
methods = ['GET']
class PublisherAction(ActionEndpoint):
model = Publisher
def action(self, obj, *args, **kwargs):
return {'result': 'done'}
class BookDetail(DetailEndpoint):
model = Book
lookup_field = 'isbn'