Merge pull request #263 from simple-login/fix-alias-trash
Fix alias trash
This commit is contained in:
commit
edb8144be8
|
@ -142,14 +142,17 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
|
||||||
|
|
||||||
|
|
||||||
def delete_alias(alias: Alias, user: User):
|
def delete_alias(alias: Alias, user: User):
|
||||||
Alias.delete(alias.id)
|
"""
|
||||||
db.session.commit()
|
Delete an alias and add it to either global or domain trash
|
||||||
|
Should be used instead of Alias.delete, DomainDeletedAlias.create, DeletedAlias.create
|
||||||
|
"""
|
||||||
# save deleted alias to either global or domain trash
|
# save deleted alias to either global or domain trash
|
||||||
if alias.custom_domain_id:
|
if alias.custom_domain_id:
|
||||||
try:
|
try:
|
||||||
DomainDeletedAlias.create(
|
db.session.add(
|
||||||
user_id=user.id, email=alias.email, domain_id=alias.custom_domain_id
|
DomainDeletedAlias(
|
||||||
|
user_id=user.id, email=alias.email, domain_id=alias.custom_domain_id
|
||||||
|
)
|
||||||
)
|
)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
|
@ -161,8 +164,11 @@ def delete_alias(alias: Alias, user: User):
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
DeletedAlias.create(email=alias.email)
|
db.session.add(DeletedAlias(email=alias.email))
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
LOG.exception("alias %s has been added before to DeletedAlias", alias.email)
|
LOG.exception("alias %s has been added before to DeletedAlias", alias.email)
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
|
|
||||||
|
Alias.query.filter(Alias.id == alias.id).delete()
|
||||||
|
db.session.commit()
|
||||||
|
|
|
@ -10,7 +10,6 @@ from arrow import Arrow
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
from flask_login import UserMixin
|
from flask_login import UserMixin
|
||||||
from sqlalchemy import text, desc, CheckConstraint, and_, func
|
from sqlalchemy import text, desc, CheckConstraint, and_, func
|
||||||
from sqlalchemy.exc import IntegrityError
|
|
||||||
from sqlalchemy_utils import ArrowType
|
from sqlalchemy_utils import ArrowType
|
||||||
|
|
||||||
from app import s3
|
from app import s3
|
||||||
|
@ -870,6 +869,10 @@ class Alias(db.Model, ModelMixin):
|
||||||
mailbox_id=mailbox_id or user.default_mailbox_id,
|
mailbox_id=mailbox_id or user.default_mailbox_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def delete(cls, obj_id):
|
||||||
|
raise Exception("should use delete_alias(alias,user) instead")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_new_random(
|
def create_new_random(
|
||||||
cls,
|
cls,
|
||||||
|
@ -1292,6 +1295,10 @@ class DeletedAlias(db.Model, ModelMixin):
|
||||||
|
|
||||||
email = db.Column(db.String(256), unique=True, nullable=False)
|
email = db.Column(db.String(256), unique=True, nullable=False)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create(cls, **kw):
|
||||||
|
raise Exception("should use delete_alias(alias,user) instead")
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<Deleted Alias {self.email}>"
|
return f"<Deleted Alias {self.email}>"
|
||||||
|
|
||||||
|
@ -1411,6 +1418,10 @@ class DomainDeletedAlias(db.Model, ModelMixin):
|
||||||
)
|
)
|
||||||
user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False)
|
user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create(cls, **kw):
|
||||||
|
raise Exception("should use delete_alias(alias,user) instead")
|
||||||
|
|
||||||
|
|
||||||
class LifetimeCoupon(db.Model, ModelMixin):
|
class LifetimeCoupon(db.Model, ModelMixin):
|
||||||
code = db.Column(db.String(128), nullable=False, unique=True)
|
code = db.Column(db.String(128), nullable=False, unique=True)
|
||||||
|
@ -1439,15 +1450,13 @@ class Directory(db.Model, ModelMixin):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def delete(cls, obj_id):
|
def delete(cls, obj_id):
|
||||||
# Put all aliases belonging to this directory to global trash
|
obj: Directory = cls.get(obj_id)
|
||||||
try:
|
user = obj.user
|
||||||
for alias in Alias.query.filter_by(directory_id=obj_id):
|
# Put all aliases belonging to this directory to global or domain trash
|
||||||
DeletedAlias.create(email=alias.email)
|
for alias in Alias.query.filter_by(directory_id=obj_id):
|
||||||
db.session.commit()
|
from app import alias_utils
|
||||||
# this can happen when a previously deleted alias is re-created via catch-all or directory feature
|
|
||||||
except IntegrityError:
|
alias_utils.delete_alias(alias, user)
|
||||||
LOG.exception("Some aliases have been added before to DeletedAlias")
|
|
||||||
db.session.rollback()
|
|
||||||
|
|
||||||
cls.query.filter(cls.id == obj_id).delete()
|
cls.query.filter(cls.id == obj_id).delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -1501,23 +1510,23 @@ class Mailbox(db.Model, ModelMixin):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def delete(cls, obj_id):
|
def delete(cls, obj_id):
|
||||||
# Put all aliases belonging to this mailbox to global trash
|
mailbox: Mailbox = cls.get(obj_id)
|
||||||
try:
|
user = mailbox.user
|
||||||
for alias in Alias.query.filter_by(mailbox_id=obj_id):
|
|
||||||
# special handling for alias that has several mailboxes and has mailbox_id=obj_id
|
# Put all aliases belonging to this mailbox to global or domain trash
|
||||||
if len(alias.mailboxes) > 1:
|
for alias in Alias.query.filter_by(mailbox_id=obj_id):
|
||||||
# use the first mailbox found in alias._mailboxes
|
# special handling for alias that has several mailboxes and has mailbox_id=obj_id
|
||||||
first_mb = alias._mailboxes[0]
|
if len(alias.mailboxes) > 1:
|
||||||
alias.mailbox_id = first_mb.id
|
# use the first mailbox found in alias._mailboxes
|
||||||
alias._mailboxes.remove(first_mb)
|
first_mb = alias._mailboxes[0]
|
||||||
else:
|
alias.mailbox_id = first_mb.id
|
||||||
# only put aliases that have mailbox as a single mailbox into trash
|
alias._mailboxes.remove(first_mb)
|
||||||
DeletedAlias.create(email=alias.email)
|
else:
|
||||||
db.session.commit()
|
from app import alias_utils
|
||||||
# this can happen when a previously deleted alias is re-created via catch-all or directory feature
|
|
||||||
except IntegrityError:
|
# only put aliases that have mailbox as a single mailbox into trash
|
||||||
LOG.exception("Some aliases have been added before to DeletedAlias")
|
alias_utils.delete_alias(alias, user)
|
||||||
db.session.rollback()
|
db.session.commit()
|
||||||
|
|
||||||
cls.query.filter(cls.id == obj_id).delete()
|
cls.query.filter(cls.id == obj_id).delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -275,10 +275,6 @@ def fake_data():
|
||||||
|
|
||||||
ClientUser.create(user_id=user.id, client_id=client1.id, name="Fake Name")
|
ClientUser.create(user_id=user.id, client_id=client1.id, name="Fake Name")
|
||||||
|
|
||||||
DeletedAlias.create(email="d1@ab.cd")
|
|
||||||
DeletedAlias.create(email="d2@ab.cd")
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
referral = Referral.create(user_id=user.id, code="REFCODE", name="First referral")
|
referral = Referral.create(user_id=user.id, code="REFCODE", name="First referral")
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
12
shell.py
12
shell.py
|
@ -106,11 +106,13 @@ def migrate_domain_trash():
|
||||||
domain = CustomDomain.get_by(domain=alias_domain)
|
domain = CustomDomain.get_by(domain=alias_domain)
|
||||||
if domain:
|
if domain:
|
||||||
LOG.d("move %s to domain %s trash", deleted_alias, domain)
|
LOG.d("move %s to domain %s trash", deleted_alias, domain)
|
||||||
DomainDeletedAlias.create(
|
db.session.add(
|
||||||
user_id=domain.user_id,
|
DomainDeletedAlias(
|
||||||
email=deleted_alias.email,
|
user_id=domain.user_id,
|
||||||
domain_id=domain.id,
|
email=deleted_alias.email,
|
||||||
created_at=deleted_alias.created_at,
|
domain_id=domain.id,
|
||||||
|
created_at=deleted_alias.created_at,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
DeletedAlias.delete(deleted_alias.id)
|
DeletedAlias.delete(deleted_alias.id)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue