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.6.10, created at 2025-02-07 19:28 +0000

1""" 

2A bone representing a country selection input field in a web application or form. 

3 

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 

9 

10 

11class SelectCountryBone(SelectBone): 

12 """ 

13 A bone representing a country selection input field in a web application or form. 

14 

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). 

18 

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. 

23 """ 

24 

25 ISO3CODES = { 

26 "abw": "Aruba", 

27 "afg": "Afghanistan", 

28 "ago": "Angola", 

29 "aia": "Anguilla", 

30 "alb": "Albania", 

31 "and": "Andorra", 

32 "ant": "Netherlands Antilles", 

33 "are": "United Arab Emirates", 

34 "arg": "Argentina", 

35 "arm": "Armenia", 

36 "asm": "American Samoa", 

37 "ata": "Antarctica", 

38 "atf": "French Southern Territories", 

39 "atg": "Antigua and Barbuda", 

40 "aus": "Australia", 

41 "aut": "Austria", 

42 "aze": "Azerbaijan", 

43 "bdi": "Burundi", 

44 "bel": "Belgium", 

45 "ben": "Benin", 

46 "bfa": "Burkina Faso", 

47 "bgd": "Bangladesh", 

48 "bgr": "Bulgaria", 

49 "bhr": "Bahrain", 

50 "bhs": "Bahamas", 

51 "bih": "Bosnia and Herzegovina", 

52 "blm": "Saint Barthelemy", 

53 "blr": "Belarus", 

54 "blz": "Belize", 

55 "bmu": "Bermuda", 

56 "bol": "Bolivia", 

57 "bra": "Brazil", 

58 "brb": "Barbados", 

59 "brn": "Brunei", 

60 "btn": "Bhutan", 

61 "bvt": "Bouvet Island", 

62 "bwa": "Botswana", 

63 "caf": "Central African Republic", 

64 "can": "Canada", 

65 "cck": "Cocos Islands", 

66 "che": "Switzerland", 

67 "chl": "Chile", 

68 "chn": "China", 

69 "civ": "Ivory Coast", 

70 "cmr": "Cameroon", 

71 "cod": "Congo Democratic Republic", 

72 "cog": "Congo Republic", 

73 "cok": "Cook Islands", 

74 "col": "Colombia", 

75 "com": "Comoros", 

76 "cpv": "Cape Verde", 

77 "cri": "Costa Rica", 

78 "cub": "Cuba", 

79 "cxr": "Christmas Island", 

80 "cym": "Cayman Islands", 

81 "cyp": "Cyprus", 

82 "cze": "Czech Republic", 

83 "deu": "Germany", 

84 "dji": "Djibouti", 

85 "dma": "Dominica", 

86 "dnk": "Denmark", 

87 "dom": "Dominican Republic", 

88 "dza": "Algeria", 

89 "ecu": "Ecuador", 

90 "egy": "Egypt", 

91 "eri": "Eritrea", 

92 "esh": "Western Sahara", 

93 "esp": "Spain", 

94 "est": "Estonia", 

95 "eth": "Ethiopia", 

96 "fin": "Finland", 

97 "fji": "Fiji", 

98 "flk": "Falkland Islands", 

99 "fra": "France", 

100 "fro": "Faroe Islands", 

101 "fsm": "Micronesia", 

102 "gab": "Gabon", 

103 "gbr": "United Kingdom", 

104 "geo": "Georgia", 

105 "ggy": "Guernsey", 

106 "gha": "Ghana", 

107 "gib": "Gibraltar", 

108 "gin": "Guinea", 

109 "gmb": "Gambia", 

110 "gnb": "Guinea-Bissau", 

111 "gnq": "Equatorial Guinea", 

112 "grc": "Greece", 

113 "grd": "Grenada", 

114 "grl": "Greenland", 

115 "gtm": "Guatemala", 

116 "gum": "Guam", 

117 "guy": "Guyana", 

118 "hkg": "Hong Kong", 

119 "hmd": "Heard Island and McDonald Islands", 

120 "hnd": "Honduras", 

121 "hrv": "Croatia", 

122 "hti": "Haiti", 

123 "hun": "Hungary", 

124 "idn": "Indonesia", 

125 "imn": "Isle of Man", 

126 "ind": "India", 

127 "iot": "British Indian Ocean Territory", 

128 "irl": "Ireland", 

129 "irn": "Iran", 

130 "irq": "Iraq", 

131 "isl": "Iceland", 

132 "isr": "Israel", 

133 "ita": "Italy", 

134 "jam": "Jamaica", 

135 "jey": "Jersey", 

136 "jor": "Jordan", 

137 "jpn": "Japan", 

138 "kaz": "Kazakhstan", 

139 "ken": "Kenya", 

140 "kgz": "Kyrgyzstan", 

141 "khm": "Cambodia", 

142 "kir": "Kiribati", 

143 "kna": "Saint Kitts and Nevis", 

144 "kor": "Korea South", 

145 "kwt": "Kuwait", 

146 "lao": "Laos", 

147 "lbn": "Lebanon", 

148 "lbr": "Liberia", 

149 "lby": "Libya", 

150 "lca": "Saint Lucia", 

151 "lie": "Liechtenstein", 

152 "lka": "Sri Lanka", 

153 "lso": "Lesotho", 

154 "ltu": "Lithuania", 

155 "lux": "Luxembourg", 

156 "lva": "Latvia", 

157 "mac": "Macao", 

158 "maf": "Saint Martin", 

159 "mar": "Morocco", 

160 "mco": "Monaco", 

161 "mda": "Moldova", 

162 "mdg": "Madagascar", 

163 "mdv": "Maldives", 

164 "mex": "Mexico", 

165 "mhl": "Marshall Islands", 

166 "mkd": "Macedonia", 

167 "mli": "Mali", 

168 "mlt": "Malta", 

169 "mmr": "Myanmar", 

170 "mne": "Montenegro", 

171 "mng": "Mongolia", 

172 "mnp": "Northern Mariana Islands", 

173 "moz": "Mozambique", 

174 "mrt": "Mauritania", 

175 "msr": "Montserrat", 

176 "mus": "Mauritius", 

177 "mwi": "Malawi", 

178 "mys": "Malaysia", 

179 "myt": "Mayotte", 

180 "nam": "Namibia", 

181 "ncl": "New Caledonia", 

182 "ner": "Niger", 

183 "nfk": "Norfolk Island", 

184 "nga": "Nigeria", 

185 "nic": "Nicaragua", 

186 "niu": "Niue", 

187 "nld": "Netherlands", 

188 "nor": "Norway", 

189 "npl": "Nepal", 

190 "nru": "Nauru", 

191 "nzl": "New Zealand", 

192 "omn": "Oman", 

193 "pak": "Pakistan", 

194 "pan": "Panama", 

195 "pcn": "Pitcairn", 

196 "per": "Peru", 

197 "phl": "Philippines", 

198 "plw": "Palau", 

199 "png": "Papua New Guinea", 

200 "pol": "Poland", 

201 "pri": "Puerto Rico", 

202 "prk": "Korea North", 

203 "prt": "Portugal", 

204 "pry": "Paraguay", 

205 "pse": "Palestinian Territory", 

206 "pyf": "French Polynesia", 

207 "qat": "Qatar", 

208 "rou": "Romania", 

209 "rus": "Russia", 

210 "rwa": "Rwanda", 

211 "sau": "Saudi Arabia", 

212 "sdn": "Sudan", 

213 "sen": "Senegal", 

214 "sgp": "Singapore", 

215 "shn": "Saint Helena Ascension and Tristan da Cunha", 

216 "sjm": "Svalbard", 

217 "slb": "Solomon Islands", 

218 "sle": "Sierra Leone", 

219 "slv": "El Salvador", 

220 "smr": "San Marino", 

221 "som": "Somalia", 

222 "spm": "Saint Pierre and Miquelon", 

223 "srb": "Serbia", 

224 "stp": "Sao Tome and Principe", 

225 "sur": "Suriname", 

226 "svk": "Slovakia", 

227 "svn": "Slovenia", 

228 "swe": "Sweden", 

229 "swz": "Swaziland", 

230 "syc": "Seychelles", 

231 "syr": "Syria", 

232 "tca": "Turks and Caicos Islands", 

233 "tcd": "Chad", 

234 "tgo": "Togo", 

235 "tha": "Thailand", 

236 "tjk": "Tajikistan", 

237 "tkl": "Tokelau", 

238 "tkm": "Turkmenistan", 

239 "tls": "Timor-Leste", 

240 "ton": "Tonga", 

241 "tto": "Trinidad and Tobago", 

242 "tun": "Tunisia", 

243 "tur": "Turkey", 

244 "tuv": "Tuvalu", 

245 "twn": "Taiwan", 

246 "tza": "Tanzania", 

247 "uga": "Uganda", 

248 "ukr": "Ukraine", 

249 "ury": "Uruguay", 

250 "usa": "United States", 

251 "uzb": "Uzbekistan", 

252 "vat": "Holy See", 

253 "vct": "Saint Vincent and the Grenadines", 

254 "ven": "Venezuela", 

255 "vgb": "British Virgin Islands", 

256 "vir": "Virgin Islands", 

257 "vnm": "Vietnam", 

258 "vut": "Vanuatu", 

259 "wlf": "Wallis and Futuna", 

260 "wsm": "Samoa", 

261 "yem": "Yemen", 

262 "zaf": "South Africa", 

263 "zmb": "Zambia", 

264 "zwe": "Zimbabwe" 

265 } 

266 """ 

