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,
email_belongs_to_alias_domains,
)
from app.errors import AliasInTrashError
from app.extensions import db
from app.log import LOG
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)
return None
# if alias has been deleted before, do not auto-create it
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
try:
LOG.d("create alias %s for directory %s", address, directory)
alias = Alias.create(
@ -82,6 +74,14 @@ def try_auto_create_directory(address: str) -> Optional[Alias]:
db.session.commit()
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]:
@ -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)
return None
# if alias has been deleted before, do not auto-create it
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
try:
LOG.d("create alias %s for domain %s", address, custom_domain)
alias = Alias.create(
email=address,
user_id=custom_domain.user_id,
@ -125,9 +115,16 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
automatic_creation=True,
mailbox_id=domain_user.default_mailbox_id,
)
db.session.commit()
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):