Skip to content

Commit

Permalink
Example showing how staticmethods can be used to reduce chance of acc…
Browse files Browse the repository at this point in the history
…identaly thread safety problems in class based generic views
  • Loading branch information
Simon Willison committed May 12, 2009
1 parent 77dc629 commit 2fd4480
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions example_rest_view.py
@@ -0,0 +1,25 @@
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)

0 comments on commit 2fd4480

Please sign in to comment.