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,46 +9,68 @@
{% block default_content %}
<div class="col-md-8 offset-md-2 pb-3">
<form method="post" enctype="multipart/form-data">
{{ form.csrf_token }}
<input type="hidden" name="form-name" value="update-profile">
<!-- Change email -->
<div class="card">
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="form-name" value="update-email">
{{ change_email_form.csrf_token }}
<h1 class="h3">Profile</h1>
<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) }}
{% if pending_email %}
<div class="mt-2">
<span class="text-danger">Pending email change: {{ pending_email }}</span>
<a href="{{ url_for('dashboard.resend_email_change') }}" class="btn btn-secondary btn-sm">Resend
confirmation email</a>
<a href="{{ url_for('dashboard.cancel_email_change') }}" class="btn btn-secondary btn-sm">Cancel email
change</a>
<div class="card-body">
<div class="card-title">
Email
</div>
{% endif %}
</div>
<div class="form-group">
<label class="form-label">Email</label>
<div class="form-group">
<label class="form-label">Name</label>
{{ form.name(class="form-control", value=current_user.name) }}
{{ render_field_errors(form.name) }}
</div>
<!-- Not allow user to change email if there's a pending change -->
{{ change_email_form.email(class="form-control", value=current_user.email, readonly=pending_email != None) }}
{{ render_field_errors(change_email_form.email) }}
<div class="form-group">
<div class="form-label">Profile picture</div>
{{ form.profile_picture(class="form-control-file") }}
{{ render_field_errors(form.profile_picture) }}
{% if current_user.profile_picture_id %}
<img src="{{ current_user.profile_picture_url() }}" class="profile-picture">
{% endif %}
</div>
{% if pending_email %}
<div class="mt-2">
<span class="text-danger">Pending email change: {{ pending_email }}</span>
<a href="{{ url_for('dashboard.resend_email_change') }}" class="btn btn-secondary btn-sm">Resend
confirmation email</a>
<a href="{{ url_for('dashboard.cancel_email_change') }}" class="btn btn-secondary btn-sm">Cancel email
change</a>
</div>
{% endif %}
</div>
<button class="btn btn-primary">Change Email</button>
</div>
</form>
</div>
<!-- END Change email -->
<button class="btn btn-primary">Update</button>
</form>
<!-- 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) }}
{{ render_field_errors(form.name) }}
</div>
<div class="form-group">
<div class="form-label">Profile picture</div>
{{ form.profile_picture(class="form-control-file") }}
{{ render_field_errors(form.profile_picture) }}
{% if current_user.profile_picture_id %}
<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>
@ -92,16 +114,17 @@
<h3 class="mb-0" id="notification">Notifications</h3>
<div class="small-text mb-3">Do you want to receive our newsletter?</div>
<form method="post">
<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">
<label for="notification">I want to receive your newsletter</label>
</div>
<form method="post">
<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">
<label for="notification">I want to receive your newsletter</label>
</div>
<button type="submit" class="btn btn-outline-primary">Submit</button>
</form>
</div>
<button type="submit" class="btn btn-outline-primary">Submit</button>
</form>
{% if current_user.get_subscription() %}

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,
)