def register(): """ Registers a Physician to use our system. Physicians will be required to enter a username, email address, password, and password confirmation. """ form = UserRegistrationForm(request.form) if request.method == 'POST': try: if User.objects(email=form.email.data).count() > 0: u = User.objects(email=form.email.data)[0] if not u.confirmed: flash("That email address has already been registered, but has not been confirmed. " "Please click the link in the confirmation email to continue.", 'warning') return render_template('register.html', form=form) except AttributeError: pass # Users table is empty, so no need to check. if form.validate(): # Create the new user with "unconfirmed" state. new_user = User(username=form.username.data.lower(), full_name=form.full_name.data, email=form.email.data) new_user.set_password(form.password.data) new_user.confirmed = False try: # Try to save this new user (implicitly validating the uniqueness of email/username) new_user.save() # Generate and send a confirmation email to this new Physician user email_sent = email_physician_confirmation(email=form.email.data, name=form.full_name.data) if email_sent: success_msg = "Account successfully created. Please check your email for a confirmation link " \ " in order to login." flash(success_msg, 'success') return redirect('/') else: flash('We were unable to send your confirmation email. Please ensure the provided email address " \ "is correct.', 'warning') except NotUniqueError: flash("That username or email is already registered. Please try a different one.", 'warning') return render_template('register.html', form=form) else: flash("Invalid input: please see the suggestions below.", 'warning') return render_template('register.html', form=form)