diff --git a/app/utils/countries.py b/app/utils/countries.py index 9fb4f98a..342cddbf 100644 --- a/app/utils/countries.py +++ b/app/utils/countries.py @@ -1,5 +1,6 @@ """app.utils.countries.py""" import logging +from abc import ABCMeta, abstractmethod LOGGER = logging.getLogger(__name__) @@ -366,15 +367,49 @@ # "MS Zaandam" } -# fmt: on -def country_code(value): - """ - Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1 - Defaults to "XX". - """ - code = COUNTRY_NAME__COUNTRY_CODE.get(value, DEFAULT_COUNTRY_CODE) - if code == DEFAULT_COUNTRY_CODE: - # log at sub DEBUG level - LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!") +class CountriesComponent(metaclass=ABCMeta): + """Interface""" + @abstractmethod + def country_code(self): pass + def country_name(self): pass - return code +class CountriesGroup(CountriesComponent): + """The Composite: A list of countries grouped in some manner""" + countriesList = [] + + def __init__(self, countriesList): + self.countriesList = countriesList + + def country_code(self): + codeList = [] + for country in self.countriesList: + codeList.append(country.country_code()) + return codeList + + def country_name(self): + nameList = [] + for country in self.countriesList: + nameList.append(country.country_code()) + return nameList + +class Country(CountriesComponent): + """The Leaf: A singular country""" + def __init__(self, name, code): + self.name = name + self.code = code + + def country_name(self): + return self.name + + # fmt: on + def country_code(value): + """ + Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1 + Defaults to "XX". + """ + code = COUNTRY_NAME__COUNTRY_CODE.get(value, DEFAULT_COUNTRY_CODE) + if code == DEFAULT_COUNTRY_CODE: + # log at sub DEBUG level + LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!") + + return code