Beispiel #1
0
def signup():
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))
    form = SignupForm()
    if form.validate_on_submit():
        user = User(firstname=form.firstname.data, lastname=form.lastname.data,
                    username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Congratulations, you are part of the secret santa fun!')
        return redirect(url_for('auth.login'))
    return render_template('auth/signup.html', title='Sign Up', form=form)
Beispiel #2
0
def _init_database(db):
    '''
    Initializes the database for testing.
    '''
    example1 = User()
    example1.username = '******'
    example1.firstname = 'First1'
    example1.lastname = 'Last1'
    example1.email = '*****@*****.**'
    example1.dateofbirth = datetime.datetime(2020, 10, 5)
    example1.is_admin = False
    example1.set_password('test1123')
    db.session.add(example1)

    example2 = User()
    example2.username = '******'
    example2.firstname = 'First2'
    example2.lastname = 'Last2'
    example2.email = '*****@*****.**'
    example2.dateofbirth = datetime.datetime(2020, 10, 5)
    example2.is_admin = False
    example2.set_password('test2123')
    db.session.add(example2)

    example3 = User()
    example3.username = '******'
    example3.firstname = 'First3'
    example3.lastname = 'Last3'
    example3.email = '*****@*****.**'
    example3.dateofbirth = datetime.datetime(2020, 10, 5)
    example3.is_admin = False
    example3.set_password('test3123')
    db.session.add(example3)

    db.session.commit()
Beispiel #3
0
def signup(func_id=2):
    '''
    Registers a user.

    Returns:
        200 -> the user has been registered successfully
    '''

    params = request.get_json()

    if len(params) > 6:
        return errors.response(f'{BP_ID}{func_id}1')

    if not all(x in params for x in ['email', 'username', 'password']):
        return errors.response(f'{BP_ID}{func_id}2')

    new_user = User()

    err_code = None
    for k in params:
        if k == 'username':
            username = params[k]
            if 5 <= len(username) <= 25 and re.search(r'^\w+$', username):
                new_user.username = username
            else:
                err_code = 3
                break

        elif k == 'email':
            email = params[k]
            if re.search(r'^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$',
                         email):
                new_user.email = email
            else:
                err_code = 4
                break

        elif k == 'password':
            password = params[k]
            if 8 <= len(password) <= 64:
                new_user.set_password(password)
            else:
                err_code = 5
                break

        elif k == 'firstname':
            firstname = params[k]
            if len(firstname) <= 64:
                new_user.firstname = firstname
            else:
                err_code = 6
                break

        elif k == 'lastname':
            lastname = params[k]
            if len(lastname) <= 64:
                new_user.lastname = lastname
            else:
                err_code = 6
                break

        elif k == 'dateofbirth':
            try:
                new_user.dateofbirth = dt.datetime.strptime(
                    params[k], f'%Y-%m-%d')
            except ValueError:
                err_code = 7
                break
        else:
            err_code = 8
            break

    if err_code is not None:
        return errors.response(f'{BP_ID}{func_id}{err_code}')

    db.session.add(new_user)
    try:
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        print(e)
        if 'user.username' in str(e):
            return errors.response(f'{BP_ID}{func_id}9U')
        if 'user.email' in str(e):
            return errors.response(f'{BP_ID}{func_id}9E')

    return jsonify({}), 200