Skip to content

Instantly share code, notes, and snippets.

@scottferg
Created July 7, 2010 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottferg/467220 to your computer and use it in GitHub Desktop.
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.
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