Ejemplo n.º 1
0
def register():
    registration_form = RegistrationForm()
    if current_user.is_authenticated:
        return redirect(url_for(Action.login))  # redirect if logged in
    elif request.method == 'GET' and current_user.is_anonymous:
        return render_template(
            "register.html",
            form=registration_form)  # display form if not logged in
    elif request.method == 'POST' and registration_form.validate_on_submit():
        # check that username, email are unique (don't exist in db yet)
        # this is already done by flask-wtf in User model class, most likely when validate_on_submit() is called
        # if form data not unique, flash error to user and reload "register.html", be ambiguous for security
        # if form data unique, add user to database, inform them they have successfully registered
        # all checks specified in this comment block are taken care of by flask-wtf custome validate_field() methods
        # ...so just add a user
        new_user = User(username=registration_form.username.data,
                        email=registration_form.email.data)
        new_user.set_password(registration_form.password.data)
        db.session.add(new_user)
        db.session.commit()
        flash(_(f"Registration success! Welcome {new_user.username}"))
        return redirect(url_for(Action.login))
    elif not registration_form.validate_on_submit(
    ) and request.method == 'POST':
        flash(_("Registration failed."))
        return render_template("register.html",
                               title=_("Register"),
                               form=registration_form)
    return "Error in register action"  # debugging
Ejemplo n.º 2
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(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 now a registered user!'))
        return redirect(url_for('login'))
    return render_template('register.html', title=_('Register'), form=form)
Ejemplo n.º 3
0
 def test_password_hashing(self):
     u = User(username='******')
     u.set_password('pass')
     self.assertFalse(u.check_password('1234'))
     self.assertTrue(u.check_password('pass'))
Ejemplo n.º 4
0
 def test_password_hashing(self):
     u = User(username='******')
     u.set_password('cat')
     self.assertFalse(u.check_password('dog'))
     self.assertTrue(u.check_password('cat'))