user can update the random alias domain

This commit is contained in:
Son NK 2020-06-25 13:04:27 +02:00
parent 040c6d1f9e
commit abe9768db4
3 changed files with 55 additions and 4 deletions

View file

@ -132,11 +132,12 @@
</div>
</div>
<div class="card">
<div id="random-alias" class="card">
<div class="card-body">
<div class="card-title">Random Alias</div>
<div class="mb-3">Change the way random aliases are generated by default.</div>
<form method="post" class="form-inline">
<div class="mt-3 mb-1">Change the way random aliases are generated by default.</div>
<form method="post" action="#random-alias" class="form-inline">
<input type="hidden" name="form-name" value="change-alias-generator">
<select class="form-control mr-sm-2" name="alias-generator-scheme">
<option value="{{ AliasGeneratorEnum.word.value }}"
@ -148,6 +149,25 @@
</select>
<button class="btn btn-outline-primary">Update</button>
</form>
{% if current_user.has_custom_domain() %}
<div class="mt-3 mb-1">Select the domain for random aliases.</div>
<form method="post" action="#random-alias" class="form-inline">
<input type="hidden" name="form-name" value="change-random-alias-default-domain">
<select class="form-control mr-sm-2" name="random-alias-default-domain">
<option value="" {% if not current_user.default_random_alias_domain_id %} selected {% endif %}>
{{ FIRST_ALIAS_DOMAIN }} (SimpleLogin domain)
</option>
{% for domain in current_user.custom_domains() %}
<option value="{{ domain.id }}"
{% if current_user.default_random_alias_domain_id == domain.id %} selected {% endif %} >
{{ domain.domain }} (your domain)
</option>
{% endfor %}
</select>
<button class="btn btn-outline-primary">Update</button>
</form>
{% endif %}
</div>
</div>

View file

@ -10,7 +10,7 @@ from wtforms import StringField, validators
from wtforms.fields.html5 import EmailField
from app import s3, email_utils
from app.config import URL
from app.config import URL, FIRST_ALIAS_DOMAIN
from app.dashboard.base import dashboard_bp
from app.email_utils import (
email_domain_can_be_used_as_mailbox,
@ -159,6 +159,30 @@ def setting():
flash("Your preference has been updated", "success")
return redirect(url_for("dashboard.setting"))
elif request.form.get("form-name") == "change-random-alias-default-domain":
default_domain = request.form.get("random-alias-default-domain")
if default_domain:
default_domain_id = int(default_domain)
# sanity check
domain = CustomDomain.get(default_domain_id)
if (
not domain
or domain.user_id != current_user.id
or not domain.verified
):
flash(
"Something went wrong, sorry for the inconvenience. Please retry. ",
"error",
)
return redirect(url_for("dashboard.setting"))
current_user.default_random_alias_domain_id = default_domain_id
else:
current_user.default_random_alias_domain_id = None
db.session.commit()
flash("Your preference has been updated", "success")
return redirect(url_for("dashboard.setting"))
elif request.form.get("form-name") == "change-sender-format":
sender_format = int(request.form.get("sender-format"))
if SenderFormatEnum.has_value(sender_format):
@ -215,6 +239,7 @@ def setting():
pending_email=pending_email,
AliasGeneratorEnum=AliasGeneratorEnum,
manual_sub=manual_sub,
FIRST_ALIAS_DOMAIN=FIRST_ALIAS_DOMAIN,
)

View file

@ -434,6 +434,12 @@ class User(db.Model, ModelMixin, UserMixin):
def nb_directory(self):
return Directory.query.filter_by(user_id=self.id).count()
def has_custom_domain(self):
return CustomDomain.filter_by(user_id=self.id, verified=True).count() > 0
def custom_domains(self):
return CustomDomain.filter_by(user_id=self.id, verified=True).all()
def __repr__(self):
return f"<User {self.id} {self.name} {self.email}>"