def new_user(): """ Create a new user. """ form = forms.NewUserForm() if form.validate_on_submit(): username = form.user.data if pagure.lib.search_user(SESSION, username=username): flask.flash('Username already taken.', 'error') return flask.redirect(flask.request.url) email = form.email_address.data if pagure.lib.search_user(SESSION, email=email): flask.flash('Email address already taken.', 'error') return flask.redirect(flask.request.url) password = '******' % (form.password.data, APP.config.get('PASSWORD_SEED', None)) form.password.data = hashlib.sha512(password).hexdigest() token = pagure.lib.login.id_generator(40) user = model.User() user.token = token form.populate_obj(obj=user) user.default_email = form.email_address.data SESSION.add(user) SESSION.flush() emails = [email.email for email in user.emails] if form.email_address.data not in emails: useremail = model.UserEmail(user_id=user.id, email=form.email_address.data) SESSION.add(useremail) SESSION.flush() try: SESSION.flush() send_confirmation_email(user) flask.flash( 'User created, please check your email to activate the ' 'account') except SQLAlchemyError as err: SESSION.rollback() flask.flash('Could not create user.') APP.logger.debug('Could not create user.') APP.logger.exception(err) SESSION.commit() return flask.redirect(flask.url_for('auth_login')) return flask.render_template( 'login/user_new.html', form=form, )
def new_user(): """ Create a new user. """ form = forms.NewUserForm() if form.validate_on_submit(): username = form.user.data if pagure.lib.query.search_user(flask.g.session, username=username): flask.flash("Username already taken.", "error") return flask.redirect(flask.request.url) email = form.email_address.data if pagure.lib.query.search_user(flask.g.session, email=email): flask.flash("Email address already taken.", "error") return flask.redirect(flask.request.url) form.password.data = generate_hashed_value(form.password.data) token = pagure.lib.login.id_generator(40) user = model.User() user.token = token form.populate_obj(obj=user) user.default_email = form.email_address.data flask.g.session.add(user) flask.g.session.flush() try: pagure.lib.query.add_email_to_user( flask.g.session, user, form.email_address.data ) flask.g.session.commit() send_confirmation_email(user) flask.flash( "User created, please check your email to activate the " "account" ) except pagure.exceptions.PagureException as err: flask.flash(str(err), "error") _log.exception(err) except SQLAlchemyError: # pragma: no cover flask.g.session.rollback() flask.flash("Could not create user.") _log.exception("Could not create user.") return flask.redirect(flask.url_for("auth_login")) return flask.render_template("login/user_new.html", form=form)
def new_user(): """ Create a new user. """ form = forms.NewUserForm() if form.validate_on_submit(): username = form.user.data if pagure.lib.search_user(SESSION, username=username): flask.flash('Username already taken.', 'error') return flask.redirect(flask.request.url) email = form.email_address.data if pagure.lib.search_user(SESSION, email=email): flask.flash('Email address already taken.', 'error') return flask.redirect(flask.request.url) form.password.data = generate_hashed_value(form.password.data) token = pagure.lib.login.id_generator(40) user = model.User() user.token = token form.populate_obj(obj=user) user.default_email = form.email_address.data SESSION.add(user) SESSION.flush() pagure.lib.add_email_to_user(SESSION, user, form.email_address.data) try: SESSION.commit() send_confirmation_email(user) flask.flash( 'User created, please check your email to activate the ' 'account') except SQLAlchemyError: # pragma: no cover SESSION.rollback() flask.flash('Could not create user.') _log.exception('Could not create user.') return flask.redirect(flask.url_for('auth_login')) return flask.render_template( 'login/user_new.html', form=form, )