2022-03-27 16:07:44 +02:00
|
|
|
from datetime import datetime
|
2024-04-21 20:44:08 +02:00
|
|
|
|
2024-05-19 11:29:40 +02:00
|
|
|
from flask import Blueprint, flash, redirect, render_template, url_for
|
2022-03-27 16:07:44 +02:00
|
|
|
from flask_bcrypt import generate_password_hash
|
2024-04-21 20:44:08 +02:00
|
|
|
from flask_login import login_user
|
2024-04-28 13:15:23 +02:00
|
|
|
from sqlalchemy.exc import (
|
2024-05-28 23:02:17 +02:00
|
|
|
DatabaseError,
|
|
|
|
DataError,
|
|
|
|
IntegrityError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
2024-04-28 13:15:23 +02:00
|
|
|
)
|
2024-04-21 20:44:08 +02:00
|
|
|
from werkzeug.routing import BuildError
|
|
|
|
|
2024-04-28 13:04:07 +02:00
|
|
|
from app import db
|
2024-06-02 22:16:52 +02:00
|
|
|
from statuspengguna.forms.resetpasswordform import ResetPasswordForm
|
2024-04-28 15:34:29 +02:00
|
|
|
from models.user_model import User
|
2024-04-28 13:04:07 +02:00
|
|
|
|
|
|
|
reset_password = Blueprint(
|
2024-05-28 23:02:17 +02:00
|
|
|
"reset_password",
|
|
|
|
__name__,
|
|
|
|
template_folder="templates/statuspengguna",
|
|
|
|
static_folder="static",
|
2024-04-28 13:04:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@reset_password.route("/resetpassword/<path>", methods=["GET", "POST"])
|
|
|
|
def resetpassword(path):
|
2024-05-28 23:02:17 +02:00
|
|
|
return ResetPassword(path)
|
2024-04-28 13:04:07 +02:00
|
|
|
|
2022-03-27 16:07:44 +02:00
|
|
|
|
2022-03-28 18:48:31 +02:00
|
|
|
def ResetPassword(path):
|
2024-05-28 23:02:17 +02:00
|
|
|
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
|
2022-03-27 16:07:44 +02:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
resetpasswordform = ResetPasswordForm()
|
|
|
|
if resetpasswordform.validate_on_submit():
|
|
|
|
return ResetUserPasswordInDB(user, resetpasswordform)
|
|
|
|
return render_template(
|
|
|
|
"resetpassword.html",
|
|
|
|
resetpasswordform=resetpasswordform,
|
|
|
|
path=path,
|
|
|
|
linkvalid=linkvalid,
|
|
|
|
)
|
2022-03-27 16:07:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def ResetUserPasswordInDB(user, resetpasswordform):
|
2024-05-28 23:02:17 +02:00
|
|
|
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"))
|
2022-03-27 16:07:44 +02:00
|
|
|
|
2024-05-28 23:02:17 +02:00
|
|
|
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")
|