Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example showing how staticmethods can be used to reduce chance of acc…
…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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |