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

35 lines
918 B
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
2019-11-18 19:41:17 +01:00
from app.models import EmailChange
@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
user.email = email_change.new_email
EmailChange.delete(email_change.id)
Session.commit()
2019-11-18 19:41:17 +01:00
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"))