2022-03-27 16:07:44 +02:00
|
|
|
from datetime import datetime
|
2024-04-21 20:44:08 +02:00
|
|
|
from uuid import uuid1
|
|
|
|
|
2024-04-28 13:04:07 +02:00
|
|
|
from flask import Blueprint, render_template
|
|
|
|
from flask_mail import Mail, Message
|
2024-04-28 13:15:23 +02:00
|
|
|
from sqlalchemy.exc import (
|
2024-05-28 23:02:17 +02:00
|
|
|
DatabaseError,
|
|
|
|
DataError,
|
|
|
|
InterfaceError,
|
|
|
|
InvalidRequestError,
|
2024-04-28 13:15:23 +02:00
|
|
|
)
|
2022-03-25 15:37:00 +01:00
|
|
|
|
2024-04-28 13:04:07 +02:00
|
|
|
from app import db, get_app
|
2024-06-02 22:37:55 +02:00
|
|
|
from statuspengguna.forms.forgotpasswordform import ForgotPasswordForm
|
2024-04-28 15:34:29 +02:00
|
|
|
from models.user_model import User
|
2024-04-28 13:04:07 +02:00
|
|
|
|
|
|
|
mail = Mail(get_app())
|
|
|
|
forgot_password = Blueprint(
|
2024-05-28 23:02:17 +02:00
|
|
|
"forgotpassword",
|
|
|
|
__name__,
|
|
|
|
template_folder="templates/statuspengguna",
|
|
|
|
static_folder="static",
|
2024-04-28 13:04:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@forgot_password.route("/", methods=["GET", "POST"])
|
|
|
|
def forgotpassword():
|
2024-05-28 23:02:17 +02:00
|
|
|
return ForgotPassword(mail)
|
2024-04-28 13:04:07 +02:00
|
|
|
|
2022-03-25 15:37:00 +01:00
|
|
|
|
|
|
|
def ForgotPassword(mail):
|
2024-05-28 23:02:17 +02:00
|
|
|
forgotpasswordform = ForgotPasswordForm()
|
|
|
|
if forgotpasswordform.validate_on_submit():
|
|
|
|
user = User.query.filter_by(
|
|
|
|
email=forgotpasswordform.email.data
|
|
|
|
).first()
|
|
|
|
if user is not None:
|
|
|
|
resethash = AddResetPasswordHash(user, forgotpasswordform)
|
|
|
|
ResetPassWordMessage(user, resethash, mail)
|
|
|
|
forgotpasswordform.email.errors.append(
|
|
|
|
f"""If {forgotpasswordform.email.data} exists, an email is send with
|
2022-03-25 15:37:00 +01:00
|
|
|
a password reset link. (If your inbox doesn't
|
|
|
|
contain any new mail, please check your spam folder.)"""
|
2024-05-28 23:02:17 +02:00
|
|
|
)
|
|
|
|
return render_template(
|
|
|
|
"forgotpassword.html", forgotpasswordform=forgotpasswordform
|
|
|
|
)
|
2022-03-25 15:37:00 +01:00
|
|
|
|
|
|
|
|
2022-03-27 16:07:44 +02:00
|
|
|
def AddResetPasswordHash(user, forgotpasswordform):
|
2024-05-28 23:02:17 +02:00
|
|
|
resethash = uuid1().hex
|
|
|
|
try:
|
|
|
|
user.resettime = datetime.now()
|
|
|
|
user.resethash = resethash
|
|
|
|
db.session.commit()
|
|
|
|
except (InvalidRequestError, DataError, InterfaceError, DatabaseError):
|
|
|
|
forgotpasswordform.email.errors.append("Something went wrong!")
|
|
|
|
db.session.rollback()
|
|
|
|
return resethash
|
2022-03-27 16:07:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def ResetPassWordMessage(user, resethash, mail):
|
2024-05-28 23:02:17 +02:00
|
|
|
msg = Message(
|
|
|
|
"Distribusiverse Forgotten Password ",
|
|
|
|
sender=("Distribusiverse mailer", "test@this.com"),
|
|
|
|
recipients=[user.email],
|
|
|
|
)
|
|
|
|
msg.html = f"""{user.username} has requested a password reset for
|
2022-03-27 16:07:44 +02:00
|
|
|
Distribusiverse.<br><hr>
|
|
|
|
<a href='http://localhost:5000/resetpassword/{resethash}'>Click here to
|
|
|
|
reset your password.</a>"""
|
2024-05-28 23:02:17 +02:00
|
|
|
mail.send(msg)
|