my deorator function...
def validate_captcha(view): '''decorator validate captcha based on settings''' def failure(): return httpresponse('you need complete captcha, please refresh , try again') if request.method == 'post': url = "https://www.google.com/recaptcha/api/siteverify" values = { 'secret': settings.google_recaptcha_secret_key, 'response': request.post.get(u'g-recaptcha-response', none), 'remoteip': request.meta.get("remote_addr", none), } data = urllib.urlencode(values) req = urllib2.request(url, data) response = urllib2.urlopen(req) result = json.loads(response.read()) # result["success"] true on success if result["success"]: return view else: return fail return fail
and view...
@validate_captcha def sendemail(request): ...
i put request in decorator args, undefined when put in view args. tried calling few others ways without success, how put in there?
you need have wrapper function:
def validate_captcha(view): def wrap(request, *args, **kwargs): if request.method == 'post': url = "https://www.google.com/recaptcha/api/siteverify" values = { 'secret': settings.google_recaptcha_secret_key, 'response': request.post.get(u'g-recaptcha-response', none), 'remoteip': request.meta.get("remote_addr", none), } data = urllib.urlencode(values) req = urllib2.request(url, data) response = urllib2.urlopen(req) result = json.loads(response.read()) # result["success"] true on success if result["success"]: return view else: return fail return fail return wrap
make sure study awesome , quite detailed overview on decorators in python (i think, 1 of best answers ever):
Comments
Post a Comment