diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 1bfd7907..9789a06e 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -117,3 +117,46 @@ class ProfileForm(FlaskForm): db.session.add(agent) if commit: db.session.commit() + + +class PasswordForm(FlaskForm): + password = PasswordField( + 'Current Password', + [validators.DataRequired()], + render_kw={'class': "form-control"}, + ) + newpassword = PasswordField( + 'New Password', + [validators.DataRequired()], + render_kw={'class': "form-control"}, + ) + renewpassword = PasswordField( + 'Re-enter New Password', + [validators.DataRequired()], + render_kw={'class': "form-control"}, + ) + + def validate(self, extra_validators=None): + is_valid = super().validate(extra_validators) + + if not is_valid: + return False + + if not g.user.check_password(self.password.data): + self.password.errors = ['Incorrect password'] + return False + + if self.newpassword.data != self.renewpassword.data: + self.newpassword.errors = ['Is not the same password'] + self.renewpassword.errors = ['Is not the same password'] + return False + + return True + + def save(self, commit=True): + g.user.password = generate_password_hash(self.newpassword.data) + + db.session.add(g.user) + if commit: + db.session.commit() + return diff --git a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html index dba8db62..782b92c8 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html @@ -132,7 +132,7 @@ {{ f }} {% else %}