def register(): if flask.g.user: return flask.redirect(redirect_url()) form = forms.RegisterForm(flask.request.form) if flask.request.method == 'POST' and form.validate(): user = models.User(username=form.username.data.strip(), email=form.email.data.strip(), password=form.password.data) user.last_login_ip = ip_address(flask.request.remote_addr).packed db.session.add(user) db.session.commit() if app.config['USE_EMAIL_VERIFICATION']: # force verification, enable email activ_link = get_activation_link(user) send_verification_email(user.email, activ_link) return flask.render_template('waiting.html') else: # disable verification, set user as active and auto log in user.status = models.UserStatusType.ACTIVE db.session.add(user) db.session.commit() flask.g.user = user flask.session['user_id'] = user.id flask.session.permanent = True flask.session.modified = True return flask.redirect(redirect_url()) return flask.render_template('register.html', form=form)
def send_verification_email(user): activation_link = get_activation_link(user) tmpl_context = {'activation_link': activation_link, 'user': user} email_msg = email.EmailHolder( subject='Verify your {} account'.format( app.config['GLOBAL_SITE_NAME']), recipient=user, text=flask.render_template('email/verify.txt', **tmpl_context), html=flask.render_template('email/verify.html', **tmpl_context), ) email.send_email(email_msg)