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