267 ISO 3166-1 alpha-3 (commonly referred to as ISO3) is a part of the ISO 3166 standard, which defines three-letter 

268 codes to represent countries. 

269 """ 

270 

271 ISO2CODES = { 

272 "aw": "Aruba", 

273 "af": "Afghanistan", 

274 "ao": "Angola", 

275 "ai": "Anguilla", 

276 "al": "Albania", 

277 "ad": "Andorra", 

278 "an": "Netherlands Antilles", 

279 "ae": "United Arab Emirates", 

280 "ar": "Argentina", 

281 "am": "Armenia", 

282 "as": "American Samoa", 

283 "aq": "Antarctica", 

284 "tf": "French Southern Territories", 

285 "ag": "Antigua and Barbuda", 

286 "au": "Australia", 

287 "at": "Austria", 

288 "az": "Azerbaijan", 

289 "bi": "Burundi", 

290 "be": "Belgium", 

291 "bj": "Benin", 

292 "bf": "Burkina Faso", 

293 "bd": "Bangladesh", 

294 "bg": "Bulgaria", 

295 "bh": "Bahrain", 

296 "bs": "Bahamas", 

297 "ba": "Bosnia and Herzegovina", 

298 "bl": "Saint Barthelemy", 

299 "by": "Belarus", 

300 "bz": "Belize", 

301 "bm": "Bermuda", 

302 "bo": "Bolivia", 

303 "br": "Brazil", 

304 "bb": "Barbados", 

305 "bn": "Brunei", 

306 "bt": "Bhutan", 

307 "bv": "Bouvet Island", 

308 "bw": "Botswana", 

309 "cf": "Central African Republic", 

310 "ca": "Canada", 

311 "cc": "Cocos Islands", 

312 "ch": "Switzerland", 

313 "cl": "Chile", 

314 "cn": "China", 

315 "ci": "Ivory Coast", 

316 "cm": "Cameroon", 

317 "cd": "Congo Democratic Republic", 

318 "cg": "Congo Republic", 

319 "ck": "Cook Islands", 

320 "co": "Colombia", 

321 "km": "Comoros", 

322 "cv": "Cape Verde", 

323 "cr": "Costa Rica", 

324 "cu": "Cuba", 

325 "cx": "Christmas Island", 

326 "ky": "Cayman Islands", 

327 "cy": "Cyprus", 

328 "cz": "Czech Republic", 

329 "de": "Germany", 

330 "dj": "Djibouti", 

331 "dm": "Dominica", 

332 "dk": "Denmark", 

333 "do": "Dominican Republic", 

334 "dz": "Algeria", 

335 "ec": "Ecuador", 

336 "eg": "Egypt", 

337 "er": "Eritrea", 

338 "eh": "Western Sahara", 

339 "es": "Spain", 

340 "ee": "Estonia", 

341 "et": "Ethiopia", 

342 "fi": "Finland", 

343 "fj": "Fiji", 

344 "fk": "Falkland Islands", 

345 "fr": "France", 

346 "fo": "Faroe Islands", 

347 "fm": "Micronesia", 

348 "ga": "Gabon", 

349 "gb": "United Kingdom", 

350 "ge": "Georgia", 

351 "gg": "Guernsey", 

352 "gh": "Ghana", 

353 "gi": "Gibraltar", 

354 "gn": "Guinea", 

355 "gm": "Gambia", 

356 "gw": "Guinea-Bissau", 

357 "gq": "Equatorial Guinea", 

358 "gr": "Greece", 

359 "gd": "Grenada", 

360 "gl": "Greenland", 

361 "gt": "Guatemala", 

362 "gu": "Guam", 

363 "gy": "Guyana", 

364 "hk": "Hong Kong", 

365 "hm": "Heard Island and McDonald Islands", 

366 "hn": "Honduras", 

367 "hr": "Croatia", 

368 "ht": "Haiti", 

369 "hu": "Hungary", 

370 "id": "Indonesia", 

371 "im": "Isle of Man", 

372 "in": "India", 

373 "io": "British Indian Ocean Territory", 

374 "ie": "Ireland", 

375 "ir": "Iran", 

376 "iq": "Iraq", 

377 "is": "Iceland", 

378 "il": "Israel", 

379 "it": "Italy", 

380 "jm": "Jamaica", 

381 "je": "Jersey", 

382 "jo": "Jordan", 

383 "jp": "Japan", 

384 "kz": "Kazakhstan", 

385 "ke": "Kenya", 

386 "kg": "Kyrgyzstan", 

387 "kh": "Cambodia", 

388 "ki": "Kiribati", 

389 "kn": "Saint Kitts and Nevis", 

390 "kr": "Korea South", 

391 "kw": "Kuwait", 

392 "la": "Laos", 

393 "lb": "Lebanon", 

394 "lr": "Liberia", 

395 "ly": "Libya", 

396 "lc": "Saint Lucia", 

397 "li": "Liechtenstein", 

398 "lk": "Sri Lanka", 

399 "ls": "Lesotho", 

400 "lt": "Lithuania", 

401 "lu": "Luxembourg", 

402 "lv": "Latvia", 

403 "mo": "Macao", 

404 "mf": "Saint Martin", 

405 "ma": "Morocco", 

406 "mc": "Monaco", 

407 "md": "Moldova", 

408 "mg": "Madagascar", 

409 "mv": "Maldives", 

410 "mx": "Mexico", 

411 "mh": "Marshall Islands", 

412 "mk": "Macedonia", 

413 "ml": "Mali", 

414 "mt": "Malta", 

415 "mm": "Myanmar", 

416 "me": "Montenegro", 

417 "mn": "Mongolia", 

418 "mp": "Northern Mariana Islands", 

419 "mz": "Mozambique", 

420 "mr": "Mauritania", 

421 "ms": "Montserrat", 

422 "mu": "Mauritius", 

423 "mw": "Malawi", 

424 "my": "Malaysia", 

425 "yt": "Mayotte", 

426 "na": "Namibia", 

427 "nc": "New Caledonia", 

428 "ne": "Niger", 

429 "nf": "Norfolk Island", 

430 "ng": "Nigeria", 

431 "ni": "Nicaragua", 

432 "nu": "Niue", 

433 "nl": "Netherlands", 

434 "no": "Norway", 

435 "np": "Nepal", 

436 "nr": "Nauru", 

437 "nz": "New Zealand", 

438 "om": "Oman", 

439 "pk": "Pakistan", 

440 "pa": "Panama", 

441 "pn": "Pitcairn", 

442 "pe": "Peru", 

443 "ph": "Philippines", 

444 "pw": "Palau", 

445 "pg": "Papua New Guinea", 

446 "pl": "Poland", 

447 "pr": "Puerto Rico", 

448 "kp": "Korea North", 

449 "pt": "Portugal", 

450 "py": "Paraguay", 

451 "ps": "Palestinian Territory", 

452 "pf": "French Polynesia", 

453 "qa": "Qatar", 

454 "ro": "Romania", 

455 "ru": "Russia", 

456 "rw": "Rwanda", 

457 "sa": "Saudi Arabia", 

458 "sd": "Sudan", 

459 "sn": "Senegal", 

460 "sg": "Singapore", 

461 "sh": "Saint Helena Ascension and Tristan da Cunha", 

462 "sj": "Svalbard", 

463 "sb": "Solomon Islands", 

464 "sl": "Sierra Leone", 

465 "sv": "El Salvador", 

466 "sm": "San Marino", 

467 "so": "Somalia", 

468 "pm": "Saint Pierre and Miquelon", 

469 "rs": "Serbia", 

470 "st": "Sao Tome and Principe", 

471 "sr": "Suriname", 

472 "sk": "Slovakia", 

473 "si": "Slovenia", 

474 "se": "Sweden", 

475 "sz": "Swaziland", 

476 "sc": "Seychelles", 

477 "sy": "Syria", 

478 "tc": "Turks and Caicos Islands", 

479 "td": "Chad", 

480 "tg": "Togo", 

481 "th": "Thailand", 

482 "tj": "Tajikistan", 

483 "tk": "Tokelau", 

484 "tm": "Turkmenistan", 

485 "tl": "Timor-Leste", 

486 "to": "Tonga", 

487 "tt": "Trinidad and Tobago", 

488 "tn": "Tunisia", 

489 "tr": "Turkey", 

490 "tv": "Tuvalu", 

491 "tw": "Taiwan", 

492 "tz": "Tanzania", 

493 "ug": "Uganda", 

494 "ua": "Ukraine", 

495 "uy": "Uruguay", 

496 "us": "United States", 

497 "uz": "Uzbekistan", 

498 "va": "Holy See", 

499 "vc": "Saint Vincent and the Grenadines", 

500 "ve": "Venezuela", 

501 "vg": "British Virgin Islands", 

502 "vi": "Virgin Islands", 

503 "vn": "Vietnam", 

504 "vu": "Vanuatu", 

505 "wf": "Wallis and Futuna", 

506 "ws": "Samoa", 

507 "ye": "Yemen", 

508 "za": "South Africa", 

509 "zm": "Zambia", 

510 "zw": "Zimbabwe" 

511 } 

512 """ 

