mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-20 16:13:24 -04:00
Localize notification_utils to the admin
This changeset pulls in all of the notification_utils code directly into the admin and removes it as an external dependency. We are doing this to cut down on operational maintenance of the project and will begin removing parts of it no longer needed for the admin. Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
This commit is contained in:
81
notifications_utils/countries/__init__.py
Normal file
81
notifications_utils/countries/__init__.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from functools import lru_cache
|
||||
|
||||
from notifications_utils.insensitive_dict import InsensitiveDict
|
||||
from notifications_utils.sanitise_text import SanitiseASCII
|
||||
|
||||
from .data import (
|
||||
ADDITIONAL_SYNONYMS,
|
||||
COUNTRIES_AND_TERRITORIES,
|
||||
EUROPEAN_ISLANDS,
|
||||
ROYAL_MAIL_EUROPEAN,
|
||||
UK,
|
||||
UK_ISLANDS,
|
||||
WELSH_NAMES,
|
||||
Postage,
|
||||
)
|
||||
|
||||
|
||||
class CountryMapping(InsensitiveDict):
|
||||
@staticmethod
|
||||
@lru_cache(maxsize=2048, typed=False)
|
||||
def make_key(original_key):
|
||||
original_key = original_key.replace("&", "and")
|
||||
original_key = original_key.replace("+", "and")
|
||||
|
||||
normalised = "".join(
|
||||
character.lower()
|
||||
for character in original_key
|
||||
if character not in " _-'’,.()"
|
||||
)
|
||||
|
||||
if "?" in SanitiseASCII.encode(normalised):
|
||||
return normalised
|
||||
|
||||
return SanitiseASCII.encode(normalised)
|
||||
|
||||
def __contains__(self, key):
|
||||
if any(c.isdigit() for c in key):
|
||||
# A string with a digit can’t be a country and is probably a
|
||||
# postcode, so let’s do a little optimisation, skip the
|
||||
# expensive string manipulation to normalise the key and say
|
||||
# that there’s no matching country
|
||||
return False
|
||||
return super().__contains__(key)
|
||||
|
||||
def __getitem__(self, key):
|
||||
for key_ in (key, f"the {key}", f"yr {key}", f"y {key}"):
|
||||
if key_ in self:
|
||||
return super().__getitem__(key_)
|
||||
|
||||
raise CountryNotFoundError(f"Not a known country or territory ({key})")
|
||||
|
||||
|
||||
countries = CountryMapping(
|
||||
dict(
|
||||
COUNTRIES_AND_TERRITORIES
|
||||
+ UK_ISLANDS
|
||||
+ EUROPEAN_ISLANDS
|
||||
+ WELSH_NAMES
|
||||
+ ADDITIONAL_SYNONYMS
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class Country:
|
||||
def __init__(self, given_name):
|
||||
self.canonical_name = countries[given_name]
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.canonical_name == other.canonical_name
|
||||
|
||||
@property
|
||||
def postage_zone(self):
|
||||
if self.canonical_name == UK:
|
||||
return Postage.UK
|
||||
if self.canonical_name in ROYAL_MAIL_EUROPEAN:
|
||||
return Postage.EUROPE
|
||||
return Postage.REST_OF_WORLD
|
||||
|
||||
|
||||
class CountryNotFoundError(KeyError):
|
||||
pass
|
||||
6
notifications_utils/countries/_data/ended-countries.json
Normal file
6
notifications_utils/countries/_data/ended-countries.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"Yugoslavia": null,
|
||||
"USSR": null,
|
||||
"East Germany": "Germany",
|
||||
"Czechoslovakia": "Czechia"
|
||||
}
|
||||
62
notifications_utils/countries/_data/europe.txt
Normal file
62
notifications_utils/countries/_data/europe.txt
Normal file
@@ -0,0 +1,62 @@
|
||||
Albania
|
||||
Andorra
|
||||
Armenia
|
||||
Austria
|
||||
Azerbaijan
|
||||
Azores
|
||||
Balearic Islands
|
||||
Belarus
|
||||
Belgium
|
||||
Bosnia and Herzegovina
|
||||
Bulgaria
|
||||
Canary Islands
|
||||
Corsica
|
||||
Croatia
|
||||
Cyprus
|
||||
Czechia
|
||||
Denmark
|
||||
Estonia
|
||||
Faroe Islands
|
||||
Finland
|
||||
France
|
||||
Georgia
|
||||
Germany
|
||||
Gibraltar
|
||||
Greece
|
||||
Greenland
|
||||
Hungary
|
||||
Iceland
|
||||
Ireland
|
||||
Italy
|
||||
Kazakhstan
|
||||
Kosovo
|
||||
Kyrgyzstan
|
||||
Latvia
|
||||
Liechtenstein
|
||||
Lithuania
|
||||
Luxembourg
|
||||
North Macedonia
|
||||
Madeira
|
||||
Malta
|
||||
Moldova
|
||||
Monaco
|
||||
Montenegro
|
||||
Netherlands
|
||||
Norway
|
||||
Poland
|
||||
Portugal
|
||||
Romania
|
||||
Russia
|
||||
San Marino
|
||||
Serbia
|
||||
Slovakia
|
||||
Slovenia
|
||||
Spain
|
||||
Sweden
|
||||
Switzerland
|
||||
Tajikistan
|
||||
Turkey
|
||||
Turkmenistan
|
||||
Ukraine
|
||||
Uzbekistan
|
||||
Vatican City
|
||||
5
notifications_utils/countries/_data/european-islands.txt
Normal file
5
notifications_utils/countries/_data/european-islands.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
Azores
|
||||
Balearic Islands
|
||||
Canary Islands
|
||||
Corsica
|
||||
Madeira
|
||||
28607
notifications_utils/countries/_data/location-autocomplete-graph.json
Normal file
28607
notifications_utils/countries/_data/location-autocomplete-graph.json
Normal file
File diff suppressed because it is too large
Load Diff
58
notifications_utils/countries/_data/synonyms.json
Normal file
58
notifications_utils/countries/_data/synonyms.json
Normal file
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"England": "United Kingdom",
|
||||
"Northern Ireland": "United Kingdom",
|
||||
"Scotland": "United Kingdom",
|
||||
"Wales": "United Kingdom",
|
||||
"ROI": "Ireland",
|
||||
"Irish Republic": "Ireland",
|
||||
"Rep of Ireland": "Ireland",
|
||||
"South Ireland": "Ireland",
|
||||
"Southern Ireland": "Ireland",
|
||||
"N Ireland": "United Kingdom",
|
||||
"North Ireland": "United Kingdom",
|
||||
"GBR": "United Kingdom",
|
||||
"United States America": "United States",
|
||||
"America": "United States",
|
||||
"Macedonia": "North Macedonia",
|
||||
"Autonomous Region of the Azores": "Azores",
|
||||
"Islas Canarias": "Canary Islands",
|
||||
"Canaries": "Canary Islands",
|
||||
"Autonomous Region of Madeira": "Madeira",
|
||||
"Região Autónoma da Madeira": "Madeira",
|
||||
"Islas Baleares": "Balearic Islands",
|
||||
"Illes Balears": "Balearic Islands",
|
||||
"Corse": "Corsica",
|
||||
"Burma": "Myanmar (Burma)",
|
||||
"Czechoslovakia": "Czechia",
|
||||
"East Germany": "Germany",
|
||||
"Easter Island": "Easter Island",
|
||||
"Falkland": "Falkland Islands",
|
||||
"The Falklands": "Falkland Islands",
|
||||
"The Falkland Islands": "Falkland Islands",
|
||||
"Hawaii": "United States",
|
||||
"Khazakhstan": "Kazakhstan",
|
||||
"Korea": "South Korea",
|
||||
"Macau": "Macao",
|
||||
"Myanmar": "Myanmar (Burma)",
|
||||
"New Zeeland": "New Zealand",
|
||||
"NI": "United Kingdom",
|
||||
"Pitcairn Island": "Pitcairn, Henderson, Ducie and Oeno Islands",
|
||||
"Henderson Island": "Pitcairn, Henderson, Ducie and Oeno Islands",
|
||||
"Ducie Island": "Pitcairn, Henderson, Ducie and Oeno Islands",
|
||||
"Oeno Island": "Pitcairn, Henderson, Ducie and Oeno Islands",
|
||||
"Republic of China": "Taiwan",
|
||||
"Republik Österreich": "Austria",
|
||||
"République Islamique de Mauritanie": "Mauritania",
|
||||
"Saint Helena": "Saint Helena",
|
||||
"St Helena": "Saint Helena",
|
||||
"Swaziland": "Eswatini",
|
||||
"the south sandwich islands": "South Georgia and the South Sandwich Islands",
|
||||
"the sandwich islands": "South Georgia and the South Sandwich Islands",
|
||||
"South Georgia": "South Georgia and the South Sandwich Islands",
|
||||
"Tristan": "Tristan da Cunha",
|
||||
"Vatican": "Vatican City",
|
||||
"West Germany": "Germany",
|
||||
"Saint Kitts and Nevis": "St Kitts and Nevis",
|
||||
"Saint Kitts": "St Kitts and Nevis",
|
||||
"St Kitts": "St Kitts and Nevis"
|
||||
}
|
||||
8
notifications_utils/countries/_data/uk-islands.txt
Normal file
8
notifications_utils/countries/_data/uk-islands.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
Alderney
|
||||
Brecqhou
|
||||
Guernsey
|
||||
Herm
|
||||
Isle of Man
|
||||
Jersey
|
||||
Jethou
|
||||
Sark
|
||||
103
notifications_utils/countries/_data/welsh-names.json
Normal file
103
notifications_utils/countries/_data/welsh-names.json
Normal file
@@ -0,0 +1,103 @@
|
||||
{
|
||||
"Affganistan": "Afghanistan",
|
||||
"Antigwa a Barbiwda": "Antigua and Barbuda",
|
||||
"Yr Ariannin": "Argentina",
|
||||
"Awstralia": "Australia",
|
||||
"Awstria": "Austria",
|
||||
"Aserbaijan": "Azerbaijan",
|
||||
"Y Bahamas": "The Bahamas",
|
||||
"Belarws": "Belarus",
|
||||
"Gwlad Belg": "Belgium",
|
||||
"Bhwtan": "Bhutan",
|
||||
"Bolifia": "Bolivia",
|
||||
"Bosnia a Hercegovina": "Bosnia and Herzegovina",
|
||||
"Brasil": "Brazil",
|
||||
"Bwlgaria": "Bulgaria",
|
||||
"Bwrwndi": "Burundi",
|
||||
"Camerŵn": "Cameroon",
|
||||
"Cabo Verde": "Cape Verde",
|
||||
"Gweriniaeth Canolbarth Affrica": "Central African Republic",
|
||||
"Tchad": "Chad",
|
||||
"Tsieina": "China",
|
||||
"Y Comoros": "Comoros",
|
||||
"Ciwba": "Cuba",
|
||||
"Y Weriniaeth Tsiec": "Czechia",
|
||||
"Gweriniaeth Ddemocrataidd Congo": "Congo (Democratic Republic)",
|
||||
"Denmarc": "Denmark",
|
||||
"Gweriniaeth Dominica": "Dominican Republic",
|
||||
"Dwyrain Timor": "East Timor",
|
||||
"Ecwador": "Ecuador",
|
||||
"Yr Aifft": "Egypt",
|
||||
"Gini Gyhydeddol": "Equatorial Guinea",
|
||||
"Ffiji": "Fiji",
|
||||
"Y Ffindir": "Finland",
|
||||
"Ffrainc": "France",
|
||||
"Y Gambia": "The Gambia",
|
||||
"Yr Alban": "United Kingdom",
|
||||
"Yr Almaen": "Germany",
|
||||
"Gwlad Groeg": "Greece",
|
||||
"Gini": "Guinea",
|
||||
"Guiné-Bissau": "Guinea-Bissau",
|
||||
"Gaiana": "Guyana",
|
||||
"Hondwras": "Honduras",
|
||||
"Hwngari": "Hungary",
|
||||
"Gwlad yr Iâ": "Iceland",
|
||||
"Irac": "Iraq",
|
||||
"Iwerddon": "Ireland",
|
||||
"Yr Eidal": "Italy",
|
||||
"Iorddonen": "Jordan",
|
||||
"Kazakstan": "Kazakhstan",
|
||||
"Latfia": "Latvia",
|
||||
"Libanus": "Lebanon",
|
||||
"Libia": "Libya",
|
||||
"Lithwania": "Lithuania",
|
||||
"Lwcsembwrg": "Luxembourg",
|
||||
"Madagasgar": "Madagascar",
|
||||
"Ynysoedd Marshall": "Marshall Islands",
|
||||
"Mecsico": "Mexico",
|
||||
"Moldofa": "Moldova",
|
||||
"Moroco": "Morocco",
|
||||
"Mosambic": "Mozambique",
|
||||
"Yr Iseldiroedd": "Netherlands",
|
||||
"Seland Newydd": "New Zealand",
|
||||
"Nicaragwa": "Nicaragua",
|
||||
"Gogledd Corea": "North Korea",
|
||||
"Norwy": "Norway",
|
||||
"Papua Guinea Newydd": "Papua New Guinea",
|
||||
"Paragwâi": "Paraguay",
|
||||
"Periw": "Peru",
|
||||
"Pilipinas": "Philippines",
|
||||
"Gwlad Pwyl": "Poland",
|
||||
"Portiwgal": "Portugal",
|
||||
"Gweriniaeth y Congo": "Congo",
|
||||
"Gweriniaeth Macedonia": "North Macedonia",
|
||||
"Gogledd Macedonia": "North Macedonia",
|
||||
"Rwmania": "Romania",
|
||||
"Rwsia": "Russia",
|
||||
"Saint Kitts a Nevis": "St Kitts and Nevis",
|
||||
"St Kitts a Nevis": "St Kitts and Nevis",
|
||||
"Saint Vincent a’r Grenadines": "St Vincent",
|
||||
"São Tomé a Príncipe": "Sao Tome and Principe",
|
||||
"Sénégal": "Senegal",
|
||||
"Slofacia": "Slovakia",
|
||||
"Slofenia": "Slovenia",
|
||||
"Ynysoedd Solomon": "Solomon Islands",
|
||||
"De Affrica": "South Africa",
|
||||
"De Corea": "South Korea",
|
||||
"De Sudan": "South Sudan",
|
||||
"Sbaen": "Spain",
|
||||
"Swrinam": "Suriname",
|
||||
"Gwlad Swazi": "Eswatini",
|
||||
"Y Swistir": "Switzerland",
|
||||
"Gwlad Thai": "Thailand",
|
||||
"Trinidad a Thobago": "Trinidad and Tobago",
|
||||
"Twrci": "Turkey",
|
||||
"Twfalw": "Tuvalu",
|
||||
"Wcráin": "Ukraine",
|
||||
"Yr Emiradau Arabaidd Unedig": "United Arab Emirates",
|
||||
"Y Deyrnas Unedig": "United Kingdom",
|
||||
"Unol Daleithiau America": "United States",
|
||||
"Wrwgwái": "Uruguay",
|
||||
"Feneswela": "Venezuela",
|
||||
"Fietnam": "Vietnam"
|
||||
}
|
||||
67
notifications_utils/countries/data.py
Normal file
67
notifications_utils/countries/data.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
def _load_data(filename):
|
||||
with open(os.path.join(os.path.dirname(__file__), "_data", filename)) as contents:
|
||||
if filename.endswith(".json"):
|
||||
return json.load(contents)
|
||||
return [line.strip() for line in contents.readlines()]
|
||||
|
||||
|
||||
def find_canonical(item, graph, key):
|
||||
if item["meta"]["canonical"]:
|
||||
return key, item["names"]["en-GB"]
|
||||
return find_canonical(
|
||||
graph[item["edges"]["from"][0]],
|
||||
graph,
|
||||
key,
|
||||
)
|
||||
|
||||
|
||||
# Copied from
|
||||
# https://github.com/alphagov/govuk-country-and-territory-autocomplete
|
||||
# /blob/b61091a502983fd2a77b3cdb5f94a604412eb093
|
||||
# /dist/location-autocomplete-graph.json
|
||||
_graph = _load_data("location-autocomplete-graph.json")
|
||||
|
||||
UK = "United Kingdom"
|
||||
|
||||
ENDED_COUNTRIES = _load_data("ended-countries.json")
|
||||
ADDITIONAL_SYNONYMS = list(_load_data("synonyms.json").items())
|
||||
WELSH_NAMES = list(_load_data("welsh-names.json").items())
|
||||
_UK_ISLANDS_LIST = _load_data("uk-islands.txt")
|
||||
_EUROPEAN_ISLANDS_LIST = _load_data("european-islands.txt")
|
||||
|
||||
CURRENT_AND_ENDED_COUNTRIES_AND_TERRITORIES = [
|
||||
find_canonical(item, _graph, item["names"]["en-GB"]) for item in _graph.values()
|
||||
]
|
||||
|
||||
COUNTRIES_AND_TERRITORIES = []
|
||||
|
||||
for synonym, canonical in CURRENT_AND_ENDED_COUNTRIES_AND_TERRITORIES:
|
||||
if canonical in _UK_ISLANDS_LIST:
|
||||
COUNTRIES_AND_TERRITORIES.append((synonym, UK))
|
||||
elif canonical in ENDED_COUNTRIES:
|
||||
succeeding_country = ENDED_COUNTRIES[canonical]
|
||||
if succeeding_country:
|
||||
COUNTRIES_AND_TERRITORIES.append((synonym, succeeding_country))
|
||||
COUNTRIES_AND_TERRITORIES.append((canonical, succeeding_country))
|
||||
else:
|
||||
COUNTRIES_AND_TERRITORIES.append((synonym, canonical))
|
||||
|
||||
UK_ISLANDS = [(synonym, UK) for synonym in _UK_ISLANDS_LIST]
|
||||
|
||||
EUROPEAN_ISLANDS = [(synonym, synonym) for synonym in _EUROPEAN_ISLANDS_LIST]
|
||||
|
||||
# Copied from https://www.royalmail.com/international-zones#europe
|
||||
# Modified to use the canonical names for countries where incorrect
|
||||
ROYAL_MAIL_EUROPEAN = _load_data("europe.txt")
|
||||
|
||||
|
||||
class Postage:
|
||||
UK = "united-kingdom"
|
||||
FIRST = "first"
|
||||
SECOND = "second"
|
||||
EUROPE = "europe"
|
||||
REST_OF_WORLD = "rest-of-world"
|
||||
Reference in New Issue
Block a user