Skip to content

Commit fb0dd93

Browse files
refactor: use pycountry for country list (#12009)
* refactor: use pycountry for country list Eliminates one use of pytz * chore: renumber migration * fix: accent-aware sorting
1 parent 59ddd3b commit fb0dd93

3 files changed

Lines changed: 288 additions & 2 deletions

File tree

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
# Copyright The IETF Trust 2026, All Rights Reserved
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("meeting", "0021_correct_ietf_meeting_dates"),
9+
]
10+
11+
operations = [
12+
migrations.AlterField(
13+
model_name="meeting",
14+
name="country",
15+
field=models.CharField(
16+
blank=True,
17+
choices=[
18+
("", "---------"),
19+
("AF", "Afghanistan"),
20+
("AX", "Åland Islands"),
21+
("AL", "Albania"),
22+
("DZ", "Algeria"),
23+
("AS", "American Samoa"),
24+
("AD", "Andorra"),
25+
("AO", "Angola"),
26+
("AI", "Anguilla"),
27+
("AQ", "Antarctica"),
28+
("AG", "Antigua and Barbuda"),
29+
("AR", "Argentina"),
30+
("AM", "Armenia"),
31+
("AW", "Aruba"),
32+
("AU", "Australia"),
33+
("AT", "Austria"),
34+
("AZ", "Azerbaijan"),
35+
("BS", "Bahamas"),
36+
("BH", "Bahrain"),
37+
("BD", "Bangladesh"),
38+
("BB", "Barbados"),
39+
("BY", "Belarus"),
40+
("BE", "Belgium"),
41+
("BZ", "Belize"),
42+
("BJ", "Benin"),
43+
("BM", "Bermuda"),
44+
("BT", "Bhutan"),
45+
("BO", "Bolivia"),
46+
("BQ", "Bonaire, Sint Eustatius and Saba"),
47+
("BA", "Bosnia and Herzegovina"),
48+
("BW", "Botswana"),
49+
("BV", "Bouvet Island"),
50+
("BR", "Brazil"),
51+
("IO", "British Indian Ocean Territory"),
52+
("BN", "Brunei Darussalam"),
53+
("BG", "Bulgaria"),
54+
("BF", "Burkina Faso"),
55+
("BI", "Burundi"),
56+
("CV", "Cabo Verde"),
57+
("KH", "Cambodia"),
58+
("CM", "Cameroon"),
59+
("CA", "Canada"),
60+
("KY", "Cayman Islands"),
61+
("CF", "Central African Republic"),
62+
("TD", "Chad"),
63+
("CL", "Chile"),
64+
("CN", "China"),
65+
("CX", "Christmas Island"),
66+
("CC", "Cocos (Keeling) Islands"),
67+
("CO", "Colombia"),
68+
("KM", "Comoros"),
69+
("CG", "Congo"),
70+
("CD", "Congo, The Democratic Republic of the"),
71+
("CK", "Cook Islands"),
72+
("CR", "Costa Rica"),
73+
("CI", "Côte d'Ivoire"),
74+
("HR", "Croatia"),
75+
("CU", "Cuba"),
76+
("CW", "Curaçao"),
77+
("CY", "Cyprus"),
78+
("CZ", "Czechia"),
79+
("DK", "Denmark"),
80+
("DJ", "Djibouti"),
81+
("DM", "Dominica"),
82+
("DO", "Dominican Republic"),
83+
("EC", "Ecuador"),
84+
("EG", "Egypt"),
85+
("SV", "El Salvador"),
86+
("GQ", "Equatorial Guinea"),
87+
("ER", "Eritrea"),
88+
("EE", "Estonia"),
89+
("SZ", "Eswatini"),
90+
("ET", "Ethiopia"),
91+
("FK", "Falkland Islands (Malvinas)"),
92+
("FO", "Faroe Islands"),
93+
("FJ", "Fiji"),
94+
("FI", "Finland"),
95+
("FR", "France"),
96+
("GF", "French Guiana"),
97+
("PF", "French Polynesia"),
98+
("TF", "French Southern Territories"),
99+
("GA", "Gabon"),
100+
("GM", "Gambia"),
101+
("GE", "Georgia"),
102+
("DE", "Germany"),
103+
("GH", "Ghana"),
104+
("GI", "Gibraltar"),
105+
("GR", "Greece"),
106+
("GL", "Greenland"),
107+
("GD", "Grenada"),
108+
("GP", "Guadeloupe"),
109+
("GU", "Guam"),
110+
("GT", "Guatemala"),
111+
("GG", "Guernsey"),
112+
("GN", "Guinea"),
113+
("GW", "Guinea-Bissau"),
114+
("GY", "Guyana"),
115+
("HT", "Haiti"),
116+
("HM", "Heard Island and McDonald Islands"),
117+
("VA", "Holy See (Vatican City State)"),
118+
("HN", "Honduras"),
119+
("HK", "Hong Kong"),
120+
("HU", "Hungary"),
121+
("IS", "Iceland"),
122+
("IN", "India"),
123+
("ID", "Indonesia"),
124+
("IR", "Iran"),
125+
("IQ", "Iraq"),
126+
("IE", "Ireland"),
127+
("IM", "Isle of Man"),
128+
("IL", "Israel"),
129+
("IT", "Italy"),
130+
("JM", "Jamaica"),
131+
("JP", "Japan"),
132+
("JE", "Jersey"),
133+
("JO", "Jordan"),
134+
("KZ", "Kazakhstan"),
135+
("KE", "Kenya"),
136+
("KI", "Kiribati"),
137+
("KW", "Kuwait"),
138+
("KG", "Kyrgyzstan"),
139+
("LA", "Laos"),
140+
("LV", "Latvia"),
141+
("LB", "Lebanon"),
142+
("LS", "Lesotho"),
143+
("LR", "Liberia"),
144+
("LY", "Libya"),
145+
("LI", "Liechtenstein"),
146+
("LT", "Lithuania"),
147+
("LU", "Luxembourg"),
148+
("MO", "Macao"),
149+
("MG", "Madagascar"),
150+
("MW", "Malawi"),
151+
("MY", "Malaysia"),
152+
("MV", "Maldives"),
153+
("ML", "Mali"),
154+
("MT", "Malta"),
155+
("MH", "Marshall Islands"),
156+
("MQ", "Martinique"),
157+
("MR", "Mauritania"),
158+
("MU", "Mauritius"),
159+
("YT", "Mayotte"),
160+
("MX", "Mexico"),
161+
("FM", "Micronesia, Federated States of"),
162+
("MD", "Moldova"),
163+
("MC", "Monaco"),
164+
("MN", "Mongolia"),
165+
("ME", "Montenegro"),
166+
("MS", "Montserrat"),
167+
("MA", "Morocco"),
168+
("MZ", "Mozambique"),
169+
("MM", "Myanmar"),
170+
("NA", "Namibia"),
171+
("NR", "Nauru"),
172+
("NP", "Nepal"),
173+
("NL", "Netherlands"),
174+
("NC", "New Caledonia"),
175+
("NZ", "New Zealand"),
176+
("NI", "Nicaragua"),
177+
("NE", "Niger"),
178+
("NG", "Nigeria"),
179+
("NU", "Niue"),
180+
("NF", "Norfolk Island"),
181+
("MP", "Northern Mariana Islands"),
182+
("KP", "North Korea"),
183+
("MK", "North Macedonia"),
184+
("NO", "Norway"),
185+
("OM", "Oman"),
186+
("PK", "Pakistan"),
187+
("PW", "Palau"),
188+
("PS", "Palestine, State of"),
189+
("PA", "Panama"),
190+
("PG", "Papua New Guinea"),
191+
("PY", "Paraguay"),
192+
("PE", "Peru"),
193+
("PH", "Philippines"),
194+
("PN", "Pitcairn"),
195+
("PL", "Poland"),
196+
("PT", "Portugal"),
197+
("PR", "Puerto Rico"),
198+
("QA", "Qatar"),
199+
("RE", "Réunion"),
200+
("RO", "Romania"),
201+
("RU", "Russian Federation"),
202+
("RW", "Rwanda"),
203+
("BL", "Saint Barthélemy"),
204+
("SH", "Saint Helena, Ascension and Tristan da Cunha"),
205+
("KN", "Saint Kitts and Nevis"),
206+
("LC", "Saint Lucia"),
207+
("MF", "Saint Martin (French part)"),
208+
("PM", "Saint Pierre and Miquelon"),
209+
("VC", "Saint Vincent and the Grenadines"),
210+
("WS", "Samoa"),
211+
("SM", "San Marino"),
212+
("ST", "Sao Tome and Principe"),
213+
("SA", "Saudi Arabia"),
214+
("SN", "Senegal"),
215+
("RS", "Serbia"),
216+
("SC", "Seychelles"),
217+
("SL", "Sierra Leone"),
218+
("SG", "Singapore"),
219+
("SX", "Sint Maarten (Dutch part)"),
220+
("SK", "Slovakia"),
221+
("SI", "Slovenia"),
222+
("SB", "Solomon Islands"),
223+
("SO", "Somalia"),
224+
("ZA", "South Africa"),
225+
("GS", "South Georgia and the South Sandwich Islands"),
226+
("KR", "South Korea"),
227+
("SS", "South Sudan"),
228+
("ES", "Spain"),
229+
("LK", "Sri Lanka"),
230+
("SD", "Sudan"),
231+
("SR", "Suriname"),
232+
("SJ", "Svalbard and Jan Mayen"),
233+
("SE", "Sweden"),
234+
("CH", "Switzerland"),
235+
("SY", "Syria"),
236+
("TW", "Taiwan"),
237+
("TJ", "Tajikistan"),
238+
("TZ", "Tanzania"),
239+
("TH", "Thailand"),
240+
("TL", "Timor-Leste"),
241+
("TG", "Togo"),
242+
("TK", "Tokelau"),
243+
("TO", "Tonga"),
244+
("TT", "Trinidad and Tobago"),
245+
("TN", "Tunisia"),
246+
("TR", "Türkiye"),
247+
("TM", "Turkmenistan"),
248+
("TC", "Turks and Caicos Islands"),
249+
("TV", "Tuvalu"),
250+
("UG", "Uganda"),
251+
("UA", "Ukraine"),
252+
("AE", "United Arab Emirates"),
253+
("GB", "United Kingdom"),
254+
("US", "United States"),
255+
("UM", "United States Minor Outlying Islands"),
256+
("UY", "Uruguay"),
257+
("UZ", "Uzbekistan"),
258+
("VU", "Vanuatu"),
259+
("VE", "Venezuela"),
260+
("VN", "Vietnam"),
261+
("VG", "Virgin Islands, British"),
262+
("VI", "Virgin Islands, U.S."),
263+
("WF", "Wallis and Futuna"),
264+
("EH", "Western Sahara"),
265+
("YE", "Yemen"),
266+
("ZM", "Zambia"),
267+
("ZW", "Zimbabwe"),
268+
],
269+
max_length=2,
270+
),
271+
),
272+
]

