def register(): form = RegistrationForm() if current_app.config['REQUEST_REGISTRATION']: if 'token' in request.json: form = TokenRegistrationForm() else: form = RegistrationRequestForm() if form.validate_on_submit(): return registration_request(form.email.data, request.remote_addr) if form.validate_on_submit(): new_user = User(form.username.data, form.email.data, form.password.data, request.remote_addr) new_user.save() if login_user(new_user): return jsonify(new_user) return jsonify(anonymous_user_data) form.errors['_status_code'] = 400 return jsonify(**form.errors)
def login(): form = LoginForm() if form.validate_on_submit(): form_user = User.get_user(form.username.data) if form_user and form_user.check_password(form.password.data): if login_user(form_user): return jsonify(form_user) else: return jsonify(username=['Your account is currently disabled.'], _status_code=400) else: return jsonify(username=['Invalid username, email or password.'], _status_code=400) return '' form.errors['_status_code'] = 400 return jsonify(**form.errors)
def load_user(userid): return User.get(userid)
def validate_email(form, field): if User.get_by_email(field.data): raise ValidationError("Email already registered.")
def validate_username(form, field): if User.get_by_username(field.data): raise ValidationError("Username already registered.")
def validate_email(form, field): if User.get_by_email(field.data): raise ValidationError("This email is already registered") if UserRegistrationRequest.get_by_email(field.data): raise ValidationError("Registration already requested for this email.")