mirror of
https://github.com/simple-login/app.git
synced 2025-02-23 15:23:27 +08:00
use job system for deleting mailbox
This commit is contained in:
parent
3a0b125323
commit
fbabe6fb44
4 changed files with 50 additions and 10 deletions
|
@ -1,10 +1,12 @@
|
|||
from smtplib import SMTPRecipientsRefused
|
||||
|
||||
import arrow
|
||||
from flask import g
|
||||
from flask import jsonify
|
||||
from flask import request
|
||||
|
||||
from app.api.base import api_bp, require_api_auth
|
||||
from app.config import JOB_DELETE_MAILBOX
|
||||
from app.dashboard.views.mailbox import send_verification_email
|
||||
from app.dashboard.views.mailbox_detail import verify_mailbox_change
|
||||
from app.db import Session
|
||||
|
@ -13,7 +15,8 @@ from app.email_utils import (
|
|||
email_can_be_used_as_mailbox,
|
||||
is_valid_email,
|
||||
)
|
||||
from app.models import Mailbox
|
||||
from app.log import LOG
|
||||
from app.models import Mailbox, Job
|
||||
from app.utils import sanitize_email
|
||||
|
||||
|
||||
|
@ -88,8 +91,14 @@ def delete_mailbox(mailbox_id):
|
|||
if mailbox.id == user.default_mailbox_id:
|
||||
return jsonify(error="You cannot delete the default mailbox"), 400
|
||||
|
||||
Mailbox.delete(mailbox_id)
|
||||
Session.commit()
|
||||
# Schedule delete account job
|
||||
LOG.w("schedule delete mailbox job for %s", mailbox)
|
||||
Job.create(
|
||||
name=JOB_DELETE_MAILBOX,
|
||||
payload={"mailbox_id": mailbox.id},
|
||||
run_at=arrow.now(),
|
||||
commit=True,
|
||||
)
|
||||
|
||||
return jsonify(deleted=True), 200
|
||||
|
||||
|
|
|
@ -255,6 +255,7 @@ JOB_ONBOARDING_3 = "onboarding-3"
|
|||
JOB_ONBOARDING_4 = "onboarding-4"
|
||||
JOB_BATCH_IMPORT = "batch-import"
|
||||
JOB_DELETE_ACCOUNT = "delete-account"
|
||||
JOB_DELETE_MAILBOX = "delete-mailbox"
|
||||
|
||||
# for pagination
|
||||
PAGE_LIMIT = 20
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from threading import Thread
|
||||
|
||||
import arrow
|
||||
from flask import render_template, request, redirect, url_for, flash
|
||||
from flask_login import login_required, current_user
|
||||
from flask_wtf import FlaskForm
|
||||
|
@ -7,7 +6,7 @@ from itsdangerous import Signer
|
|||
from wtforms import validators
|
||||
from wtforms.fields.html5 import EmailField
|
||||
|
||||
from app.config import MAILBOX_SECRET, URL
|
||||
from app.config import MAILBOX_SECRET, URL, JOB_DELETE_MAILBOX
|
||||
from app.dashboard.base import dashboard_bp
|
||||
from app.db import Session
|
||||
from app.email_utils import (
|
||||
|
@ -18,7 +17,7 @@ from app.email_utils import (
|
|||
is_valid_email,
|
||||
)
|
||||
from app.log import LOG
|
||||
from app.models import Mailbox
|
||||
from app.models import Mailbox, Job
|
||||
|
||||
|
||||
class NewMailboxForm(FlaskForm):
|
||||
|
@ -51,8 +50,15 @@ def mailbox_route():
|
|||
flash("You cannot delete default mailbox", "error")
|
||||
return redirect(url_for("dashboard.mailbox_route"))
|
||||
|
||||
LOG.d("Schedule deleting %s", mailbox)
|
||||
Thread(target=delete_mailbox, args=(mailbox.id,)).start()
|
||||
# Schedule delete account job
|
||||
LOG.w("schedule delete mailbox job for %s", mailbox)
|
||||
Job.create(
|
||||
name=JOB_DELETE_MAILBOX,
|
||||
payload={"mailbox_id": mailbox.id},
|
||||
run_at=arrow.now(),
|
||||
commit=True,
|
||||
)
|
||||
|
||||
flash(
|
||||
f"Mailbox {mailbox.email} scheduled for deletion."
|
||||
f"You will receive a confirmation email when the deletion is finished",
|
||||
|
|
|
@ -12,6 +12,7 @@ from app.config import (
|
|||
JOB_ONBOARDING_4,
|
||||
JOB_BATCH_IMPORT,
|
||||
JOB_DELETE_ACCOUNT,
|
||||
JOB_DELETE_MAILBOX,
|
||||
)
|
||||
from app.db import Session
|
||||
from app.email_utils import (
|
||||
|
@ -20,7 +21,7 @@ from app.email_utils import (
|
|||
)
|
||||
from app.import_utils import handle_batch_import
|
||||
from app.log import LOG
|
||||
from app.models import User, Job, BatchImport
|
||||
from app.models import User, Job, BatchImport, Mailbox
|
||||
from server import create_light_app
|
||||
|
||||
|
||||
|
@ -163,6 +164,29 @@ if __name__ == "__main__":
|
|||
render("transactional/account-delete.txt"),
|
||||
render("transactional/account-delete.html"),
|
||||
)
|
||||
elif job.name == JOB_DELETE_MAILBOX:
|
||||
mailbox_id = job.payload.get("mailbox_id")
|
||||
mailbox = Mailbox.get(mailbox_id)
|
||||
if not mailbox:
|
||||
continue
|
||||
|
||||
mailbox_email = mailbox.email
|
||||
user = mailbox.user
|
||||
|
||||
Mailbox.delete(mailbox_id)
|
||||
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.
|
||||
""",
|
||||
)
|
||||
|
||||
else:
|
||||
LOG.e("Unknown job name %s", job.name)
|
||||
|
||||
|
|
Loading…
Reference in a new issue