From 7c339b808e6a15359acd8600591d07c89c1e6c17 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 6 Feb 2018 09:29:11 +0000 Subject: [PATCH] Refactor logic around gov domains into a class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This gives us space to add more logic in the future, and expose more information than whether a given domain is/isn’t government. --- app/utils.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/app/utils.py b/app/utils.py index 38df2d4f4..1aaa19eb1 100644 --- a/app/utils.py +++ b/app/utils.py @@ -280,9 +280,11 @@ def get_help_argument(): def is_gov_user(email_address): - valid_domains = current_app.config['EMAIL_DOMAIN_REGEXES'] - email_regex = (r"[\.|@]({})$".format("|".join(valid_domains))) - return bool(re.search(email_regex, email_address.lower())) + try: + GovernmentDomain(email_address) + return True + except NotGovernmentDomain: + return False def get_template( @@ -428,3 +430,21 @@ def set_status_filters(filter_args): SENDING_STATUSES if 'sending' in status_filters else [], FAILURE_STATUSES if 'failed' in status_filters else [] ))) + + +class NotGovernmentDomain(Exception): + pass + + +class GovernmentDomain: + + def __init__(self, email_address_or_domain): + + for domain in current_app.config['EMAIL_DOMAIN_REGEXES']: + if re.search( + r"[\.|@]({})$".format(domain), + email_address_or_domain.lower() + ): + return + + raise NotGovernmentDomain()