Separate email change in setting screen

This commit is contained in:
Son NK 2020-02-13 16:57:17 +07:00
parent 4cd526513c
commit 5c4f46fdc2
2 changed files with 107 additions and 79 deletions

View file

@ -9,17 +9,22 @@
{% block default_content %}
<div class="col-md-8 offset-md-2 pb-3">
<!-- Change email -->
<div class="card">
<form method="post" enctype="multipart/form-data">
{{ form.csrf_token }}
<input type="hidden" name="form-name" value="update-profile">
<h1 class="h3">Profile</h1>
<input type="hidden" name="form-name" value="update-email">
{{ change_email_form.csrf_token }}
<div class="card-body">
<div class="card-title">
Email
</div>
<div class="form-group">
<label class="form-label">Email</label>
<!-- Not allow user to change email if there's a pending change -->
{{ form.email(class="form-control", value=current_user.email, readonly=pending_email != None) }}
{{ render_field_errors(form.email) }}
{{ change_email_form.email(class="form-control", value=current_user.email, readonly=pending_email != None) }}
{{ render_field_errors(change_email_form.email) }}
{% if pending_email %}
<div class="mt-2">
@ -31,7 +36,22 @@
</div>
{% endif %}
</div>
<button class="btn btn-primary">Change Email</button>
</div>
</form>
</div>
<!-- END Change email -->
<!-- Change name & profile picture -->
<div class="card">
<form method="post" enctype="multipart/form-data">
{{ form.csrf_token }}
<input type="hidden" name="form-name" value="update-profile">
<div class="card-body">
<div class="card-title">
Profile
</div>
<div class="form-group">
<label class="form-label">Name</label>
{{ form.name(class="form-control", value=current_user.name) }}
@ -46,9 +66,11 @@
<img src="{{ current_user.profile_picture_url() }}" class="profile-picture">
{% endif %}
</div>
<button class="btn btn-primary">Update</button>
</div>
</form>
</div>
<!-- END change name & profile picture -->
<hr>
@ -96,7 +118,8 @@
<input type="hidden" name="form-name" value="notification-preference">
<div class="form-inline mb-3">
<div class="form-group">
<input type="checkbox" id="notification" name="notification" {% if current_user.notification %} checked {% endif %} class="form-check-input">
<input type="checkbox" id="notification" name="notification" {% if current_user.notification %}
checked {% endif %} class="form-check-input">
<label for="notification">I want to receive your newsletter</label>
</div>
</div>

View file

@ -7,6 +7,7 @@ from flask_login import login_required, current_user, logout_user
from flask_wtf import FlaskForm
from flask_wtf.file import FileField
from wtforms import StringField, validators
from wtforms.fields.html5 import EmailField
from app import s3, email_utils
from app.config import URL
@ -30,11 +31,16 @@ from app.utils import random_string
class SettingForm(FlaskForm):
email = StringField("Email")
name = StringField("Name")
profile_picture = FileField("Profile Picture")
class ChangeEmailForm(FlaskForm):
email = EmailField(
"email", validators=[validators.DataRequired(), validators.Email()]
)
class PromoCodeForm(FlaskForm):
code = StringField("Name", validators=[validators.DataRequired()])
@ -44,6 +50,7 @@ class PromoCodeForm(FlaskForm):
def setting():
form = SettingForm()
promo_form = PromoCodeForm()
change_email_form = ChangeEmailForm()
email_change = EmailChange.get_by(user_id=current_user.id)
if email_change:
@ -52,6 +59,37 @@ def setting():
pending_email = None
if request.method == "POST":
if request.form.get("form-name") == "update-email":
if change_email_form.validate():
if form.email.data != current_user.email and not pending_email:
new_email = form.email.data
# check if this email is not already used
if (
email_already_used(new_email)
or GenEmail.get_by(email=new_email)
or DeletedAlias.get_by(email=new_email)
):
flash(f"Email {new_email} already used", "error")
elif not can_be_used_as_personal_email(new_email):
flash(
"You cannot use this email address as your personal inbox.",
"error",
)
else:
email_change = EmailChange.create(
user_id=current_user.id,
code=random_string(
60
), # todo: make sure the code is unique
new_email=new_email,
)
db.session.commit()
send_change_email_confirmation(current_user, email_change)
flash(
"A confirmation email is on the way, please check your inbox",
"success",
)
if request.form.get("form-name") == "update-profile":
if form.validate():
profile_updated = False
@ -79,40 +117,6 @@ def setting():
if profile_updated:
flash(f"Your profile has been updated", "success")
if (
form.email.data
and form.email.data != current_user.email
and not pending_email
):
new_email = form.email.data
# check if this email is not used by other user, or as alias
if (
email_already_used(new_email)
or GenEmail.get_by(email=new_email)
or DeletedAlias.get_by(email=new_email)
):
flash(f"Email {new_email} already used", "error")
elif not can_be_used_as_personal_email(new_email):
flash(
"You cannot use this email address as your personal inbox.",
"error",
)
else:
email_change = EmailChange.create(
user_id=current_user.id,
code=random_string(
60
), # todo: make sure the code is unique
new_email=new_email,
)
db.session.commit()
send_change_email_confirmation(current_user, email_change)
flash(
"A confirmation email is on the way, please check your inbox",
"success",
)
elif request.form.get("form-name") == "change-password":
send_reset_password_email(current_user)
@ -174,6 +178,7 @@ def setting():
form=form,
PlanEnum=PlanEnum,
promo_form=promo_form,
change_email_form=change_email_form,
pending_email=pending_email,
AliasGeneratorEnum=AliasGeneratorEnum,
)