Esempio n. 1
0
	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
Esempio n. 2
0
def Forbidden(msg):
    error = {
            'success': False,
            'data': {
                'code': 403,
                'message': msg
            }
        }
    resp = HttpResponseForbidden()
    resp.content = json.dumps(error)
    return resp