Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/bones/selectcountry.py: 26%
43 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-01 22:44 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-01 22:44 +0000
1"""
2A bone representing a country selection input field in a web application or form.
4The SelectCountryBone is designed to provide a user-friendly way to select a country from a predefined list of
5countries. It inherits from the BaseBone class and extends it to support country-specific functionalities,
6such as displaying country names and handling country codes (e.g., ISO 3166-1 alpha-2 or alpha-3).
7"""
8from viur.core.bones.select import SelectBone
11class SelectCountryBone(SelectBone):
12 """
13 A bone representing a country selection input field in a web application or form.
15 The SelectCountryBone is designed to provide a user-friendly way to select a country from a predefined list of
16 countries. It inherits from the BaseBone class and extends it to support country-specific functionalities,
17 such as displaying country names and handling country codes (e.g., ISO 3166-1 alpha-2 or alpha-3).
19 :param codes: The version of the ISO-Codes, either 2 or 3
20 :param values: Optional, either a str representing a predefined group (currently "dach" and "eu"), a list of
21 ISO-Codes (must match the Code-Length from `codes`), or a dict with matching iso-codes as keys and
22 the full country-names as values.
24 .. seealso::
25 https://www.iso.org/obp/ui/#search/code/
26 """
28 ISO3CODES = {
29 "abw": "Aruba",
30 "afg": "Afghanistan",
31 "ago": "Angola",
32 "aia": "Anguilla",
33 "ala": "Åland Islands",
34 "alb": "Albania",
35 "and": "Andorra",
36 "are": "United Arab Emirates",
37 "arg": "Argentina",
38 "arm": "Armenia",
39 "asm": "American Samoa",
40 "ata": "Antarctica",
41 "atf": "French Southern Territories",
42 "atg": "Antigua and Barbuda",
43 "aus": "Australia",
44 "aut": "Austria",
45 "aze": "Azerbaijan",
46 "bdi": "Burundi",
47 "bel": "Belgium",
48 "ben": "Benin",
49 "bes": "Bonaire, Sint Eustatius and Saba",
50 "bfa": "Burkina Faso",
51 "bgd": "Bangladesh",
52 "bgr": "Bulgaria",
53 "bhr": "Bahrain",
54 "bhs": "Bahamas",
55 "bih": "Bosnia and Herzegovina",
56 "blm": "Saint Barthélemy",
57 "blr": "Belarus",
58 "blz": "Belize",
59 "bmu": "Bermuda",
60 "bol": "Bolivia",
61 "bra": "Brazil",
62 "brb": "Barbados",
63 "brn": "Brunei",
64 "btn": "Bhutan",
65 "bvt": "Bouvet Island",
66 "bwa": "Botswana",
67 "caf": "Central African Republic",
68 "can": "Canada",
69 "cck": "Cocos (Keeling) Islands",
70 "che": "Switzerland",
71 "chl": "Chile",
72 "chn": "China",
73 "civ": "Côte d'Ivoire",
74 "cmr": "Cameroon",
75 "cod": "Congo Democratic Republic",
76 "cog": "Congo Republic",
77 "cok": "Cook Islands",
78 "col": "Colombia",
79 "com": "Comoros",
80 "cpv": "Cabo Verde",
81 "cri": "Costa Rica",
82 "cub": "Cuba",
83 "cuw": "Curaçao",
84 "cxr": "Christmas Island",
85 "cym": "Cayman Islands",
86 "cyp": "Cyprus",
87 "cze": "Czechia",
88 "deu": "Germany",
89 "dji": "Djibouti",
90 "dma": "Dominica",
91 "dnk": "Denmark",
92 "dom": "Dominican Republic",
93 "dza": "Algeria",
94 "ecu": "Ecuador",
95 "egy": "Egypt",
96 "eri": "Eritrea",
97 "esh": "Western Sahara",
98 "esp": "Spain",
99 "est": "Estonia",
100 "eth": "Ethiopia",
101 "fin": "Finland",
102 "fji": "Fiji",
103 "flk": "Falkland Islands",
104 "fra": "France",
105 "fro": "Faroe Islands",
106 "fsm": "Micronesia",
107 "gab": "Gabon",
108 "gbr": "United Kingdom",
109 "geo": "Georgia",
110 "ggy": "Guernsey",
111 "gha": "Ghana",
112 "gib": "Gibraltar",
113 "gin": "Guinea",
114 "glp": "Guadeloupe",
115 "gmb": "Gambia",
116 "gnb": "Guinea-Bissau",
117 "gnq": "Equatorial Guinea",
118 "grc": "Greece",
119 "grd": "Grenada",
120 "grl": "Greenland",
121 "gtm": "Guatemala",
122 "guf": "French Guiana",
123 "gum": "Guam",
124 "guy": "Guyana",
125 "hkg": "Hong Kong",
126 "hmd": "Heard Island and McDonald Islands",
127 "hnd": "Honduras",
128 "hrv": "Croatia",
129 "hti": "Haiti",
130 "hun": "Hungary",
131 "idn": "Indonesia",
132 "imn": "Isle of Man",
133 "ind": "India",
134 "iot": "British Indian Ocean Territory",
135 "irl": "Ireland",
136 "irn": "Iran",
137 "irq": "Iraq",
138 "isl": "Iceland",
139 "isr": "Israel",
140 "ita": "Italy",
141 "jam": "Jamaica",
142 "jey": "Jersey",
143 "jor": "Jordan",
144 "jpn": "Japan",
145 "kaz": "Kazakhstan",
146 "ken": "Kenya",
147 "kgz": "Kyrgyzstan",
148 "khm": "Cambodia",
149 "kir": "Kiribati",
150 "kna": "Saint Kitts and Nevis",
151 "kor": "Korea South",
152 "kwt": "Kuwait",
153 "lao": "Laos",
154 "lbn": "Lebanon",
155 "lbr": "Liberia",
156 "lby": "Libya",
157 "lca": "Saint Lucia",
158 "lie": "Liechtenstein",
159 "lka": "Sri Lanka",
160 "lso": "Lesotho",
161 "ltu": "Lithuania",
162 "lux": "Luxembourg",
163 "lva": "Latvia",
164 "mac": "Macao",
165 "maf": "Saint Martin",
166 "mar": "Morocco",
167 "mco": "Monaco",
168 "mda": "Moldova",
169 "mdg": "Madagascar",
170 "mdv": "Maldives",
171 "mex": "Mexico",
172 "mhl": "Marshall Islands",
173 "mkd": "North Macedonia",
174 "mli": "Mali",
175 "mlt": "Malta",
176 "mmr": "Myanmar",
177 "mne": "Montenegro",
178 "mng": "Mongolia",
179 "mnp": "Northern Mariana Islands",
180 "moz": "Mozambique",
181 "mrt": "Mauritania",
182 "msr": "Montserrat",
183 "mtq": "Martinique",
184 "mus": "Mauritius",
185 "mwi": "Malawi",
186 "mys": "Malaysia",
187 "myt": "Mayotte",
188 "nam": "Namibia",
189 "ncl": "New Caledonia",
190 "ner": "Niger",
191 "nfk": "Norfolk Island",
192 "nga": "Nigeria",
193 "nic": "Nicaragua",
194 "niu": "Niue",
195 "nld": "Netherlands (Kingdom of the)",
196 "nor": "Norway",
197 "npl": "Nepal",
198 "nru": "Naoero",
199 "nzl": "New Zealand",
200 "omn": "Oman",
201 "pak": "Pakistan",
202 "pan": "Panama",
203 "pcn": "Pitcairn",
204 "per": "Peru",
205 "phl": "Philippines",
206 "plw": "Palau",
207 "png": "Papua New Guinea",
208 "pol": "Poland",
209 "pri": "Puerto Rico",
210 "prk": "Korea North",
211 "prt": "Portugal",
212 "pry": "Paraguay",
213 "pse": "Palestine, State of",
214 "pyf": "French Polynesia",
215 "qat": "Qatar",
216 "reu": "Réunion",
217 "rou": "Romania",
218 "rus": "Russia",
219 "rwa": "Rwanda",
220 "sau": "Saudi Arabia",
221 "sdn": "Sudan",
222 "sen": "Senegal",
223 "sgp": "Singapore",
224 "sgs": "South Georgia and the South Sandwich Islands",
225 "shn": "Saint Helena Ascension and Tristan da Cunha",
226 "sjm": "Svalbard and Jan Mayen",
227 "slb": "Solomon Islands",
228 "sle": "Sierra Leone",
229 "slv": "El Salvador",
230 "smr": "San Marino",
231 "som": "Somalia",
232 "spm": "Saint Pierre and Miquelon",
233 "srb": "Serbia",
234 "ssd": "South Sudan",
235 "stp": "Sao Tome and Principe",
236 "sur": "Suriname",
237 "svk": "Slovakia",
238 "svn": "Slovenia",
239 "swe": "Sweden",
240 "swz": "Eswatini",
241 "sxm": "Sint Maarten",
242 "syc": "Seychelles",
243 "syr": "Syria",
244 "tca": "Turks and Caicos Islands",
245 "tcd": "Chad",
246 "tgo": "Togo",
247 "tha": "Thailand",
248 "tjk": "Tajikistan",
249 "tkl": "Tokelau",
250 "tkm": "Turkmenistan",
251 "tls": "Timor-Leste",
252 "ton": "Tonga",
253 "tto": "Trinidad and Tobago",
254 "tun": "Tunisia",
255 "tur": "Türkiye",
256 "tuv": "Tuvalu",
257 "twn": "Taiwan",
258 "tza": "Tanzania",
259 "uga": "Uganda",
260 "ukr": "Ukraine",
261 "umi": "United States Minor Outlying Islands",
262 "ury": "Uruguay",
263 "usa": "United States",
264 "uzb": "Uzbekistan",
265 "vat": "Holy See",
266 "vct": "Saint Vincent and the Grenadines",
267 "ven": "Venezuela",
268 "vgb": "British Virgin Islands",
269 "vir": "Virgin Islands (U.S.)",
270 "vnm": "Vietnam",
271 "vut": "Vanuatu",
272 "wlf": "Wallis and Futuna",
273 "wsm": "Samoa",
274 "yem": "Yemen",
275 "zaf": "South Africa",
276 "zmb": "Zambia",
277 "zwe": "Zimbabwe"
278 }
279 """
280 ISO 3166-1 alpha-3 (commonly referred to as ISO3) is a part of the ISO 3166 standard, which defines three-letter
281 codes to represent countries.
282 """
284 ISO2CODES = {
285 "ad": "Andorra",
286 "ae": "United Arab Emirates",
287 "af": "Afghanistan",
288 "ag": "Antigua and Barbuda",
289 "ai": "Anguilla",
290 "al": "Albania",
291 "am": "Armenia",
292 "ao": "Angola",
293 "aq": "Antarctica",
294 "ar": "Argentina",
295 "as": "American Samoa",
296 "at": "Austria",
297 "au": "Australia",
298 "aw": "Aruba",
299 "ax": "Åland Islands",
300 "az": "Azerbaijan",
301 "ba": "Bosnia and Herzegovina",
302 "bb": "Barbados",
303 "bd": "Bangladesh",
304 "be": "Belgium",
305 "bf": "Burkina Faso",
306 "bg": "Bulgaria",
307 "bh": "Bahrain",
308 "bi": "Burundi",
309 "bj": "Benin",
310 "bl": "Saint Barthélemy",
311 "bm": "Bermuda",
312 "bn": "Brunei",
313 "bo": "Bolivia",
314 "bq": "Bonaire, Sint Eustatius and Saba",
315 "br": "Brazil",
316 "bs": "Bahamas",
317 "bt": "Bhutan",
318 "bv": "Bouvet Island",
319 "bw": "Botswana",
320 "by": "Belarus",
321 "bz": "Belize",
322 "ca": "Canada",
323 "cc": "Cocos (Keeling) Islands",
324 "cd": "Congo Democratic Republic",
325 "cf": "Central African Republic",
326 "cg": "Congo Republic",
327 "ch": "Switzerland",
328 "ci": "Côte d'Ivoire",
329 "ck": "Cook Islands",
330 "cl": "Chile",
331 "cm": "Cameroon",
332 "cn": "China",
333 "co": "Colombia",
334 "cr": "Costa Rica",
335 "cu": "Cuba",
336 "cv": "Cabo Verde",
337 "cw": "Curaçao",
338 "cx": "Christmas Island",
339 "cy": "Cyprus",
340 "cz": "Czechia",
341 "de": "Germany",
342 "dj": "Djibouti",
343 "dk": "Denmark",
344 "dm": "Dominica",
345 "do": "Dominican Republic",
346 "dz": "Algeria",
347 "ec": "Ecuador",
348 "ee": "Estonia",
349 "eg": "Egypt",
350 "eh": "Western Sahara",
351 "er": "Eritrea",
352 "es": "Spain",
353 "et": "Ethiopia",
354 "fi": "Finland",
355 "fj": "Fiji",
356 "fk": "Falkland Islands",
357 "fm": "Micronesia",
358 "fo": "Faroe Islands",
359 "fr": "France",
360 "ga": "Gabon",
361 "gb": "United Kingdom",
362 "gd": "Grenada",
363 "ge": "Georgia",
364 "gf": "French Guiana",
365 "gg": "Guernsey",
366 "gh": "Ghana",
367 "gi": "Gibraltar",
368 "gl": "Greenland",
369 "gm": "Gambia",
370 "gn": "Guinea",
371 "gp": "Guadeloupe",
372 "gq": "Equatorial Guinea",
373 "gr": "Greece",
374 "gs": "South Georgia and the South Sandwich Islands",
375 "gt": "Guatemala",
376 "gu": "Guam",
377 "gw": "Guinea-Bissau",
378 "gy": "Guyana",
379 "hk": "Hong Kong",
380 "hm": "Heard Island and McDonald Islands",
381 "hn": "Honduras",
382 "hr": "Croatia",
383 "ht": "Haiti",
384 "hu": "Hungary",
385 "id": "Indonesia",
386 "ie": "Ireland",
387 "il": "Israel",
388 "im": "Isle of Man",
389 "in": "India",
390 "io": "British Indian Ocean Territory",
391 "iq": "Iraq",
392 "ir": "Iran",
393 "is": "Iceland",
394 "it": "Italy",
395 "je": "Jersey",
396 "jm": "Jamaica",
397 "jo": "Jordan",
398 "jp": "Japan",
399 "ke": "Kenya",
400 "kg": "Kyrgyzstan",
401 "kh": "Cambodia",
402 "ki": "Kiribati",
403 "km": "Comoros",
404 "kn": "Saint Kitts and Nevis",
405 "kp": "Korea North",
406 "kr": "Korea South",
407 "kw": "Kuwait",
408 "ky": "Cayman Islands",
409 "kz": "Kazakhstan",
410 "la": "Laos",
411 "lb": "Lebanon",
412 "lc": "Saint Lucia",
413 "li": "Liechtenstein",
414 "lk": "Sri Lanka",
415 "lr": "Liberia",
416 "ls": "Lesotho",
417 "lt": "Lithuania",
418 "lu": "Luxembourg",
419 "lv": "Latvia",
420 "ly": "Libya",
421 "ma": "Morocco",
422 "mc": "Monaco",
423 "md": "Moldova",
424 "me": "Montenegro",
425 "mf": "Saint Martin",
426 "mg": "Madagascar",
427 "mh": "Marshall Islands",
428 "mk": "North Macedonia",
429 "ml": "Mali",
430 "mm": "Myanmar",
431 "mn": "Mongolia",
432 "mo": "Macao",
433 "mp": "Northern Mariana Islands",
434 "mq": "Martinique",
435 "mr": "Mauritania",
436 "ms": "Montserrat",
437 "mt": "Malta",
438 "mu": "Mauritius",
439 "mv": "Maldives",
440 "mw": "Malawi",
441 "mx": "Mexico",
442 "my": "Malaysia",
443 "mz": "Mozambique",
444 "na": "Namibia",
445 "nc": "New Caledonia",
446 "ne": "Niger",
447 "nf": "Norfolk Island",
448 "ng": "Nigeria",
449 "ni": "Nicaragua",
450 "nl": "Netherlands (Kingdom of the)",
451 "no": "Norway",
452 "np": "Nepal",
453 "nr": "Naoero",
454 "nu": "Niue",
455 "nz": "New Zealand",
456 "om": "Oman",
457 "pa": "Panama",
458 "pe": "Peru",
459 "pf": "French Polynesia",
460 "pg": "Papua New Guinea",
461 "ph": "Philippines",
462 "pk": "Pakistan",
463 "pl": "Poland",
464 "pm": "Saint Pierre and Miquelon",
465 "pn": "Pitcairn",
466 "pr": "Puerto Rico",
467 "ps": "Palestine, State of",
468 "pt": "Portugal",
469 "pw": "Palau",
470 "py": "Paraguay",
471 "qa": "Qatar",
472 "re": "Réunion",
473 "ro": "Romania",
474 "rs": "Serbia",
475 "ru": "Russia",
476 "rw": "Rwanda",
477 "sa": "Saudi Arabia",
478 "sb": "Solomon Islands",
479 "sc": "Seychelles",
480 "sd": "Sudan",
481 "se": "Sweden",
482 "sg": "Singapore",
483 "sh": "Saint Helena Ascension and Tristan da Cunha",
484 "si": "Slovenia",
485 "sj": "Svalbard and Jan Mayen",
486 "sk": "Slovakia",
487 "sl": "Sierra Leone",
488 "sm": "San Marino",
489 "sn": "Senegal",
490 "so": "Somalia",
491 "sr": "Suriname",
492 "ss": "South Sudan",
493 "st": "Sao Tome and Principe",
494 "sv": "El Salvador",
495 "sx": "Sint Maarten",
496 "sy": "Syria",
497 "sz": "Eswatini",
498 "tc": "Turks and Caicos Islands",
499 "td": "Chad",
500 "tf": "French Southern Territories",
501 "tg": "Togo",
502 "th": "Thailand",
503 "tj": "Tajikistan",
504 "tk": "Tokelau",
505 "tl": "Timor-Leste",
506 "tm": "Turkmenistan",
507 "tn": "Tunisia",
508 "to": "Tonga",
509 "tr": "Türkiye",
510 "tt": "Trinidad and Tobago",
511 "tv": "Tuvalu",
512 "tw": "Taiwan",
513 "tz": "Tanzania",
514 "ua": "Ukraine",
515 "ug": "Uganda",
516 "um": "United States Minor Outlying Islands",
517 "us": "United States",
518 "uy": "Uruguay",
519 "uz": "Uzbekistan",
520 "va": "Holy See",
521 "vc": "Saint Vincent and the Grenadines",
522 "ve": "Venezuela",
523 "vg": "British Virgin Islands",
524 "vi": "Virgin Islands (U.S.)",
525 "vn": "Vietnam",
526 "vu": "Vanuatu",
527 "wf": "Wallis and Futuna",
528 "ws": "Samoa",
529 "ye": "Yemen",
530 "yt": "Mayotte",
531 "za": "South Africa",
532 "zm": "Zambia",
533 "zw": "Zimbabwe"
534 }
535 """
536 The ISO 2 country code (also known as ISO 3166-1 alpha-2) is an international standard consisting of two-letter
537 codes used to identify countries.
538 """
539 ISO3TOISO2 = { # Convert iso3 to iso2 codes
540 "abw": "aw",
541 "afg": "af",
542 "ago": "ao",
543 "aia": "ai",
544 "ala": "ax",
545 "alb": "al",
546 "and": "ad",
547 "are": "ae",
548 "arg": "ar",
549 "arm": "am",
550 "asm": "as",
551 "ata": "aq",
552 "atf": "tf",
553 "atg": "ag",
554 "aus": "au",
555 "aut": "at",
556 "aze": "az",
557 "bdi": "bi",
558 "bel": "be",
559 "ben": "bj",
560 "bes": "bq",
561 "bfa": "bf",
562 "bgd": "bd",
563 "bgr": "bg",
564 "bhr": "bh",
565 "bhs": "bs",
566 "bih": "ba",
567 "blm": "bl",
568 "blr": "by",
569 "blz": "bz",
570 "bmu": "bm",
571 "bol": "bo",
572 "bra": "br",
573 "brb": "bb",
574 "brn": "bn",
575 "btn": "bt",
576 "bvt": "bv",
577 "bwa": "bw",
578 "caf": "cf",
579 "can": "ca",
580 "cck": "cc",
581 "che": "ch",
582 "chl": "cl",
583 "chn": "cn",
584 "civ": "ci",
585 "cmr": "cm",
586 "cod": "cd",
587 "cog": "cg",
588 "cok": "ck",
589 "col": "co",
590 "com": "km",
591 "cpv": "cv",
592 "cri": "cr",
593 "cub": "cu",
594 "cuw": "cw",
595 "cxr": "cx",
596 "cym": "ky",
597 "cyp": "cy",
598 "cze": "cz",
599 "deu": "de",
600 "dji": "dj",
601 "dma": "dm",
602 "dnk": "dk",
603 "dom": "do",
604 "dza": "dz",
605 "ecu": "ec",
606 "egy": "eg",
607 "eri": "er",
608 "esh": "eh",
609 "esp": "es",
610 "est": "ee",
611 "eth": "et",
612 "fin": "fi",
613 "fji": "fj",
614 "flk": "fk",
615 "fra": "fr",
616 "fro": "fo",
617 "fsm": "fm",
618 "gab": "ga",
619 "gbr": "gb",
620 "geo": "ge",
621 "ggy": "gg",
622 "gha": "gh",
623 "gib": "gi",
624 "gin": "gn",
625 "glp": "gp",
626 "gmb": "gm",
627 "gnb": "gw",
628 "gnq": "gq",
629 "grc": "gr",
630 "grd": "gd",
631 "grl": "gl",
632 "gtm": "gt",
633 "guf": "gf",
634 "gum": "gu",
635 "guy": "gy",
636 "hkg": "hk",
637 "hmd": "hm",
638 "hnd": "hn",
639 "hrv": "hr",
640 "hti": "ht",
641 "hun": "hu",
642 "idn": "id",
643 "imn": "im",
644 "ind": "in",
645 "iot": "io",
646 "irl": "ie",
647 "irn": "ir",
648 "irq": "iq",
649 "isl": "is",
650 "isr": "il",
651 "ita": "it",
652 "jam": "jm",
653 "jey": "je",
654 "jor": "jo",
655 "jpn": "jp",
656 "kaz": "kz",
657 "ken": "ke",
658 "kgz": "kg",
659 "khm": "kh",
660 "kir": "ki",
661 "kna": "kn",
662 "kor": "kr",
663 "kwt": "kw",
664 "lao": "la",
665 "lbn": "lb",
666 "lbr": "lr",
667 "lby": "ly",
668 "lca": "lc",
669 "lie": "li",
670 "lka": "lk",
671 "lso": "ls",
672 "ltu": "lt",
673 "lux": "lu",
674 "lva": "lv",
675 "mac": "mo",
676 "maf": "mf",
677 "mar": "ma",
678 "mco": "mc",
679 "mda": "md",
680 "mdg": "mg",
681 "mdv": "mv",
682 "mex": "mx",
683 "mhl": "mh",
684 "mkd": "mk",
685 "mli": "ml",
686 "mlt": "mt",
687 "mmr": "mm",
688 "mne": "me",
689 "mng": "mn",
690 "mnp": "mp",
691 "moz": "mz",
692 "mrt": "mr",
693 "msr": "ms",
694 "mtq": "mq",
695 "mus": "mu",
696 "mwi": "mw",
697 "mys": "my",
698 "myt": "yt",
699 "nam": "na",
700 "ncl": "nc",
701 "ner": "ne",
702 "nfk": "nf",
703 "nga": "ng",
704 "nic": "ni",
705 "niu": "nu",
706 "nld": "nl",
707 "nor": "no",
708 "npl": "np",
709 "nru": "nr",
710 "nzl": "nz",
711 "omn": "om",
712 "pak": "pk",
713 "pan": "pa",
714 "pcn": "pn",
715 "per": "pe",
716 "phl": "ph",
717 "plw": "pw",
718 "png": "pg",
719 "pol": "pl",
720 "pri": "pr",
721 "prk": "kp",
722 "prt": "pt",
723 "pry": "py",
724 "pse": "ps",
725 "pyf": "pf",
726 "qat": "qa",
727 "reu": "re",
728 "rou": "ro",
729 "rus": "ru",
730 "rwa": "rw",
731 "sau": "sa",
732 "sdn": "sd",
733 "sen": "sn",
734 "sgp": "sg",
735 "sgs": "gs",
736 "shn": "sh",
737 "sjm": "sj",
738 "slb": "sb",
739 "sle": "sl",
740 "slv": "sv",
741 "smr": "sm",
742 "som": "so",
743 "spm": "pm",
744 "srb": "rs",
745 "ssd": "ss",
746 "stp": "st",
747 "sur": "sr",
748 "svk": "sk",
749 "svn": "si",
750 "swe": "se",
751 "swz": "sz",
752 "sxm": "sx",
753 "syc": "sc",
754 "syr": "sy",
755 "tca": "tc",
756 "tcd": "td",
757 "tgo": "tg",
758 "tha": "th",
759 "tjk": "tj",
760 "tkl": "tk",
761 "tkm": "tm",
762 "tls": "tl",
763 "ton": "to",
764 "tto": "tt",
765 "tun": "tn",
766 "tur": "tr",
767 "tuv": "tv",
768 "twn": "tw",
769 "tza": "tz",
770 "uga": "ug",
771 "ukr": "ua",
772 "umi": "um",
773 "ury": "uy",
774 "usa": "us",
775 "uzb": "uz",
776 "vat": "va",
777 "vct": "vc",
778 "ven": "ve",
779 "vgb": "vg",
780 "vir": "vi",
781 "vnm": "vn",
782 "vut": "vu",
783 "wlf": "wf",
784 "wsm": "ws",
785 "yem": "ye",
786 "zaf": "za",
787 "zmb": "zm",
788 "zwe": "zw",
789 }
790 """A Map of ISO3 to ISO2 country codes"""
792 ISO2TOISO3 = {v: k for k, v in ISO3TOISO2.items()} # Build the invert map
793 """A built Map of ISO2 to ISO3 country codes"""
795 subgroup_mappings = {
796 "dach": ["de", "at", "ch"],
797 "eu": ["at", "be", "bg", "hr", "cy", "cz", "dk", "ee", "fi", "fr", "de", "gr", "hu", "ie", "it", "lv", "lt",
798 "lu", "mt", "nl", "pl", "pt", "ro", "sk", "si", "es", "se"],
799 }
801 type = "select.country"
802 ISO2 = 2
803 ISO3 = 3
805 def __init__(self, *, descr="Country", codes=ISO2, values=None, **kwargs):
806 assert codes in [self.ISO2, self.ISO3]
807 # assert values is None
809 if values is None:
810 values = dict(
811 sorted((self.ISO2CODES if codes == self.ISO2 else self.ISO3CODES).items(), key=lambda i: i[1]))
812 else:
813 if isinstance(values, str):
814 values = self.subgroup_mappings[values]
815 if codes == self.ISO3:
816 values = [self.ISO2TOISO3[i] for i in values]
817 if isinstance(values, list):
818 if codes == self.ISO2:
819 values = {k: self.ISO2CODES[k] for k in values}
820 elif codes == self.ISO3:
821 values = {k: self.ISO3CODES[k] for k in values}
822 if isinstance(values, dict):
823 values = dict(sorted(values.items(), key=lambda i: i[1]))
825 super().__init__(
826 values=values,
827 descr=descr,
828 **kwargs
829 )
831 self.codes = codes
833 def singleValueUnserialize(self, val):
834 """
835 Unserializes a single value, converting ISO country codes between ISO 3166-1 alpha-2 and alpha-3 if necessary.
837 This method takes a country code string (either ISO 3166-1 alpha-2 or alpha-3) and checks if a conversion is
838 needed based on the `self.codes` attribute. If a conversion is required, it attempts to perform the conversion
839 using the `ISO3TOISO2` or `ISO2TOISO3` dictionaries. If the conversion is successful, the converted code is
840 returned; otherwise, the original value is returned.
842 :param val: The value to be unserialized, typically a string representing an ISO country code.
844 :return: The unserialized value, either the original or converted ISO country code.
845 """
846 if isinstance(val, str) and len(val) == 3 and self.codes == self.ISO2:
847 # We got an ISO3 code from the db, but are using ISO2
848 try:
849 return self.ISO3TOISO2[val]
850 except KeyError:
851 pass
852 elif isinstance(val, str) and len(val) == 2 and self.codes == self.ISO3:
853 # We got ISO2 code, wanted ISO3
854 try:
855 return self.ISO2TOISO3[val]
856 except KeyError:
857 pass
858 return val