Example #1
0
 def test_validate_success(self, db):
     """Register with success."""
     form = RegisterForm(
         username="******",
         email="*****@*****.**",
         password="******",
         confirm="example",
     )
     assert form.validate() is True
Example #2
0
    def test_validate_email_already_registered(self, user):
        """Enter email that is already registered."""
        form = RegisterForm(username="******",
                            email=user.email,
                            password="******",
                            confirm="example")

        assert form.validate() is False
        assert "Email already registered" in form.email.errors
Example #3
0
    def test_validate_user_already_registered(self, user):
        """Enter username that is already registered."""
        form = RegisterForm(
            username=user.username,
            email="*****@*****.**",
            password="******",
            confirm="example",
        )

        assert form.validate() is False
        assert "Username already registered" in form.username.errors
def register():
    """Register new user."""
    if current_user.is_authenticated:
        return redirect(url_for('public.home'))

    form = RegisterForm()
    if form.validate_on_submit():
        username = form.username.data
        email = form.email.data
        password = form.password.data
        password1 = form.confirm.data
        if not all([username, email, password, password1]):
            flash('请把信息填写完整', "warning")
        elif password != password1:
            flash('两次密码不一致,请重新输入!', "warning")
        elif User.query.filter(User.username == username).first():
            flash('这个用户名已经被注册过了!', "warning")
        elif User.query.filter(User.email == email).first():
            flash('这个邮箱已经被注册过了!', "warning")
        else:
            new_user = User(username=username,
                            email=email,
                            active=False,
                            id=None)
            # add a User(active = False)
            new_user.set_password(password)
            db.session.add(new_user)
            # try:
            #     db.session.commit()

            #     return redirect(url_for('public.home'))
            # except:
            #     flash("注册失败,请重试!")
            #     db.session.rollback()
            db.session.commit()
            flash("感谢注册,请联系管理员激活账号!", "success")
            return redirect(url_for('public.login'))
    return render_template('public/register.html', form=form)