Permalink
Browse files

Example showing how staticmethods can be used to reduce chance of acc…

…identaly thread safety problems in class based generic views
  • Loading branch information...
1 parent 77dc629 commit 2fd44808077c02c343dcac90cac4fd2f2f18877c Simon Willison committed May 12, 2009
Showing with 25 additions and 0 deletions.
  1. +25 −0 example_rest_view.py
View
@@ -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.