add debug zone to auto create page

This commit is contained in:
Son Nguyen Kim 2021-09-21 10:14:36 +02:00
parent 809f547742
commit f160ebec4e
2 changed files with 69 additions and 2 deletions

View File

@ -125,6 +125,39 @@
</div>
<div id="debug-zone">
<hr>
<h3>Debug Zone</h3>
<p>You can test whether an alias will be automatically created given the rules above </p>
<div class="alert alert-info">
No worries, no alias will be created during the test :)
</div>
<form method="post" action="#debug-zone">
<input type="hidden" name="form-name" value="test-auto-create-rule">
{{ auto_create_test_form.csrf_token }}
<div class="d-flex">
<div class="form-group d-flex">
{{ auto_create_test_form.local(class="form-control", type="text", placeholder="local", value=auto_create_test_local) }}
{{ render_field_errors(auto_create_test_form.local) }}
<b class="pt-2 ml-1">@{{ custom_domain.domain }}</b>
</div>
<div>
</div>
</div>
<div class="form-group">
<button class="btn btn-outline-primary">Test</button>
</div>
</form>
{% if auto_create_test_result %}
<div class="alert {% if auto_create_test_passed %} alert-success {% else %} alert-warning {% endif %}">
{{ auto_create_test_result }}
</div>
{% endif %}
</div>
</div>

View File

@ -1,3 +1,4 @@
import re
from threading import Thread
from flask import render_template, request, redirect, url_for, flash
@ -382,6 +383,12 @@ class AutoCreateRuleForm(FlaskForm):
)
class AutoCreateTestForm(FlaskForm):
local = StringField(
"local part", validators=[validators.DataRequired(), validators.Length(max=128)]
)
@dashboard_bp.route(
"/domains/<int:custom_domain_id>/auto-create", methods=["GET", "POST"]
)
@ -391,6 +398,13 @@ def domain_detail_auto_create(custom_domain_id):
mailboxes = current_user.mailboxes()
new_auto_create_rule_form = AutoCreateRuleForm()
auto_create_test_form = AutoCreateTestForm()
auto_create_test_local, auto_create_test_result, auto_create_test_passed = (
"",
"",
False,
)
if not custom_domain or custom_domain.user_id != current_user.id:
flash("You cannot see this page", "warning")
return redirect(url_for("dashboard.index"))
@ -476,6 +490,28 @@ def domain_detail_auto_create(custom_domain_id):
AutoCreateRule.delete(rule_id)
db.session.commit()
flash(f"Rule #{rule_order} has been deleted", "success")
elif request.form.get("form-name") == "test-auto-create-rule":
if auto_create_test_form.validate():
local = auto_create_test_form.local.data
auto_create_test_local = local
for rule in custom_domain.auto_create_rules:
rule: AutoCreateRule
regex = re.compile(rule.regex)
if re.fullmatch(regex, local):
auto_create_test_result = (
f"{local}@{custom_domain.domain} passes rule #{rule.order}"
)
auto_create_test_passed = True
break
else: # no rule passes
auto_create_test_result = (
f"{local}@{custom_domain.domain} doesn't pass any rule"
)
return render_template(
"dashboard/domain_detail/auto-create.html", **locals()
)
return redirect(
url_for(
@ -483,6 +519,4 @@ def domain_detail_auto_create(custom_domain_id):
)
)
nb_alias = Alias.filter_by(custom_domain_id=custom_domain.id).count()
return render_template("dashboard/domain_detail/auto-create.html", **locals())