From 54041078426a0c51ed7de6ee12fd2ac4e81ec7e8 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 6 Feb 2018 16:26:02 +0000 Subject: [PATCH] Split email list out again The list of email domains is a different list from the list of all government domains. And because the list of all government domains is really long now, it could be unnecessarily slow to search through when (a lot of the time) all we care about is whether the email address ends with `.gov.uk`. --- app/email_domains.yml | 30 ++++++++++++++++++++++++++++++ app/utils.py | 12 ++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 app/email_domains.yml diff --git a/app/email_domains.yml b/app/email_domains.yml new file mode 100644 index 000000000..bb90d459f --- /dev/null +++ b/app/email_domains.yml @@ -0,0 +1,30 @@ +--- +- gov.uk +- mod.uk +- mil.uk +- ddc-mod.org +- slc.co.uk +- gov.scot +- parliament.uk +- nhs.uk +- nhs.net +- police.uk +- dclgdatamart.co.uk +- ucds.email +- naturalengland.org.uk +- hmcts.net +- scotent.co.uk +- assembly.wales +- cjsm.net +- cqc.org.uk +- bl.uk +- stfc.ac.uk +- wmfs.net +- bbsrc.ac.uk +- acas.org.uk +- gov.wales +- biglotteryfund.org.uk +- marinemanagement.org.uk +- britishmuseum.org +- derrystrabane.com +- highwaysengland.co.uk diff --git a/app/utils.py b/app/utils.py index 9874e1ec7..e0bf116c7 100644 --- a/app/utils.py +++ b/app/utils.py @@ -284,7 +284,7 @@ def get_help_argument(): def is_gov_user(email_address): try: - GovernmentDomain(email_address) + GovernmentDomain(email_address, domain_list='email_domain_names') return True except NotGovernmentDomain: return False @@ -447,17 +447,21 @@ class GovernmentDomain: domains = yaml.safe_load(domains) domain_names = sorted(domains.keys(), key=len) - def __init__(self, email_address_or_domain): + with open('{}/email_domains.yml'.format(_dir_path)) as email_domains: + email_domain_names = yaml.safe_load(email_domains) + + def __init__(self, email_address_or_domain, domain_list='domain_names'): try: self._match = next(filter( self.get_matching_function(email_address_or_domain), - self.domain_names, + getattr(self, domain_list), )) except StopIteration: raise NotGovernmentDomain() - self.owner, self.sector, self.agreement_signed = self._get_details_of_domain() + if domain_list == 'domain_names': + self.owner, self.sector, self.agreement_signed = self._get_details_of_domain() @staticmethod def get_matching_function(email_address_or_domain):