Пример #1
0
def register():
    """
    Registration Form
    """
    
    form = RegisterForm(request.form)
    errors = []
    

    if form.is_submitted():
        is_validated = True
        if form.name.data.strip() == '':
            is_validated = False
            errors.append(gettext('Username is required'))
        #validate email
        if form.email.data.strip() == '':
            is_validated = False
            errors.append(gettext('Email is required'))
        #validate valid email
        match = re.search(r'^.+@([^.@][^@]+)$', form.email.data.strip())
        if not match:
            is_validated = False
            errors.append(gettext('Invalid email address'))
        
        if form.password.data.strip() == '':
            is_validated = False
            errors.append(gettext('Password field is required'))
            
        if form.confirm.data.strip() == '':
            is_validated = False
            errors.append(gettext('You have to confirm your password'))
        if form.confirm.data != form.password.data:
            is_validated = False
            errors.append(gettext('Passwords must match'))
        
        if len(form.recaptcha.errors) > 0:
            is_validated = False
            errors.append(gettext('Captcha was incorrect'))
            
        if is_validated:
            same_username_user = User.query.filter_by(username=form.name.data).first()
            same_email_user = User.query.filter_by(email=form.email.data).first()
            if same_email_user is not None:
                errors.append(gettext('Duplicate email address'))
            if same_username_user is not None:
                errors.append(gettext('Duplicate username'))

            if len(errors) > 0:
                return render_template("users/register.html", form=form, errors=errors)

            # Insert the record in our database and commit it
            user = User(username=form.name.data.lower(), email=form.email.data,
                        password=generate_password_hash(form.password.data))
            user.verification_code = utilities.generate_random_string(50)
            user.banned = 2
            db.session.add(user)
            db.session.commit()
            
            # send confirm email and redirect to confirm page
            email_activation = Journal.query.filter(Journal.id==120).first().get_journal_content(session['locale'])
            send_mail([user.email], email_activation.title, render_template_string(email_activation.content, activate_link=url_for('users.verify_account', code=user.verification_code, _external=True)))
            return render_template("users/register_finish.html", email=user.email)
        else:
            return render_template("users/register.html", form=form, errors=errors)
    return render_template("users/register.html", form=form, errors=[])
Пример #2
0
def register():
    """
    Registration Form
    """

    form = RegisterForm(request.form)
    errors = []

    if form.is_submitted():
        is_validated = True
        if form.name.data.strip() == '':
            is_validated = False
            errors.append(gettext('Username is required'))
        #validate email
        if form.email.data.strip() == '':
            is_validated = False
            errors.append(gettext('Email is required'))
        #validate valid email
        match = re.search(r'^.+@([^.@][^@]+)$', form.email.data.strip())
        if not match:
            is_validated = False
            errors.append(gettext('Invalid email address'))

        if form.password.data.strip() == '':
            is_validated = False
            errors.append(gettext('Password field is required'))

        if form.confirm.data.strip() == '':
            is_validated = False
            errors.append(gettext('You have to confirm your password'))
        if form.confirm.data != form.password.data:
            is_validated = False
            errors.append(gettext('Passwords must match'))

        if len(form.recaptcha.errors) > 0:
            is_validated = False
            errors.append(gettext('Captcha was incorrect'))

        if is_validated:
            same_username_user = User.query.filter_by(
                username=form.name.data).first()
            same_email_user = User.query.filter_by(
                email=form.email.data).first()
            if same_email_user is not None:
                errors.append(gettext('Duplicate email address'))
            if same_username_user is not None:
                errors.append(gettext('Duplicate username'))

            if len(errors) > 0:
                return render_template("users/register.html",
                                       form=form,
                                       errors=errors)

            # Insert the record in our database and commit it
            user = User(username=form.name.data.lower(),
                        email=form.email.data,
                        password=generate_password_hash(form.password.data))
            user.verification_code = utilities.generate_random_string(50)
            user.banned = 2
            db.session.add(user)
            db.session.commit()

            # send confirm email and redirect to confirm page
            email_activation = Journal.query.filter(
                Journal.id == 120).first().get_journal_content(
                    session['locale'])
            send_mail([user.email], email_activation.title,
                      render_template_string(email_activation.content,
                                             activate_link=url_for(
                                                 'users.verify_account',
                                                 code=user.verification_code,
                                                 _external=True)))
            return render_template("users/register_finish.html",
                                   email=user.email)
        else:
            return render_template("users/register.html",
                                   form=form,
                                   errors=errors)
    return render_template("users/register.html", form=form, errors=[])