forked from crunk/distribusi-verse
crunk
3 years ago
9 changed files with 184 additions and 17 deletions
@ -0,0 +1,27 @@ |
|||
"""Reset Password Form form to reset a users PasswordField.""" |
|||
from wtforms import ( |
|||
SubmitField, |
|||
PasswordField, |
|||
) |
|||
|
|||
from wtforms import validators |
|||
from wtforms.validators import Length, EqualTo |
|||
from flask_wtf import FlaskForm |
|||
|
|||
|
|||
class ResetPasswordForm(FlaskForm): |
|||
"""ResetPassword for distribusi-verse form class""" |
|||
|
|||
password = PasswordField( |
|||
"New password:", |
|||
validators=[validators.InputRequired(), Length(12, 72)], |
|||
) |
|||
confirmpassword = PasswordField( |
|||
"Confirm your password:", |
|||
validators=[ |
|||
validators.InputRequired(), |
|||
Length(12, 72), |
|||
EqualTo("password", message="Passwords must match !"), |
|||
], |
|||
) |
|||
submit = SubmitField("Reset your password") |
@ -0,0 +1,81 @@ |
|||
from datetime import datetime |
|||
from flask import ( |
|||
render_template, |
|||
redirect, |
|||
flash, |
|||
url_for, |
|||
) |
|||
from sqlalchemy.exc import ( |
|||
IntegrityError, |
|||
DataError, |
|||
DatabaseError, |
|||
InterfaceError, |
|||
InvalidRequestError, |
|||
) |
|||
from werkzeug.routing import BuildError |
|||
from usermodel import User |
|||
from forms.resetpasswordform import ResetPasswordForm |
|||
from flask_login import login_user |
|||
from flask_bcrypt import generate_password_hash |
|||
from app import db |
|||
|
|||
|
|||
def ResetPassword(path): |
|||
linkvalid = False |
|||
user = User.query.filter_by(resethash=path).first() |
|||
if user is None: |
|||
return redirect(url_for("index")) |
|||
timepassed = datetime.now() - user.resettime |
|||
if timepassed.days < 1: |
|||
linkvalid = True |
|||
|
|||
resetpasswordform = ResetPasswordForm() |
|||
if resetpasswordform.validate_on_submit(): |
|||
return ResetUserPasswordInDB(user, resetpasswordform) |
|||
return render_template( |
|||
"resetpassword.html", |
|||
resetpasswordform=resetpasswordform, |
|||
path=path, |
|||
linkvalid=linkvalid, |
|||
) |
|||
|
|||
|
|||
def ResetUserPasswordInDB(user, resetpasswordform): |
|||
try: |
|||
newpassword = resetpasswordform.confirmpassword.data |
|||
user.password = generate_password_hash(newpassword) |
|||
user.resethash = None |
|||
user.resettime = None |
|||
db.session.commit() |
|||
flash("Password Succesfully updated", "success") |
|||
login_user(user) |
|||
return redirect(url_for("index")) |
|||
|
|||
except InvalidRequestError: |
|||
db.session.rollback() |
|||
resetpasswordform.email.errors.append("Something went wrong!") |
|||
flash("Something went wrong!", "danger") |
|||
except IntegrityError: |
|||
db.session.rollback() |
|||
resetpasswordform.email.errors.append("User already exists!") |
|||
flash("User already exists!", "warning") |
|||
except DataError: |
|||
db.session.rollback() |
|||
resetpasswordform.email.errors.append("Invalid Entry") |
|||
flash("Invalid Entry", "warning") |
|||
except InterfaceError: |
|||
db.session.rollback() |
|||
resetpasswordform.email.errors.append( |
|||
"Error connecting to the database" |
|||
) |
|||
flash("Error connecting to the database", "danger") |
|||
except DatabaseError: |
|||
db.session.rollback() |
|||
resetpasswordform.email.errors.append( |
|||
"Error connecting to the database" |
|||
) |
|||
flash("Error connecting to the database", "danger") |
|||
except BuildError: |
|||
db.session.rollback() |
|||
resetpasswordform.email.errors.append("Unknown error occured!") |
|||
flash("An error occured !", "danger") |
@ -0,0 +1,29 @@ |
|||
{% extends "base.html" %} |
|||
{% block main %} |
|||
<div id="login"> |
|||
{% if linkvalid%} |
|||
<form class="form" action="{{ url_for('resetpassword', path=path) }}" method="post"> |
|||
{{ resetpasswordform.csrf_token }} |
|||
<fieldset class="required"> |
|||
{{ resetpasswordform.password.label }} |
|||
{{ resetpasswordform.password }} |
|||
{% for message in resetpasswordform.password.errors %} |
|||
<div class="error">{{ message }}</div> |
|||
{% endfor %} |
|||
</fieldset> |
|||
<fieldset class="required"> |
|||
{{ resetpasswordform.confirmpassword.label }} |
|||
{{ resetpasswordform.confirmpassword }} |
|||
{% for message in resetpasswordform.confirmpassword.errors %} |
|||
<div class="error">{{ message }}</div> |
|||
{% endfor %} |
|||
</fieldset> |
|||
<fieldset class="button required"> |
|||
{{ resetpasswordform.submit }} |
|||
</fieldset> |
|||
</form> |
|||
{% else %} |
|||
<h3>Password reset link no longer valid.</h3> |
|||
{% endif %} |
|||
</div> |
|||
{% endblock main %} |
Loading…
Reference in new issue