‎ietf/meeting/models.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import os
99
from functools import cached_property
1010

11+
import locale
12+
import pycountry
1113
import pytz
1214
import random
1315
import re
@@ -51,11 +53,22 @@
5153
)
5254
from ietf.utils.fields import MissingOkImageField
5355

54-
# Set up countries / timezones, including an empty choice for fields
56+
# Set up countries / timezones, including an empty choice for fields. After moving to
57+
# Django 5.x, the choices can become a runtime callable.
58+
_stashed_locale = locale.setlocale(locale.LC_ALL) # save locale settings
59+
locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8") # UTF-8-aware collation
5560
EMPTY_CHOICE = ("", "-" * 9)
5661
COUNTRIES = (EMPTY_CHOICE,) + tuple(
57-
sorted(pytz.country_names.items(), key=lambda x: x[1])
62+
sorted(
63+
[
64+
# use common_name if available, falling back to name if not
65+
(country.alpha_2, getattr(country, "common_name", country.name))
66+
for country in pycountry.countries
67+
],
68+
key=lambda x: locale.strxfrm(x[1]), # locale-aware sort
69+
)
5870
)
71+
locale.setlocale(locale.LC_ALL, _stashed_locale) # restore original settings
5972

6073
_tzdata_ics_path = Path(settings.TZDATA_ICS_PATH)
6174
TIMEZONES = (EMPTY_CHOICE,) + tuple(

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ opentelemetry-exporter-otlp-proto-http>=1.38.0
6565
pillow>=11.3.0
6666
psycopg2>=2.9.10
6767
pyang>=2.6.1
68+
pycountry>=26.2
6869
pydyf>=0.12.1
6970
pyflakes>=3.4.0
7071
pyopenssl>=25.1.0 # Used by urllib3.contrib, which is used by PyQuery but not marked as a dependency

0 commit comments

Comments
 (0)