Created
July 7, 2010 20:24
-
-
Save scottferg/467220 to your computer and use it in GitHub Desktop.
This decorator will validate the request parameters sent to your view. Wrap the view with @required_parameter('someparam') to validate that 'someparam' was provided.
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
class required_parameter(object): | |
def __init__(self, paramName): | |
self.paramName = paramName | |
def __call__(self, function): | |
def error(param): | |
return HttpResponse('Required parameter %s not provided' % param) | |
def wrapped_f(*args): | |
if (self.paramName in args[0].REQUEST.keys() and args[0].REQUEST.get(self.paramName)): | |
return function(*args) | |
else: | |
return error(self.paramName) | |
return wrapped_f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment