Created
November 12, 2015 22:26
-
-
Save chrisgeo/cfdb3d41aac98d07d6c1 to your computer and use it in GitHub Desktop.
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
""" | |
Helper utility for passing control to different views based on the HTTP method. | |
Construct instances of this class with a dictionary mapping method names (or | |
sequence of method names) to the view function that will handle them. The | |
special and optional '__default__' key is used to handle all otherwise | |
unmentioned methods. Methods not mentioned in the dictionary are implicitly | |
forbidden if there is no '__default__' key. | |
For example: | |
dispatch_dict = { | |
('POST', 'PUT') : new_data_handler, | |
'GET': display_only_handler | |
} | |
urlconf = patterns('', | |
url('/my/pattern/', MethodDispatcher(dispatch_dict)) | |
) | |
Each view function in the dictionary will be passed the same set of arguments, | |
so be prepared to use *args and/or **kwargs if you need to make the function | |
signatures uniform. | |
""" | |
import logging | |
from django import http | |
log = logging.getLogger(__name__) | |
class MethodDispatcher(object): | |
""" Allows Django URLs to pick a dispatch based on HTTP Headers """ | |
def __init__(self, method_map): | |
self.methods = {} | |
self.default = None | |
for key, value in method_map.items(): | |
if key == '__default__': | |
self.default = value | |
elif isinstance(key, basestring): | |
self.methods[key] = value | |
else: | |
for elt in key: | |
self.methods[elt] = value | |
def __call__(self, request, *args, **kwargs): | |
view_func = self.methods.get(request.method, self.default) | |
if view_func: | |
return view_func(request, *args, **kwargs) | |
return http.HttpResponseNotAllowed(self.methods.keys()) |
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
@csrf_exempt | |
@login_required | |
@validate_schema(comment_schema) | |
def add_comment(request): | |
""" Adds or updates a comment """ | |
return api_response({ | |
'status': HTTP_STATUS.OK, | |
'comments': [] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment