custom email must have .suffix

This commit is contained in:
Son NK 2019-07-22 21:12:22 +02:00 committed by Son NK
parent a1c65d3921
commit 2a6021c230
2 changed files with 22 additions and 12 deletions

View file

@ -19,19 +19,20 @@
<div class="form-group">
<label class="form-label">Custom Alias</label>
<small class="text-muted">
Email must use alphanumeric characters and must be at least 6 characters
Email must use alphanumeric characters and must be at least 3 characters
</small>
<div class="row mt-4">
<div class="col">
{{ form.email(class="form-control",
pattern="[0-9|A-Z|a-z]{6,}",
title="Email must use alphanumeric characters and must be at least 6 characters.") }}
pattern="[0-9|A-Z|a-z]{3,}",
title="Email must use alphanumeric characters and must be at least 3 characters.") }}
{{ render_field_errors(form.email) }}
</div>
<div class="col align-self-center">
<h4>
@simplelogin.co
</h4>
<input type="hidden" name="email-suffix" value="{{ email_suffix }}">
<h4>
.{{ email_suffix }}@{{ EMAIL_DOMAIN }}
</h4>
</div>
</div>
</div>

View file

@ -1,4 +1,4 @@
from flask import render_template, redirect, url_for, flash
from flask import render_template, redirect, url_for, flash, request
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, validators
@ -8,7 +8,7 @@ from app.dashboard.base import dashboard_bp
from app.extensions import db
from app.log import LOG
from app.models import GenEmail
from app.utils import convert_to_id
from app.utils import convert_to_id, random_string
class CustomAliasForm(FlaskForm):
@ -31,10 +31,12 @@ def custom_alias():
if form.validate_on_submit():
email = form.email.data
email = convert_to_id(email)
if len(email) < 6:
error = "email must be at least 6 letters"
email_suffix = request.form.get("email-suffix")
if len(email) < 3:
error = "email must be at least 3 letters"
else:
full_email = f"{email}@{EMAIL_DOMAIN}"
full_email = f"{email}.{email_suffix}@{EMAIL_DOMAIN}"
# check if email already exists
if GenEmail.get_by(email=full_email):
error = "email already chosen, please choose another one"
@ -48,4 +50,11 @@ def custom_alias():
return redirect(url_for("dashboard.index"))
return render_template("dashboard/custom_alias.html", form=form, error=error)
email_suffix = random_string(6)
return render_template(
"dashboard/custom_alias.html",
form=form,
error=error,
email_suffix=email_suffix,
EMAIL_DOMAIN=EMAIL_DOMAIN,
)