From a4f8dc9c9d0988a1b47d420129371a8a632f7475 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 23 May 2020 19:45:26 +0200 Subject: [PATCH] Use AliasInTrashError instead of DeletedAlias.get_by check when trying to create alias automatically --- app/alias_utils.py | 55 ++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/app/alias_utils.py b/app/alias_utils.py index b3087da7..4c7dd500 100644 --- a/app/alias_utils.py +++ b/app/alias_utils.py @@ -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,8 +62,19 @@ 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): + try: + LOG.d("create alias %s for directory %s", address, directory) + + alias = Alias.create( + email=address, + user_id=directory.user_id, + directory_id=directory.id, + mailbox_id=dir_user.default_mailbox_id, + ) + + db.session.commit() + return alias + except AliasInTrashError: LOG.warning( "Alias %s was deleted before, cannot auto-create using directory %s, user %s", address, @@ -71,18 +83,6 @@ def try_auto_create_directory(address: str) -> Optional[Alias]: ) return None - LOG.d("create alias %s for directory %s", address, directory) - - alias = Alias.create( - email=address, - user_id=directory.user_id, - directory_id=directory.id, - mailbox_id=dir_user.default_mailbox_id, - ) - - db.session.commit() - return alias - def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]: """Try to create an alias with catch-all domain""" @@ -106,8 +106,18 @@ 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): + try: + LOG.d("create alias %s for domain %s", address, custom_domain) + alias = Alias.create( + email=address, + user_id=custom_domain.user_id, + custom_domain_id=custom_domain.id, + 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, @@ -116,19 +126,6 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]: ) return None - LOG.d("create alias %s for domain %s", address, custom_domain) - - alias = Alias.create( - email=address, - user_id=custom_domain.user_id, - custom_domain_id=custom_domain.id, - automatic_creation=True, - mailbox_id=domain_user.default_mailbox_id, - ) - - db.session.commit() - return alias - def delete_alias(alias: Alias, user: User): Alias.delete(alias.id)