From f5bc166f3959f27257dd33f6ac22a4c3902432cf Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 1 Aug 2020 12:31:02 +0200 Subject: [PATCH] able to choose mailboxes for a domain --- .../templates/dashboard/custom_domain.html | 93 ++++++++++++++++--- app/dashboard/views/custom_domain.py | 64 ++++++++++++- 2 files changed, 143 insertions(+), 14 deletions(-) diff --git a/app/dashboard/templates/dashboard/custom_domain.html b/app/dashboard/templates/dashboard/custom_domain.html index 34be64af..1ebc7008 100644 --- a/app/dashboard/templates/dashboard/custom_domain.html +++ b/app/dashboard/templates/dashboard/custom_domain.html @@ -20,7 +20,10 @@ {% if not current_user.is_premium() %} {% endif %} @@ -46,33 +49,99 @@ {% endif %} -
+ +
Created {{ custom_domain.created_at | dt }}
{{ custom_domain.nb_alias() }} aliases. +

+ + Mailboxes: + + +
+ + {% set domain_mailboxes=custom_domain.mailboxes %} +
+ + + +
+
+ +
+
+ +
+
+
+
- Details ➡ + + Details ➡ + {% endfor %} -
- {{ new_custom_domain_form.csrf_token }} - +
+
+
+
-

New Domain

+ + {{ new_custom_domain_form.csrf_token }} + - {{ new_custom_domain_form.domain(class="form-control", placeholder="my-domain.com", maxlength=128) }} - {{ render_field_errors(new_custom_domain_form.domain) }} -
Please use full path domain, for ex my-subdomain.my-domain.com
+

New Domain

- - + {{ new_custom_domain_form.domain(class="form-control", placeholder="my-domain.com", maxlength=128) }} + {{ render_field_errors(new_custom_domain_form.domain) }} +
+ Please use full path domain, for ex my-subdomain.my-domain.com +
+
+ By default, aliases created with your domain are "owned" by your default + mailbox {{ current_user.default_mailbox.email }}.
+ This below option allow you to choose the mailbox(es) that a new alias automatically belongs to. +
+ + + + + +
+
+
+
{% endblock %} + +{% block script %} + +{% endblock %} \ No newline at end of file diff --git a/app/dashboard/views/custom_domain.py b/app/dashboard/views/custom_domain.py index b6953c4d..bcf96323 100644 --- a/app/dashboard/views/custom_domain.py +++ b/app/dashboard/views/custom_domain.py @@ -7,7 +7,7 @@ from app.config import EMAIL_SERVERS_WITH_PRIORITY from app.dashboard.base import dashboard_bp from app.email_utils import get_email_domain_part from app.extensions import db -from app.models import CustomDomain +from app.models import CustomDomain, Mailbox, DomainMailbox class NewCustomDomainForm(FlaskForm): @@ -20,7 +20,7 @@ class NewCustomDomainForm(FlaskForm): @login_required def custom_domain(): custom_domains = CustomDomain.query.filter_by(user_id=current_user.id).all() - + mailboxes = current_user.mailboxes() new_custom_domain_form = NewCustomDomainForm() errors = {} @@ -54,6 +54,28 @@ def custom_domain(): ) db.session.commit() + mailbox_ids = request.form.getlist("mailbox_ids") + if mailbox_ids: + # check if mailbox is not tempered with + mailboxes = [] + for mailbox_id in mailbox_ids: + mailbox = Mailbox.get(mailbox_id) + if ( + not mailbox + or mailbox.user_id != current_user.id + or not mailbox.verified + ): + flash("Something went wrong, please retry", "warning") + return redirect(url_for("dashboard.custom_domain")) + mailboxes.append(mailbox) + + for mailbox in mailboxes: + DomainMailbox.create( + domain_id=new_custom_domain.id, mailbox_id=mailbox.id + ) + + db.session.commit() + flash( f"New domain {new_custom_domain.domain} is created", "success" ) @@ -64,6 +86,43 @@ def custom_domain(): custom_domain_id=new_custom_domain.id, ) ) + elif request.form.get("form-name") == "update": + domain_id = request.form.get("domain-id") + domain = CustomDomain.get(domain_id) + + if not domain or domain.user_id != current_user.id: + flash("Unknown error. Refresh the page", "warning") + return redirect(url_for("dashboard.custom_domain")) + + mailbox_ids = request.form.getlist("mailbox_ids") + # check if mailbox is not tempered with + mailboxes = [] + for mailbox_id in mailbox_ids: + mailbox = Mailbox.get(mailbox_id) + if ( + not mailbox + or mailbox.user_id != current_user.id + or not mailbox.verified + ): + flash("Something went wrong, please retry", "warning") + return redirect(url_for("dashboard.custom_domain")) + mailboxes.append(mailbox) + + if not mailboxes: + flash("You must select at least 1 mailbox", "warning") + return redirect(url_for("dashboard.custom_domain")) + + # first remove all existing domain-mailboxes links + DomainMailbox.query.filter_by(domain_id=domain.id).delete() + db.session.flush() + + for mailbox in mailboxes: + DomainMailbox.create(domain_id=domain.id, mailbox_id=mailbox.id) + + db.session.commit() + flash(f"Domain {domain.domain} has been updated", "success") + + return redirect(url_for("dashboard.custom_domain")) return render_template( "dashboard/custom_domain.html", @@ -71,4 +130,5 @@ def custom_domain(): new_custom_domain_form=new_custom_domain_form, EMAIL_SERVERS_WITH_PRIORITY=EMAIL_SERVERS_WITH_PRIORITY, errors=errors, + mailboxes=mailboxes, )