Use AliasInTrashError instead of DeletedAlias.get_by check when trying to create alias automatically

This commit is contained in:
Son NK 2020-05-23 19:45:26 +02:00
parent c73820920b
commit a4f8dc9c9d

View file

@ -8,6 +8,7 @@ from app.email_utils import (
send_cannot_create_domain_alias, send_cannot_create_domain_alias,
email_belongs_to_alias_domains, email_belongs_to_alias_domains,
) )
from app.errors import AliasInTrashError
from app.extensions import db from app.extensions import db
from app.log import LOG from app.log import LOG
from app.models import ( from app.models import (
@ -61,16 +62,7 @@ def try_auto_create_directory(address: str) -> Optional[Alias]:
send_cannot_create_directory_alias(dir_user, address, directory_name) send_cannot_create_directory_alias(dir_user, address, directory_name)
return None return None
# if alias has been deleted before, do not auto-create it try:
if DeletedAlias.get_by(email=address):
LOG.warning(
"Alias %s was deleted before, cannot auto-create using directory %s, user %s",
address,
directory_name,
dir_user,
)
return None
LOG.d("create alias %s for directory %s", address, directory) LOG.d("create alias %s for directory %s", address, directory)
alias = Alias.create( alias = Alias.create(
@ -82,6 +74,14 @@ def try_auto_create_directory(address: str) -> Optional[Alias]:
db.session.commit() db.session.commit()
return alias return alias
except AliasInTrashError:
LOG.warning(
"Alias %s was deleted before, cannot auto-create using directory %s, user %s",
address,
directory_name,
dir_user,
)
return None
def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]: def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
@ -106,18 +106,8 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
send_cannot_create_domain_alias(domain_user, address, alias_domain) send_cannot_create_domain_alias(domain_user, address, alias_domain)
return None return None
# if alias has been deleted before, do not auto-create it try:
if DeletedAlias.get_by(email=address):
LOG.warning(
"Alias %s was deleted before, cannot auto-create using domain catch-all %s, user %s",
address,
custom_domain,
domain_user,
)
return None
LOG.d("create alias %s for domain %s", address, custom_domain) LOG.d("create alias %s for domain %s", address, custom_domain)
alias = Alias.create( alias = Alias.create(
email=address, email=address,
user_id=custom_domain.user_id, user_id=custom_domain.user_id,
@ -125,9 +115,16 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
automatic_creation=True, automatic_creation=True,
mailbox_id=domain_user.default_mailbox_id, mailbox_id=domain_user.default_mailbox_id,
) )
db.session.commit() db.session.commit()
return alias return alias
except AliasInTrashError:
LOG.warning(
"Alias %s was deleted before, cannot auto-create using domain catch-all %s, user %s",
address,
custom_domain,
domain_user,
)
return None
def delete_alias(alias: Alias, user: User): def delete_alias(alias: Alias, user: User):