handle custom domain in custom alias
This commit is contained in:
parent
3402d64249
commit
0234af03d3
|
@ -9,25 +9,25 @@
|
||||||
{% block default_content %}
|
{% block default_content %}
|
||||||
|
|
||||||
<div class="col-md-6 offset-md-3">
|
<div class="col-md-6 offset-md-3">
|
||||||
|
<h1 class="h3 mb-5">Custom Alias</h1>
|
||||||
{% if error %}
|
{% if error %}
|
||||||
<div class="text-danger text-center mb-4">{{ error }}</div>
|
<div class="text-danger text-center mb-4">{{ error }}</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<form method="post" enctype="multipart/form-data">
|
<form method="post">
|
||||||
{{ form.csrf_token }}
|
<input type="hidden" name="form-name" value="non-custom-domain-name">
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="form-label">Custom Alias</label>
|
<label class="form-label font-weight-bold">SimpleLogin domain</label>
|
||||||
<small class="text-muted">
|
<small class="text-muted">
|
||||||
You can use letter, number or dash. Alias must have at least 3 characters
|
You can use letter, number or dash. Alias must have at least 3 characters
|
||||||
</small>
|
</small>
|
||||||
|
|
||||||
<div class="row mt-4">
|
<div class="row mt-4">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
{{ form.email(class="form-control",
|
<input name="email-prefix" class="form-control" pattern="[0-9|A-Z|a-z|-]{3,}" ,
|
||||||
pattern="[0-9|A-Z|a-z|-]{3,}",
|
title="Only letter, number or dash can be used and alias must have at least 3 characters." ,
|
||||||
title="Only letter, number or dash can be used and alias must have at least 3 characters.",
|
autofocus>
|
||||||
autofocus=True) }}
|
|
||||||
{{ render_field_errors(form.email) }}
|
|
||||||
</div>
|
</div>
|
||||||
<div class="col align-self-center">
|
<div class="col align-self-center">
|
||||||
<input type="hidden" name="email-suffix" value="{{ email_suffix }}">
|
<input type="hidden" name="email-suffix" value="{{ email_suffix }}">
|
||||||
|
@ -40,6 +40,33 @@
|
||||||
|
|
||||||
<button class="btn btn-primary">Create</button>
|
<button class="btn btn-primary">Create</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
{% if custom_domains %}
|
||||||
|
<hr>
|
||||||
|
<p class="font-weight-bold">OR with your custom domains</p>
|
||||||
|
{% for custom_domain in custom_domains %}
|
||||||
|
<form method="post" class="mb-6">
|
||||||
|
<input type="hidden" name="form-name" value="custom-domain-name">
|
||||||
|
<input type="hidden" name="custom-domain-id" value="{{ custom_domain.id }}">
|
||||||
|
|
||||||
|
<div class="row mt-4">
|
||||||
|
<div class="col">
|
||||||
|
<input name="email" class="form-control">
|
||||||
|
</div>
|
||||||
|
<div class="col align-self-center">
|
||||||
|
<input type="hidden" name="email-suffix" value="{{ email_suffix }}">
|
||||||
|
<h4>
|
||||||
|
@{{ custom_domain.domain }}
|
||||||
|
</h4>
|
||||||
|
</div>
|
||||||
|
<div class="col">
|
||||||
|
<button class="submit btn btn-sm btn-success">Create</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,20 +1,14 @@
|
||||||
from flask import render_template, redirect, url_for, flash, request, session
|
from flask import render_template, redirect, url_for, flash, request, session
|
||||||
from flask_login import login_required, current_user
|
from flask_login import login_required, current_user
|
||||||
from flask_wtf import FlaskForm
|
|
||||||
from wtforms import StringField, validators
|
|
||||||
|
|
||||||
from app.config import EMAIL_DOMAIN, HIGHLIGHT_GEN_EMAIL_ID
|
from app.config import EMAIL_DOMAIN, HIGHLIGHT_GEN_EMAIL_ID
|
||||||
from app.dashboard.base import dashboard_bp
|
from app.dashboard.base import dashboard_bp
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.log import LOG
|
from app.log import LOG
|
||||||
from app.models import GenEmail, DeletedAlias
|
from app.models import GenEmail, DeletedAlias, CustomDomain
|
||||||
from app.utils import convert_to_id, random_string
|
from app.utils import convert_to_id, random_string
|
||||||
|
|
||||||
|
|
||||||
class CustomAliasForm(FlaskForm):
|
|
||||||
email = StringField("Email", validators=[validators.DataRequired()])
|
|
||||||
|
|
||||||
|
|
||||||
@dashboard_bp.route("/custom_alias", methods=["GET", "POST"])
|
@dashboard_bp.route("/custom_alias", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def custom_alias():
|
def custom_alias():
|
||||||
|
@ -25,18 +19,18 @@ def custom_alias():
|
||||||
flash("ony premium user can choose custom alias", "warning")
|
flash("ony premium user can choose custom alias", "warning")
|
||||||
return redirect(url_for("dashboard.index"))
|
return redirect(url_for("dashboard.index"))
|
||||||
|
|
||||||
form = CustomAliasForm()
|
|
||||||
error = ""
|
error = ""
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if request.method == "POST":
|
||||||
email = form.email.data
|
if request.form.get("form-name") == "non-custom-domain-name":
|
||||||
email = convert_to_id(email)
|
email_prefix = request.form.get("email-prefix")
|
||||||
|
email_prefix = convert_to_id(email_prefix)
|
||||||
email_suffix = request.form.get("email-suffix")
|
email_suffix = request.form.get("email-suffix")
|
||||||
|
|
||||||
if len(email) < 3:
|
if len(email_prefix) < 3:
|
||||||
error = "email must be at least 3 letters"
|
error = "email must be at least 3 letters"
|
||||||
else:
|
else:
|
||||||
full_email = f"{email}.{email_suffix}@{EMAIL_DOMAIN}"
|
full_email = f"{email_prefix}.{email_suffix}@{EMAIL_DOMAIN}"
|
||||||
# check if email already exists
|
# check if email already exists
|
||||||
if GenEmail.get_by(email=full_email) or DeletedAlias.get_by(
|
if GenEmail.get_by(email=full_email) or DeletedAlias.get_by(
|
||||||
email=full_email
|
email=full_email
|
||||||
|
@ -44,22 +38,62 @@ def custom_alias():
|
||||||
error = "email already chosen, please choose another one"
|
error = "email already chosen, please choose another one"
|
||||||
else:
|
else:
|
||||||
# create the new alias
|
# create the new alias
|
||||||
LOG.d("create custom alias %s for user %s", full_email, current_user)
|
LOG.d(
|
||||||
|
"create custom alias %s for user %s", full_email, current_user
|
||||||
|
)
|
||||||
gen_email = GenEmail.create(
|
gen_email = GenEmail.create(
|
||||||
email=full_email, user_id=current_user.id, custom=True
|
email=full_email, user_id=current_user.id, custom=True
|
||||||
)
|
)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
flash(f"Email alias {full_email} has been created", "success")
|
flash(f"Alias {full_email} has been created", "success")
|
||||||
session[HIGHLIGHT_GEN_EMAIL_ID] = gen_email.id
|
session[HIGHLIGHT_GEN_EMAIL_ID] = gen_email.id
|
||||||
|
|
||||||
return redirect(url_for("dashboard.index"))
|
return redirect(url_for("dashboard.index"))
|
||||||
|
elif request.form.get("form-name") == "custom-domain-name":
|
||||||
|
custom_domain_id = request.form.get("custom-domain-id")
|
||||||
|
email = request.form.get("email")
|
||||||
|
|
||||||
|
custom_domain = CustomDomain.get(custom_domain_id)
|
||||||
|
|
||||||
|
if not custom_domain:
|
||||||
|
flash("Unknown error. Refresh the page", "warning")
|
||||||
|
return redirect(url_for("dashboard.custom_alias"))
|
||||||
|
elif custom_domain.user_id != current_user.id:
|
||||||
|
flash("Unknown error. Refresh the page", "warning")
|
||||||
|
return redirect(url_for("dashboard.custom_alias"))
|
||||||
|
elif not custom_domain.verified:
|
||||||
|
flash("Unknown error. Refresh the page", "warning")
|
||||||
|
return redirect(url_for("dashboard.custom_alias"))
|
||||||
|
|
||||||
|
full_email = f"{email}@{custom_domain.domain}"
|
||||||
|
|
||||||
|
if GenEmail.get_by(email=full_email):
|
||||||
|
error = f"{full_email} already exist, please choose another one"
|
||||||
|
else:
|
||||||
|
LOG.d(
|
||||||
|
"create custom alias %s for custom domain %s",
|
||||||
|
full_email,
|
||||||
|
custom_domain.domain,
|
||||||
|
)
|
||||||
|
# todo: add custom_id to GenEmail
|
||||||
|
gen_email = GenEmail.create(
|
||||||
|
email=full_email,
|
||||||
|
user_id=current_user.id,
|
||||||
|
custom=True,
|
||||||
|
custom_domain_id=custom_domain.id,
|
||||||
|
)
|
||||||
|
db.session.commit()
|
||||||
|
flash(f"Alias {full_email} has been created", "success")
|
||||||
|
|
||||||
|
session[HIGHLIGHT_GEN_EMAIL_ID] = gen_email.id
|
||||||
|
return redirect(url_for("dashboard.index"))
|
||||||
|
|
||||||
email_suffix = random_string(6)
|
email_suffix = random_string(6)
|
||||||
return render_template(
|
return render_template(
|
||||||
"dashboard/custom_alias.html",
|
"dashboard/custom_alias.html",
|
||||||
form=form,
|
|
||||||
error=error,
|
error=error,
|
||||||
email_suffix=email_suffix,
|
email_suffix=email_suffix,
|
||||||
EMAIL_DOMAIN=EMAIL_DOMAIN,
|
EMAIL_DOMAIN=EMAIL_DOMAIN,
|
||||||
|
custom_domains=current_user.verified_custom_domains(),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue