create change_email view

This commit is contained in:
Son NK 2019-11-18 19:41:17 +01:00
parent be6266fb0e
commit 2dd25cc72d
3 changed files with 62 additions and 0 deletions

View File

@ -9,4 +9,5 @@ from .views import (
github,
google,
facebook,
change_email,
)

View File

@ -0,0 +1,30 @@
{% extends "single.html" %}
{% block title %}
Change Email
{% endblock %}
{% block single_content %}
{% if error %}
<div class="text-danger text-center mb-4">{{ error }}</div>
{% endif %}
{% if incorrect_code %}
<div class="text-danger text-center h4">
The link is incorrect. <br><br>
Please go to <a href="{{ url_for('dashboard.setting') }}"
class="btn btn-warning">settings</a>
page to re-send confirmation email.
</div>
{% endif %}
{% if expired_code %}
<div class="text-danger text-center h4">
The link is already expired. <br><br>
Please go to <a href="{{ url_for('dashboard.setting') }}"
class="btn btn-warning">settings</a>
page to re-send confirmation email.
</div>
{% endif %}
{% endblock %}

View File

@ -0,0 +1,31 @@
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.extensions import db
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:
return render_template("auth/change_email.html", incorrect_code=True)
if email_change.is_expired():
return render_template("auth/change_email.html", expired_code=True)
user = email_change.user
user.email = email_change.new_email
EmailChange.delete(email_change.id)
db.session.commit()
flash("Your new email has been updated successfully", "success")
login_user(user)
return redirect(url_for("dashboard.index"))