From e5308932a2d013485553b5b7e87eb07cf3ad28a0 Mon Sep 17 00:00:00 2001 From: Son Date: Sun, 15 Aug 2021 17:50:47 +0200 Subject: [PATCH] make mailbox deletion async --- app/dashboard/views/domain_detail.py | 2 +- app/dashboard/views/mailbox.py | 39 +++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/dashboard/views/domain_detail.py b/app/dashboard/views/domain_detail.py index 6d65bea6..9f6eb746 100644 --- a/app/dashboard/views/domain_detail.py +++ b/app/dashboard/views/domain_detail.py @@ -208,7 +208,7 @@ def domain_detail(custom_domain_id): return render_template("dashboard/domain_detail/info.html", **locals()) -def delete_domain(custom_domain_id: CustomDomain): +def delete_domain(custom_domain_id: int): from server import create_light_app with create_light_app().app_context(): diff --git a/app/dashboard/views/mailbox.py b/app/dashboard/views/mailbox.py index c3ee81ac..42774014 100644 --- a/app/dashboard/views/mailbox.py +++ b/app/dashboard/views/mailbox.py @@ -1,3 +1,5 @@ +from threading import Thread + from flask import render_template, request, redirect, url_for, flash from flask_login import login_required, current_user from flask_wtf import FlaskForm @@ -49,10 +51,13 @@ def mailbox_route(): flash("You cannot delete default mailbox", "error") return redirect(url_for("dashboard.mailbox_route")) - email = mailbox.email - Mailbox.delete(mailbox_id) - db.session.commit() - flash(f"Mailbox {email} has been deleted", "success") + LOG.d("Schedule deleting %s", mailbox) + Thread(target=delete_mailbox, args=(mailbox.id,)).start() + flash( + f"Mailbox {mailbox.email} scheduled for deletion." + f"You will receive a confirmation email when the deletion is finished", + "success", + ) return redirect(url_for("dashboard.mailbox_route")) if request.form.get("form-name") == "set-default": @@ -119,6 +124,32 @@ def mailbox_route(): ) +def delete_mailbox(mailbox_id: int): + from server import create_light_app + + with create_light_app().app_context(): + mailbox = Mailbox.get(mailbox_id) + if not mailbox: + return + + mailbox_email = mailbox.email + user = mailbox.user + + Mailbox.delete(mailbox_id) + db.session.commit() + LOG.d("Mailbox %s %s deleted", mailbox_id,mailbox_email) + + send_email( + user.email, + f"Your mailbox {mailbox_email} has been deleted", + f"""Mailbox {mailbox_email} along with its aliases are deleted successfully. + +Regards, +SimpleLogin team. + """, + ) + + def send_verification_email(user, mailbox): s = Signer(MAILBOX_SECRET) mailbox_id_signed = s.sign(str(mailbox.id)).decode()