Skip to content
Merged
Prev Previous commit
Next Next commit
adds country_in_database function to countrycodes
  • Loading branch information
Turreted committed Mar 25, 2020
commit 14101de21ac6502701205df7fb5b7c7befc4e931
16 changes: 14 additions & 2 deletions app/utils/countrycodes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import chain

# Default country code.
default_code = "XX"

Expand Down Expand Up @@ -359,7 +361,7 @@
# "Cruise Ship" has no mapping, i.e. the default val is used
}

def country_code(country):
def country_code(country, verbose=True):
"""
Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1
Defaults to "XX".
Expand All @@ -371,5 +373,15 @@ def country_code(country):
synonym = synonyms[country]
return is_3166_1[synonym]
else:
print ("No country_code found for '" + country + "'. Using '" + default_code + "'")
if verbose:
print ("No country_code found for '" + country + "'. Using '" + default_code + "'")
return default_code

def country_in_database(country):
"""
Checks if a given country is in the database.
"""
if country in chain(is_3166_1, synonyms):
return True
else:
return False