public
Description: Turtles all the way down
Homepage: http://simonwillison.net/2009/May/19/djng/
Clone URL: git://github.com/simonw/djng.git
djng / example_rest_view.py
100644 26 lines (19 sloc) 0.672 kb
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
import djng
 
class RestView(object):
    def __call__(self, request, *args, **kwargs):
        method = request.method.upper()
        if hasattr(self, method):
            return getattr(self, method)(request, *args, **kwargs)
        return self.method_not_supported(request)
    
    @staticmethod
    def method_not_supported(request):
        return djng.Response('Method not supported')
    
 
class MyView(RestView):
    @staticmethod
    def GET(request):
        return djng.Response('This is a GET')
    
    @staticmethod
    def POST(request):
        return djng.Response('This is a POST')
 
if __name__ == '__main__':
    djng.serve(MyView(), '0.0.0.0', 8888)