def resetpass(): """Reset the user's password If the user successfully submitted the form, send a password reset email. Otherwise, render the reset form again. """ form = ForgotForm() if form.validate_on_submit(): email = form.email.data pw_reset = PasswordReset(user=User.get_by_email(email), ) db.session.add(pw_reset) db.session.commit() #Send email here title = 'Reset your Password' url = "{site}/auth/setnewpass/{key}".format( site=os.environ['DLI_REPORTS_SITE_URL'], key=pw_reset.key, ) content = 'Click this link to reset your password: {url}'.format( url=url) content += '\nThis link will expire in 7 days!' msg = Message(title, recipients=[email]) msg.body = content mail.send(msg) flash("Email sent!", "alert-success") return redirect(url_for('default.home')) else: flash_form_errors(form) return render_template('auth/resetpass.html', form=form)
def resetpass(): """Reset the user's password If the user successfully submitted the form, send a password reset email. Otherwise, render the reset form again. """ form = ForgotForm() if form.validate_on_submit(): email = form.email.data pw_reset = PasswordReset( user=User.get_by_email(email), ) db.session.add(pw_reset) db.session.commit() #Send email here title = 'Reset your Password' url = "{site}/auth/setnewpass/{key}".format( site=os.environ['DLI_REPORTS_SITE_URL'], key=pw_reset.key, ) content = 'Click this link to reset your password: {url}'.format(url=url) content += '\nThis link will expire in 7 days!' msg = Message(title, recipients=[email]) msg.body = content mail.send(msg) flash("Email sent!", "alert-success") return redirect(url_for('default.home')) else: flash_form_errors(form) return render_template('auth/resetpass.html', form=form)
def validate(self): """Validate the form Perform validation by checking that all submitted values are within acceptable ranges and the user is allowed to register an account. """ if not Form.validate(self): return False # First check if the user has already created an account user = User.get_by_email(self.email.data) if user and user.check_password(self.password.data): self.user = user return True # Check that email is "allowed" to register candidate = RegisterCandidate.query.filter_by( email=self.email.data, registration_key=self.registration_key.data, ).first() if candidate is None: self.email.errors.append( "Sorry, you aren't allowed to register at this time.", ) return False # Check that location is within list of approved locations location = Location.query.get(self.location.data) if location is None: self.location.errors.append('Location not supported') return False department = Department.query.get(self.department.data) if department is None: self.department.errors.append('Department not found') return False # Create the new user account self.user = User( name=self.name.data, email=self.email.data, password=self.password.data, location=location, department=department, ) return True
def validate(self): """Validate the form Perform validation by checking that the user email exists. """ if not Form.validate(self): return False user = User.get_by_email(self.email.data) if user is None: self.email.errors.append('No account with that email found.') return False self.user = user return True
def populate_db_users(): """Populate the database User model""" users = [ User( name='Nobody', email='*****@*****.**', password=os.environ['DLI_REPORTS_ADMIN_PASSWORD'], location=Location.query.first(), department=Department.query.first(), ), ] # Set the "Nobody" user to be an admin by default users[0].is_admin = True db.session.add_all(users) db.session.commit()
def validate(self): """Validate the form Perform validation by checking that the user account exists and the password hashes match. """ if not Form.validate(self): return False user = User.get_by_email(self.email.data) if user is None: self.email.errors.append('No account with that email found.') return False if not user.check_password(self.password.data): self.password.errors.append('Incorrect password!') return False self.user = user return True