From 9898d857225f6e1f1e02d5ff7de3cfaeb0cf2699 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Thu, 7 May 2020 22:42:39 +0200 Subject: [PATCH] Put all aliases belonging to a directory to global trash when this directory is deleted --- app/models.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/models.py b/app/models.py index 3c38700c..10afbf25 100644 --- a/app/models.py +++ b/app/models.py @@ -1107,6 +1107,21 @@ class Directory(db.Model, ModelMixin): def nb_alias(self): return Alias.filter_by(directory_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(directory_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""