Created
April 10, 2014 22:45
-
-
Save amitittyerah/10429788 to your computer and use it in GitHub Desktop.
Decorators with arguments for neat ACL
This file contains hidden or 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
# decorators.py | |
def parameters_missing(request): | |
response_data = {} | |
response_data['status'] = False | |
response_data['message'] = 'Missing parameter' | |
return HttpResponse(json.dumps(response_data), content_type="application/json") | |
def open_request(request): | |
@csrf_exempt | |
def wrap(request, *args, **kwargs): | |
return function(request, *args, **kwargs) | |
wrap.__doc__ = function.__doc__ | |
wrap.__name__ = function.__name__ | |
return wrap | |
''' | |
The wrap method just needs to be wrapped inside another function | |
''' | |
def open_with_params(required_params): | |
def _wrapper(function): | |
# decorate the decorator | |
@open_request | |
def wrap(request, *args, **kwargs): | |
if set(request.POST).issuperset(required_params): | |
return function(request, *args, **kwargs) | |
else: | |
return parameters_missing(request) | |
wrap.__doc__ = function.__doc__ | |
wrap.__name__ = function.__name__ | |
return wrap | |
return _wrapper | |
# views.py | |
@open_with_params(["token", "access"]) | |
def add(request): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment