def register(): user_form = UserRegistrationForm() if request.method == 'GET': return render_template('register.html', user_form=user_form) if request.method == 'POST': if user_form.validate(): # create the new user new_user = User( name = user_form.name.data , email = user_form.email.data , self_reported_org = user_form.organization.data , hashed_password = bcrypt.generate_password_hash( user_form.password.data, 10) , email_confirmation_code = random_alphanumeric(20) ) new_user.save() new_user.send_verification_email() flash('We have sent you a message to confirm your email address.' , 'info') return redirect(url_for('public.root')) # validation failed flash('Validation failed.', 'error') return redirect(url_for('users.register'))
def signup(): """ Creates a user based on the fields of a sign up form. -] name - desired username password - desired password email - desired email """ if request.method == 'POST': f = request.form name_slug = slugify(f['name']) # Make sure everything checks out. if not f['name']: flash("You must choose a name.") return redirect(url_for('main')) elif not f['email']: flash("You must enter your email.") return redirect(url_for('main')) elif not f['password']: flash("You must choose a password.") return redirect(url_for('main')) else: # TODO: Make this nicer. try: if User.get_by(name_slug=name_slug): flash("Someone already chose that name.") return redirect(url_for('main')) except NoResultFound: try: if User.get_by(email=f['email']): flash("Someone is already using that email.") return redirect(url_for('main')) except NoResultFound: pass # Create the user and log in. name_slug = slugify(f['name']) hashed_password = generate_password_hash(f['password']) user = User(name=f['name'], name_slug=name_slug, \ password=hashed_password, email=f['email']) session['name'] = f['name'] if models.local: user.verified = True else: user.send_verification_email() models.session.commit() return redirect(url_for('main')) elif request.method == 'GET': return redirect(url_for('main'))