Created
February 28, 2012 06:39
-
-
Save samos123/1930229 to your computer and use it in GitHub Desktop.
Check if the request.POST or request.GET contains the parameters it should have.
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
from functools import wraps | |
from django.http import HttpResponse | |
def json_response(dict_to_convert_to_json): | |
return HttpResponse(json.dumps(dict_to_convert_to_json), mimetype="application/json") | |
def required_parameters(parameters=('email', 'api_key'), http_method='POST'): | |
""" | |
Check if the required parameters are present in the request | |
@param parameters: The names of the parameters that should be supplied | |
@param http_method: Whether the parameters are present 'GET' or in 'POST' | |
""" | |
def inner_decorator(fn): | |
def wrapped(request, *args, **kwargs): | |
# check if the user api_key matches | |
for parameter in parameters: | |
if parameter not in getattr(request, http_method): | |
return json_response({'success': False, 'errors': 'Please use the Web API correctly and supply the parameter: '+parameter}) | |
# Proceed like normally with the request | |
return fn(request, *args, **kwargs) | |
return wraps(fn)(wrapped) | |
return inner_decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment