Put all aliases belonging to a mailbox to global trash when this mailbox is deleted

This commit is contained in:
Son NK 2020-05-07 22:40:53 +02:00
parent 0441e5e2a9
commit 7a1f944887
1 changed files with 16 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import bcrypt
from flask import url_for
from flask_login import UserMixin
from sqlalchemy import text, desc, CheckConstraint
from sqlalchemy.exc import IntegrityError
from sqlalchemy_utils import ArrowType
from app import s3
@ -1140,6 +1141,21 @@ class Mailbox(db.Model, ModelMixin):
def nb_alias(self):
return Alias.filter_by(mailbox_id=self.id).count()
@classmethod
def delete(cls, obj_id):
cls.query.filter(cls.id == obj_id).delete()
db.session.commit()
# Put all aliases belonging to this mailbox to global trash
try:
for alias in Alias.query.filter_by(mailbox_id=obj_id):
DeletedAlias.create(email=alias.email)
db.session.commit()
# this can happen when a previously deleted alias is re-created via catch-all or directory feature
except IntegrityError:
LOG.error("Some aliases have been added before to DeletedAlias")
db.session.rollback()
def __repr__(self):
return f"<Mailbox {self.email}>"