def post(self, request): post_schema = { "email": schema.email(required=True), "password": schema.text(required=True), } data = json.loads(request.body) data, errors = schema.validate(post_schema, data) if errors != {}: return json_response(request, {"status": "error", "errors": errors}) username = data.get('email').lower() password = data.get('password') user = authenticate(request, username=username, password=password) if user is None: return json_response(request, { "status": "error", "errors": AuthenticationForm.error_messages.get('invalid_login') % { "username" : "email" } }) if not user.is_active: return json_response(request, { "status": "error", "errors": AuthenticationForm.error_messages.get('inactive') }) login(request, user) return json_response(request, { "status": "success", "user": user.username });
def post(self, request): def _user_check(): def _validate(value): error = _('A user with that email address already exists.') if User.objects.filter(username__iexact=value).exists(): return None, error return value, None return _validate post_schema = { "email": schema.chain([ schema.email(required=True), _user_check(), ]), "first_name": schema.text(required=True), "last_name": schema.text(required=True), "password": schema.text(required=True), "newsletter": schema.boolean(), } data = json.loads(request.body) data, errors = schema.validate(post_schema, data) if errors != {}: return json_response(request, {"status": "error", "errors": errors}) user = create_user(data['email'], data['first_name'], data['last_name'], data['password'], data.get('newsletter', False)) login(request, user) return json_response(request, { "status": "created", "user": user.username });
def post(self, request): def _user_check(): def _validate(value): error = _('A user with that email address already exists.') if User.objects.filter(username__iexact=value).exists(): return None, error return value, None return _validate post_schema = { "email": schema.chain([ schema.email(required=True), _user_check(), ]), "first_name": schema.text(required=True), "last_name": schema.text(required=True), "password": schema.text(required=True), "communication_opt_in": schema.boolean(required=True), "g-recaptcha-response": schema.text(required=True), } data = json.loads(request.body) data, errors = schema.validate(post_schema, data) recaptcha_response = data.get('g-recaptcha-response') recaptcha_data = { 'secret': settings.RECAPTCHA_SECRET_KEY, 'response': recaptcha_response } r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=recaptcha_data) captcha_result = r.json() if not captcha_result.get('success'): # TODO track metric somewhere? return json_response(request, { "status": "error", "errors": '1011010010010010111' }) if errors != {}: return json_response(request, { "status": "error", "errors": errors }) user = create_user(data['email'], data['first_name'], data['last_name'], data['password'], data.get('communication_opt_in', False)) login(request, user) return json_response(request, { "status": "created", "user": user.username })