app-MAIL-temp/app/auth/views/change_email.py

39 lines
1.1 KiB
Python
Raw Normal View History

2019-11-18 19:41:17 +01:00
from flask import request, flash, render_template, redirect, url_for
from flask_login import login_user
from app.auth.base import auth_bp
from app.db import Session
from app.log import LOG
from app.models import EmailChange, ResetPasswordCode
2019-11-18 19:41:17 +01:00
@auth_bp.route("/change_email", methods=["GET", "POST"])
def change_email():
code = request.args.get("code")
email_change: EmailChange = EmailChange.get_by(code=code)
if not email_change:
2020-08-13 10:59:39 +02:00
return render_template("auth/change_email.html")
2019-11-18 19:41:17 +01:00
if email_change.is_expired():
2020-08-13 10:59:39 +02:00
# delete the expired email
EmailChange.delete(email_change.id)
Session.commit()
2020-08-13 10:59:39 +02:00
return render_template("auth/change_email.html")
2019-11-18 19:41:17 +01:00
user = email_change.user
old_email = user.email
2019-11-18 19:41:17 +01:00
user.email = email_change.new_email
EmailChange.delete(email_change.id)
ResetPasswordCode.filter_by(user_id=user.id).delete()
Session.commit()
2019-11-18 19:41:17 +01:00
LOG.i(f"User {user} has changed their email from {old_email} to {user.email}")
2019-12-30 17:43:38 +01:00
flash("Your new email has been updated", "success")
2019-11-18 19:41:17 +01:00
login_user(user)
return redirect(url_for("dashboard.index"))