Exemplo n.º 1
0
def register():
    """Register user from current request.

    Returns the user if successful, else raise."""
    errors = None
    user = None
    try:
        data = RegisterUser(prepare_request_data(request))
        if data.get('password1') != data.get('password2'):
            errors = 'Passwords must match.'
    except voluptuous.MultipleInvalid as err:
        errors = str(err)
    if not errors:
        with tables(db.engine, 'users') as (con, users):
            try:
                password_hash = bcrypt.generate_password_hash(
                    request.form['password1'])
                user = users.insert({
                    'username': request.form['username'],
                    'password': password_hash,
                    'email': request.form['email']
                }).returning(*users.c).execute().fetchone()
            except exc.IntegrityError as e:
                errors = "Can\'t create user: " + str(e)
        if user:
            user = wrap_user(user)
            login_user(user)
            return redirect('/')
    return render_template('register.html', errors=errors)
Exemplo n.º 2
0
 def wrapper(*args, **kwargs):
     if not (request.json or request.data or request.form):
         flask_restful.abort(400, message='Validation error.',
                                 errors=['No data provided.'])
     try:
         data = schema(prepare_request_data(request))
     except voluptuous.MultipleInvalid as err:
         flask_restful.abort(400,
                                 message='Validation error.',
                                 errors=[str(e) for e in err.errors])
     setattr(request, 'validated_body', data)
     return f(*args, **kwargs)
Exemplo n.º 3
0
def login():
    """Attempt to login a user from the current request.

    Return the user if successful, else raise."""
    try:
        data = LoginUser(prepare_request_data(request))
    except voluptuous.MultipleInvalid as err:
        render_template('login.html', errors=str(err))
    user = check_login(data['username'], data['password'])
    if user:
        login_user(user)
        return safely_redirect_to_next('home')
    return render_template('login.html', errors='No such user.')