Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions app/utils/countries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""app.utils.countries.py"""
import logging
from abc import ABCMeta, abstractmethod

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -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