513 The ISO 2 country code (also known as ISO 3166-1 alpha-2) is an international standard consisting of two-letter 

514 codes used to identify countries. 

515 """ 

516 ISO3TOISO2 = { # Convert iso3 to iso2 codes 

517 "yem": "ye", 

518 "bvt": "bv", 

519 "mnp": "mp", 

520 "lso": "ls", 

521 "uga": "ug", 

522 "tkm": "tm", 

523 "alb": "al", 

524 "ita": "it", 

525 "tto": "tt", 

526 "nld": "nl", 

527 "moz": "mz", 

528 "tcd": "td", 

529 "blr": "by", 

530 "mne": "me", 

531 "mng": "mn", 

532 "bfa": "bf", 

533 "nga": "ng", 

534 "zmb": "zm", 

535 "gmb": "gm", 

536 "hrv": "hr", 

537 "gtm": "gt", 

538 "lka": "lk", 

539 "aus": "au", 

540 "jam": "jm", 

541 "pcn": "pn", 

542 "aut": "at", 

543 "ven": "ve", 

544 "vct": "vc", 

545 "mwi": "mw", 

546 "fin": "fi", 

547 "tkl": "tk", 

548 "rwa": "rw", 

549 "ant": "an", 

550 "bih": "ba", 

551 "cpv": "cv", 

552 "tjk": "tj", 

553 "pse": "ps", 

554 "lca": "lc", 

555 "geo": "ge", 

556 "atf": "tf", 

557 "nor": "no", 

558 "mhl": "mh", 

559 "png": "pg", 

560 "wsm": "ws", 

561 "zwe": "zw", 

562 "gum": "gu", 

563 "gbr": "gb", 

564 "civ": "ci", 

565 "guy": "gy", 

566 "cri": "cr", 

567 "cmr": "cm", 

568 "shn": "sh", 

569 "lie": "li", 

570 "nfk": "nf", 

571 "mda": "md", 

572 "mdg": "mg", 

573 "hti": "ht", 

574 "aze": "az", 

575 "lao": "la", 

576 "are": "ae", 

577 "chn": "cn", 

578 "arg": "ar", 

579 "sen": "sn", 

580 "btn": "bt", 

581 "mdv": "mv", 

582 "arm": "am", 

583 "est": "ee", 

584 "mus": "mu", 

585 "blz": "bz", 

586 "lux": "lu", 

587 "irq": "iq", 

588 "bdi": "bi", 

589 "smr": "sm", 

590 "per": "pe", 

591 "brb": "bb", 

592 "blm": "bl", 

593 "irl": "ie", 

594 "sur": "sr", 

595 "irn": "ir", 

596 "abw": "aw", 

597 "lva": "lv", 

598 "tca": "tc", 

599 "ner": "ne", 

600 "esh": "eh", 

601 "plw": "pw", 

602 "ken": "ke", 

603 "jor": "jo", 

604 "tur": "tr", 

605 "ggy": "gg", 

606 "omn": "om", 

607 "tuv": "tv", 

608 "mmr": "mm", 

609 "bwa": "bw", 

610 "ecu": "ec", 

611 "tun": "tn", 

612 "swe": "se", 

613 "rus": "ru", 

614 "mex": "mx", 

615 "asm": "as", 

616 "tls": "tl", 

617 "atg": "ag", 

618 "bgd": "bd", 

619 "ltu": "lt", 

620 "ata": "aq", 

621 "isr": "il", 

622 "caf": "cf", 

623 "idn": "id", 

624 "vut": "vu", 

625 "bol": "bo", 

626 "cod": "cd", 

627 "cog": "cg", 

628 "sjm": "sj", 

629 "eth": "et", 

630 "com": "km", 

631 "col": "co", 

632 "wlf": "wf", 

633 "cxr": "cx", 

634 "ago": "ao", 

635 "zaf": "za", 

636 "sgp": "sg", 

637 "som": "so", 

638 "uzb": "uz", 

639 "isl": "is", 

640 "vir": "vi", 

641 "brn": "bn", 

642 "pol": "pl", 

643 "kwt": "kw", 

644 "imn": "im", 

645 "tgo": "tg", 

646 "bra": "br", 

647 "fra": "fr", 

648 "mkd": "mk", 

649 "che": "ch", 

650 "usa": "us", 

651 "jey": "je", 

652 "fro": "fo", 

653 "msr": "ms", 

654 "dnk": "dk", 

655 "hkg": "hk", 

656 "swz": "sz", 

657 "ton": "to", 

658 "gib": "gi", 

659 "gin": "gn", 

660 "kor": "kr", 

661 "vat": "va", 

662 "cub": "cu", 

663 "mco": "mc", 

664 "dza": "dz", 

665 "cyp": "cy", 

666 "hun": "hu", 

667 "kgz": "kg", 

668 "fji": "fj", 

669 "dji": "dj", 

670 "ncl": "nc", 

671 "bmu": "bm", 

672 "hmd": "hm", 

673 "sdn": "sd", 

674 "gab": "ga", 

675 "nru": "nr", 

676 "hnd": "hn", 

677 "dma": "dm", 

678 "gnq": "gq", 

679 "ben": "bj", 

680 "bel": "be", 

681 "slv": "sv", 

682 "mli": "ml", 

683 "deu": "de", 

684 "gnb": "gw", 

685 "flk": "fk", 

686 "stp": "st", 

687 "can": "ca", 

688 "mlt": "mt", 

689 "rou": "ro", 

690 "sle": "sl", 

691 "aia": "ai", 

692 "eri": "er", 

693 "slb": "sb", 

694 "nzl": "nz", 

695 "and": "ad", 

696 "lbr": "lr", 

697 "jpn": "jp", 

698 "lby": "ly", 

699 "mys": "my", 

700 "pri": "pr", 

701 "myt": "yt", 

702 "prk": "kp", 

703 "tza": "tz", 

704 "prt": "pt", 

705 "spm": "pm", 

706 "ind": "in", 

707 "bhs": "bs", 

708 "bhr": "bh", 

709 "pry": "py", 

710 "sau": "sa", 

711 "cze": "cz", 

712 "qat": "qa", 

713 "ukr": "ua", 

714 "cym": "ky", 

715 "afg": "af", 

716 "bgr": "bg", 

717 "vgb": "vg", 

718 "nam": "na", 

719 "grd": "gd", 

720 "grc": "gr", 

721 "twn": "tw", 

722 "khm": "kh", 

723 "grl": "gl", 

724 "pak": "pk", 

725 "srb": "rs", 

726 "pan": "pa", 

727 "syc": "sc", 

728 "npl": "np", 

729 "kir": "ki", 

730 "mar": "ma", 

731 "lbn": "lb", 

732 "phl": "ph", 

733 "nic": "ni", 

734 "vnm": "vn", 

735 "iot": "io", 

736 "syr": "sy", 

737 "mac": "mo", 

738 "maf": "mf", 

739 "kaz": "kz", 

740 "cok": "ck", 

741 "pyf": "pf", 

742 "niu": "nu", 

743 "svn": "si", 

744 "egy": "eg", 

745 "svk": "sk", 

746 "dom": "do", 

747 "mrt": "mr", 

748 "esp": "es", 

749 "fsm": "fm", 

750 "kna": "kn", 

751 "gha": "gh", 

752 "cck": "cc", 

753 "chl": "cl", 

754 "ury": "uy", 

755 "tha": "th" 

756 } 

757 """A Map of ISO3 to ISO2 country codes""" 

758 

759 ISO2TOISO3 = {v: k for k, v in ISO3TOISO2.items()} # Build the invert map 

760 """A built Map of ISO2 to ISO3 country codes""" 

761 

762 subgroup_mappings = { 

763 "dach": ["de", "at", "ch"], 

764 "eu": ["at", "be", "bg", "hr", "cy", "cz", "dk", "ee", "fi", "fr", "de", "gr", "hu", "ie", "it", "lv", "lt", 

765 "lu", "mt", "nl", "pl", "pt", "ro", "sk", "si", "es", "se"], 

766 } 

767 

768 type = "select.country" 

769 ISO2 = 2 

770 ISO3 = 3 

771 

772 def __init__(self, *, descr="Country", codes=ISO2, values=None, **kwargs): 

773 assert codes in [self.ISO2, self.ISO3] 

774 # assert values is None 

775 

776 if values is None: 

777 values = dict( 

778 sorted((self.ISO2CODES if codes == self.ISO2 else self.ISO3CODES).items(), key=lambda i: i[1])) 

779 else: 

780 if isinstance(values, str): 

781 values = self.subgroup_mappings[values] 

782 if codes == self.ISO3: 

783 values = [self.ISO2TOISO3[i] for i in values] 

784 if isinstance(values, list): 

785 if codes == self.ISO2: 

786 values = {k: self.ISO2CODES[k] for k in values} 

787 elif codes == self.ISO3: 

788 values = {k: self.ISO3CODES[k] for k in values} 

789 if isinstance(values, dict): 

790 values = dict(sorted(values.items(), key=lambda i: i[1])) 

791 

792 super().__init__( 

793 values=values, 

794 descr=descr, 

795 **kwargs 

796 ) 

797 

798 self.codes = codes 

799 

800 def singleValueUnserialize(self, val): 

801 """ 

802 Unserializes a single value, converting ISO country codes between ISO 3166-1 alpha-2 and alpha-3 if necessary. 

803 

804 This method takes a country code string (either ISO 3166-1 alpha-2 or alpha-3) and checks if a conversion is 

805 needed based on the `self.codes` attribute. If a conversion is required, it attempts to perform the conversion 

806 using the `ISO3TOISO2` or `ISO2TOISO3` dictionaries. If the conversion is successful, the converted code is 

807 returned; otherwise, the original value is returned. 

808 

809 :param val: The value to be unserialized, typically a string representing an ISO country code. 

810 

811 :return: The unserialized value, either the original or converted ISO country code. 

812 """ 

813 if isinstance(val, str) and len(val) == 3 and self.codes == self.ISO2: 

814 # We got an ISO3 code from the db, but are using ISO2 

815 try: 

816 return self.ISO3TOISO2[val] 

817 except KeyError: 

818 pass 

819 elif isinstance(val, str) and len(val) == 2 and self.codes == self.ISO3: 

820 # We got ISO2 code, wanted ISO3 

821 try: 

822 return self.ISO2TOISO3[val] 

823 except KeyError: 

824 pass 

825 return val