Put all aliases belonging to a domain to global trash when the domain is deleted

This commit is contained in:
Son NK 2020-05-07 22:50:45 +02:00
parent 9898d85722
commit cf35fe2646
1 changed files with 21 additions and 7 deletions

View File

@ -1086,6 +1086,20 @@ class CustomDomain(db.Model, ModelMixin):
user = db.relationship(User)
@classmethod
def delete(cls, obj_id):
# Put all aliases belonging to this domain to global trash
try:
for alias in Alias.query.filter_by(custom_domain_id=obj_id):
DeletedAlias.create(email=alias.email)
db.session.commit()
except IntegrityError:
LOG.error("Some aliases have been added before to DeletedAlias")
db.session.rollback()
cls.query.filter(cls.id == obj_id).delete()
db.session.commit()
def nb_alias(self):
return Alias.filter_by(custom_domain_id=self.id).count()
@ -1109,10 +1123,7 @@ class Directory(db.Model, ModelMixin):
@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
# Put all aliases belonging to this directory to global trash
try:
for alias in Alias.query.filter_by(directory_id=obj_id):
DeletedAlias.create(email=alias.email)
@ -1122,6 +1133,9 @@ class Directory(db.Model, ModelMixin):
LOG.error("Some aliases have been added before to DeletedAlias")
db.session.rollback()
cls.query.filter(cls.id == obj_id).delete()
db.session.commit()
def __repr__(self):
return f"<Directory {self.name}>"
@ -1158,9 +1172,6 @@ class Mailbox(db.Model, ModelMixin):
@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):
@ -1171,6 +1182,9 @@ class Mailbox(db.Model, ModelMixin):
LOG.error("Some aliases have been added before to DeletedAlias")
db.session.rollback()
cls.query.filter(cls.id == obj_id).delete()
db.session.commit()
def __repr__(self):
return f"<Mailbox {self.email}>"