Login

Base class for RESTful Views

Author:
jpwatts
Posted:
November 30, 2008
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

Subclass Resource to create a view that will dispatch based on the HTTP method of the request.

class View(Request):
    def DELETE(self, request):
        ...

    def GET(self, request):
        ...

    def PUT(self, request):
        ...

Other snippets provided inspiration:

The code is also available on GitHub.

 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)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.