-
Notifications
You must be signed in to change notification settings - Fork 1
/
restviews.py
86 lines (66 loc) · 2.03 KB
/
restviews.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
"""RESTful Views for Django"""
from django import http
__all__ = ('Resource',)
# http://www.ietf.org/rfc/rfc2616
METHODS = """OPTIONS
GET
HEAD
POST
PUT
DELETE
TRACE
CONNECT""".split()
def _default_HEAD(self, request, *args, **kwargs):
response = self.GET(request, *args, **kwargs)
response.content = ''
return response
class ResourceBase(type):
def __new__(cls, name, bases, attrs):
if 'GET' in attrs:
attrs.setdefault('HEAD', _default_HEAD)
attrs['allow'] = sorted(k for k in attrs if k in METHODS)
return super(ResourceBase, cls).__new__(cls, name, bases, attrs)
class Resource(object):
"""A base class for creating RESTful Django views.
When called, dispatch based on the value of ``request.method``. If
there isn't a handler for the method, return an HTTP 405.
>>> class View(Resource):
... def DELETE(self, request):
... return http.HttpResponse('', status=204)
...
... def GET(self, request):
... return http.HttpResponse('Resource')
>>> request = http.HttpRequest()
>>> view = View()
>>> request.method = 'DELETE'
>>> response = view(request)
>>> response.content
''
>>> response.status_code
204
>>> request.method = 'GET'
>>> response = view(request)
>>> response.content
'Resource'
>>> response.status_code
200
>>> request.method = 'HEAD'
>>> response = view(request)
>>> response.content
''
>>> response.status_code
200
>>> request.method = 'PUT'
>>> response = view(request)
>>> response.content
''
>>> response.status_code
405
>>> response['Allow']
'DELETE, GET, HEAD'
"""
__metaclass__ = ResourceBase
def __call__(self, request, *args, **kwargs):
if request.method not in self.allow:
return http.HttpResponseNotAllowed(self.allow)
return getattr(self, request.method)(request, *args, **kwargs)