def put(self, args): plan = Plan() if not self.request.user.is_authenticated(): if not 'captcha-id' in args: return HttpResponseBadRequest('Weird') if not 'captcha-value' in args: return HttpResponseBadRequest('Please enter a value') captcha_id = args['captcha-id'] captcha_value = args['captcha-value'] if not captcha_value.isdigit(): return HttpResponseBadRequest('Please enter a number') captcha_value = int(captcha_value) answer = CaptchaRequest.validate(captcha_id, captcha_value) if answer != CAPTCHA_ANSWER_OK: captcha = generate_sum_captcha() response = HttpResponseForbidden('Wrong answer') response['location'] = captcha.uid return response if 'title' in args: plan.title = args['title'] if 'instructions' in args: plan.instructions = args['instructions'] if 'expires' in args: expires = args['expires'] if not isinstance(expires, int): if expires.isdigit(): expires = int(expires) else: return HttpResponseBadRequest() expires = min(max(expires,1),6) else: expires = 1 today = datetime.date.today() delta = relativedelta(months=+expires) plan.expires = today + delta plan.save() response = HttpResponseCreated('/rpc/%s' % plan.hash) data = { 'id': plan.hash, 'title': plan.title, 'instructions': plan.instructions, 'expires': str(plan.expires), } response.content = simplejson.dumps(data) response['Content-type'] = 'application/json' return response
def generate_captcha(): """ Generates a captcha. and returns an object which has a method get_absolute_url() which returns the url to the captcha image and a attribute 'uid' which contains the id of the captcha. """ if not usecaptcha: return None numbers = (int(random()*9)+1,int(random()*9)+1) text = "%d+%d" % numbers answer = sum(numbers) req = CaptchaRequest.generate_request(text,answer,get_current_request().path) return req
def generate_sum_captcha(request_path='any'): numbers = (int(random()*9)+1,int(random()*9)+1) print numbers text = "%d+%d=" % numbers answer = sum(numbers) return CaptchaRequest.generate_request(text,answer,request_path)