Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/bones/selectcountry.py: 98%

46 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-27 10:50 +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 

9from viur.core.i18n import translate 

10 

11 

12class SelectCountryBone(SelectBone): 

13 """ 

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

15 

16 The SelectCountryBone is designed to provide a user-friendly way to select a country from a predefined list of 

17 countries. It inherits from the BaseBone class and extends it to support country-specific functionalities, 

18 such as displaying country names and handling country codes (e.g., ISO 3166-1 alpha-2 or alpha-3). 

19 

20 :param codes: The version of the ISO-Codes, either 2 or 3 

21 :param values: Optional, either a str representing a predefined group (currently "dach" and "eu"), a list of 

22 ISO-Codes (must match the Code-Length from `codes`), or a dict with matching iso-codes as keys and 

23 the full country-names as values. 

24 

25 .. seealso:: 

26 https://www.iso.org/obp/ui/#search/code/ 

27 """ 

28 

29 ISO3CODES = { 

30 "abw": "Aruba", 

31 "afg": "Afghanistan", 

32 "ago": "Angola", 

33 "aia": "Anguilla", 

34 "ala": "Åland Islands", 

35 "alb": "Albania", 

36 "and": "Andorra", 

37 "are": "United Arab Emirates", 

38 "arg": "Argentina", 

39 "arm": "Armenia", 

40 "asm": "American Samoa", 

41 "ata": "Antarctica", 

42 "atf": "French Southern Territories", 

43 "atg": "Antigua and Barbuda", 

44 "aus": "Australia", 

45 "aut": "Austria", 

46 "aze": "Azerbaijan", 

47 "bdi": "Burundi", 

48 "bel": "Belgium", 

49 "ben": "Benin", 

50 "bes": "Bonaire, Sint Eustatius and Saba", 

51 "bfa": "Burkina Faso", 

52 "bgd": "Bangladesh", 

53 "bgr": "Bulgaria", 

54 "bhr": "Bahrain", 

55 "bhs": "Bahamas", 

56 "bih": "Bosnia and Herzegovina", 

57 "blm": "Saint Barthélemy", 

58 "blr": "Belarus", 

59 "blz": "Belize", 

60 "bmu": "Bermuda", 

61 "bol": "Bolivia", 

62 "bra": "Brazil", 

63 "brb": "Barbados", 

64 "brn": "Brunei", 

65 "btn": "Bhutan", 

66 "bvt": "Bouvet Island", 

67 "bwa": "Botswana", 

68 "caf": "Central African Republic", 

69 "can": "Canada", 

70 "cck": "Cocos (Keeling) Islands", 

71 "che": "Switzerland", 

72 "chl": "Chile", 

73 "chn": "China", 

74 "civ": "Côte d'Ivoire", 

75 "cmr": "Cameroon", 

76 "cod": "Congo Democratic Republic", 

77 "cog": "Congo Republic", 

78 "cok": "Cook Islands", 

79 "col": "Colombia", 

80 "com": "Comoros", 

81 "cpv": "Cabo Verde", 

82 "cri": "Costa Rica", 

83 "cub": "Cuba", 

84 "cuw": "Curaçao", 

85 "cxr": "Christmas Island", 

86 "cym": "Cayman Islands", 

87 "cyp": "Cyprus", 

88 "cze": "Czechia", 

89 "deu": "Germany", 

90 "dji": "Djibouti", 

91 "dma": "Dominica", 

92 "dnk": "Denmark", 

93 "dom": "Dominican Republic", 

94 "dza": "Algeria", 

95 "ecu": "Ecuador", 

96 "egy": "Egypt", 

97 "eri": "Eritrea", 

98 "esh": "Western Sahara", 

99 "esp": "Spain", 

100 "est": "Estonia", 

101 "eth": "Ethiopia", 

102 "fin": "Finland", 

103 "fji": "Fiji", 

104 "flk": "Falkland Islands", 

105 "fra": "France", 

106 "fro": "Faroe Islands", 

107 "fsm": "Micronesia", 

108 "gab": "Gabon", 

109 "gbr": "United Kingdom", 

110 "geo": "Georgia", 

111 "ggy": "Guernsey", 

112 "gha": "Ghana", 

113 "gib": "Gibraltar", 

114 "gin": "Guinea", 

115 "glp": "Guadeloupe", 

116 "gmb": "Gambia", 

117 "gnb": "Guinea-Bissau", 

118 "gnq": "Equatorial Guinea", 

119 "grc": "Greece", 

120 "grd": "Grenada", 

121 "grl": "Greenland", 

122 "gtm": "Guatemala", 

123 "guf": "French Guiana", 

124 "gum": "Guam", 

125 "guy": "Guyana", 

126 "hkg": "Hong Kong", 

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

128 "hnd": "Honduras", 

129 "hrv": "Croatia", 

130 "hti": "Haiti", 

131 "hun": "Hungary", 

132 "idn": "Indonesia", 

133 "imn": "Isle of Man", 

134 "ind": "India", 

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

136 "irl": "Ireland", 

137 "irn": "Iran", 

138 "irq": "Iraq", 

139 "isl": "Iceland", 

140 "isr": "Israel", 

141 "ita": "Italy", 

142 "jam": "Jamaica", 

143 "jey": "Jersey", 

144 "jor": "Jordan", 

145 "jpn": "Japan", 

146 "kaz": "Kazakhstan", 

147 "ken": "Kenya", 

148 "kgz": "Kyrgyzstan", 

149 "khm": "Cambodia", 

150 "kir": "Kiribati", 

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

152 "kor": "Korea South", 

153 "kwt": "Kuwait", 

154 "lao": "Laos", 

155 "lbn": "Lebanon", 

156 "lbr": "Liberia", 

157 "lby": "Libya", 

158 "lca": "Saint Lucia", 

159 "lie": "Liechtenstein", 

160 "lka": "Sri Lanka", 

161 "lso": "Lesotho", 

162 "ltu": "Lithuania", 

163 "lux": "Luxembourg", 

164 "lva": "Latvia", 

165 "mac": "Macao", 

166 "maf": "Saint Martin", 

167 "mar": "Morocco", 

168 "mco": "Monaco", 

169 "mda": "Moldova", 

170 "mdg": "Madagascar", 

171 "mdv": "Maldives", 

172 "mex": "Mexico", 

173 "mhl": "Marshall Islands", 

174 "mkd": "North Macedonia", 

175 "mli": "Mali", 

176 "mlt": "Malta", 

177 "mmr": "Myanmar", 

178 "mne": "Montenegro", 

179 "mng": "Mongolia", 

180 "mnp": "Northern Mariana Islands", 

181 "moz": "Mozambique", 

182 "mrt": "Mauritania", 

183 "msr": "Montserrat", 

184 "mtq": "Martinique", 

185 "mus": "Mauritius", 

186 "mwi": "Malawi", 

187 "mys": "Malaysia", 

188 "myt": "Mayotte", 

189 "nam": "Namibia", 

190 "ncl": "New Caledonia", 

191 "ner": "Niger", 

192 "nfk": "Norfolk Island", 

193 "nga": "Nigeria", 

194 "nic": "Nicaragua", 

195 "niu": "Niue", 

196 "nld": "Netherlands (Kingdom of the)", 

197 "nor": "Norway", 

198 "npl": "Nepal", 

199 "nru": "Naoero", 

200 "nzl": "New Zealand", 

201 "omn": "Oman", 

202 "pak": "Pakistan", 

203 "pan": "Panama", 

204 "pcn": "Pitcairn", 

205 "per": "Peru", 

206 "phl": "Philippines", 

207 "plw": "Palau", 

208 "png": "Papua New Guinea", 

209 "pol": "Poland", 

210 "pri": "Puerto Rico", 

211 "prk": "Korea North", 

212 "prt": "Portugal", 

213 "pry": "Paraguay", 

214 "pse": "Palestine, State of", 

215 "pyf": "French Polynesia", 

216 "qat": "Qatar", 

217 "reu": "Réunion", 

218 "rou": "Romania", 

219 "rus": "Russia", 

220 "rwa": "Rwanda", 

221 "sau": "Saudi Arabia", 

222 "sdn": "Sudan", 

223 "sen": "Senegal", 

224 "sgp": "Singapore", 

225 "sgs": "South Georgia and the South Sandwich Islands", 

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

227 "sjm": "Svalbard and Jan Mayen", 

228 "slb": "Solomon Islands", 

229 "sle": "Sierra Leone", 

230 "slv": "El Salvador", 

231 "smr": "San Marino", 

232 "som": "Somalia", 

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

234 "srb": "Serbia", 

235 "ssd": "South Sudan", 

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

237 "sur": "Suriname", 

238 "svk": "Slovakia", 

239 "svn": "Slovenia", 

240 "swe": "Sweden", 

241 "swz": "Eswatini", 

242 "sxm": "Sint Maarten", 

243 "syc": "Seychelles", 

244 "syr": "Syria", 

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

246 "tcd": "Chad", 

247 "tgo": "Togo", 

248 "tha": "Thailand", 

249 "tjk": "Tajikistan", 

250 "tkl": "Tokelau", 

251 "tkm": "Turkmenistan", 

252 "tls": "Timor-Leste", 

253 "ton": "Tonga", 

254 "tto": "Trinidad and Tobago", 

255 "tun": "Tunisia", 

256 "tur": "Türkiye", 

257 "tuv": "Tuvalu", 

258 "twn": "Taiwan", 

259 "tza": "Tanzania", 

260 "uga": "Uganda", 

261 "ukr": "Ukraine", 

262 "umi": "United States Minor Outlying Islands", 

263 "ury": "Uruguay", 

264 "usa": "United States", 

265 "uzb": "Uzbekistan", 

266 "vat": "Holy See", 

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

268 "ven": "Venezuela", 

269 "vgb": "British Virgin Islands", 

270 "vir": "Virgin Islands (U.S.)", 

271 "vnm": "Vietnam", 

272 "vut": "Vanuatu", 

273 "wlf": "Wallis and Futuna", 

274 "wsm": "Samoa", 

275 "yem": "Yemen", 

276 "zaf": "South Africa", 

277 "zmb": "Zambia", 

278 "zwe": "Zimbabwe" 

279 } 

280 """ 

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

282 codes to represent countries. 

283 """ 

284 

285 ISO2CODES = { 

286 "ad": "Andorra", 

287 "ae": "United Arab Emirates", 

288 "af": "Afghanistan", 

289 "ag": "Antigua and Barbuda", 

290 "ai": "Anguilla", 

291 "al": "Albania", 

292 "am": "Armenia", 

293 "ao": "Angola", 

294 "aq": "Antarctica", 

295 "ar": "Argentina", 

296 "as": "American Samoa", 

297 "at": "Austria", 

298 "au": "Australia", 

299 "aw": "Aruba", 

300 "ax": "Åland Islands", 

301 "az": "Azerbaijan", 

302 "ba": "Bosnia and Herzegovina", 

303 "bb": "Barbados", 

304 "bd": "Bangladesh", 

305 "be": "Belgium", 

306 "bf": "Burkina Faso", 

307 "bg": "Bulgaria", 

308 "bh": "Bahrain", 

309 "bi": "Burundi", 

310 "bj": "Benin", 

311 "bl": "Saint Barthélemy", 

312 "bm": "Bermuda", 

313 "bn": "Brunei", 

314 "bo": "Bolivia", 

315 "bq": "Bonaire, Sint Eustatius and Saba", 

316 "br": "Brazil", 

317 "bs": "Bahamas", 

318 "bt": "Bhutan", 

319 "bv": "Bouvet Island", 

320 "bw": "Botswana", 

321 "by": "Belarus", 

322 "bz": "Belize", 

323 "ca": "Canada", 

324 "cc": "Cocos (Keeling) Islands", 

325 "cd": "Congo Democratic Republic", 

326 "cf": "Central African Republic", 

327 "cg": "Congo Republic", 

328 "ch": "Switzerland", 

329 "ci": "Côte d'Ivoire", 

330 "ck": "Cook Islands", 

331 "cl": "Chile", 

332 "cm": "Cameroon", 

333 "cn": "China", 

334 "co": "Colombia", 

335 "cr": "Costa Rica", 

336 "cu": "Cuba", 

337 "cv": "Cabo Verde", 

338 "cw": "Curaçao", 

339 "cx": "Christmas Island", 

340 "cy": "Cyprus", 

341 "cz": "Czechia", 

342 "de": "Germany", 

343 "dj": "Djibouti", 

344 "dk": "Denmark", 

345 "dm": "Dominica", 

346 "do": "Dominican Republic", 

347 "dz": "Algeria", 

348 "ec": "Ecuador", 

349 "ee": "Estonia", 

350 "eg": "Egypt", 

351 "eh": "Western Sahara", 

352 "er": "Eritrea", 

353 "es": "Spain", 

354 "et": "Ethiopia", 

355 "fi": "Finland", 

356 "fj": "Fiji", 

357 "fk": "Falkland Islands", 

358 "fm": "Micronesia", 

359 "fo": "Faroe Islands", 

360 "fr": "France", 

361 "ga": "Gabon", 

362 "gb": "United Kingdom", 

363 "gd": "Grenada", 

364 "ge": "Georgia", 

365 "gf": "French Guiana", 

366 "gg": "Guernsey", 

367 "gh": "Ghana", 

368 "gi": "Gibraltar", 

369 "gl": "Greenland", 

370 "gm": "Gambia", 

371 "gn": "Guinea", 

372 "gp": "Guadeloupe", 

373 "gq": "Equatorial Guinea", 

374 "gr": "Greece", 

375 "gs": "South Georgia and the South Sandwich Islands", 

376 "gt": "Guatemala", 

377 "gu": "Guam", 

378 "gw": "Guinea-Bissau", 

379 "gy": "Guyana", 

380 "hk": "Hong Kong", 

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

382 "hn": "Honduras", 

383 "hr": "Croatia", 

384 "ht": "Haiti", 

385 "hu": "Hungary", 

386 "id": "Indonesia", 

387 "ie": "Ireland", 

388 "il": "Israel", 

389 "im": "Isle of Man", 

390 "in": "India", 

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

392 "iq": "Iraq", 

393 "ir": "Iran", 

394 "is": "Iceland", 

395 "it": "Italy", 

396 "je": "Jersey", 

397 "jm": "Jamaica", 

398 "jo": "Jordan", 

399 "jp": "Japan", 

400 "ke": "Kenya", 

401 "kg": "Kyrgyzstan", 

402 "kh": "Cambodia", 

403 "ki": "Kiribati", 

404 "km": "Comoros", 

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

406 "kp": "Korea North", 

407 "kr": "Korea South", 

408 "kw": "Kuwait", 

409 "ky": "Cayman Islands", 

410 "kz": "Kazakhstan", 

411 "la": "Laos", 

412 "lb": "Lebanon", 

413 "lc": "Saint Lucia", 

414 "li": "Liechtenstein", 

415 "lk": "Sri Lanka", 

416 "lr": "Liberia", 

417 "ls": "Lesotho", 

418 "lt": "Lithuania", 

419 "lu": "Luxembourg", 

420 "lv": "Latvia", 

421 "ly": "Libya", 

422 "ma": "Morocco", 

423 "mc": "Monaco", 

424 "md": "Moldova", 

425 "me": "Montenegro", 

426 "mf": "Saint Martin", 

427 "mg": "Madagascar", 

428 "mh": "Marshall Islands", 

429 "mk": "North Macedonia", 

430 "ml": "Mali", 

431 "mm": "Myanmar", 

432 "mn": "Mongolia", 

433 "mo": "Macao", 

434 "mp": "Northern Mariana Islands", 

435 "mq": "Martinique", 

436 "mr": "Mauritania", 

437 "ms": "Montserrat", 

438 "mt": "Malta", 

439 "mu": "Mauritius", 

440 "mv": "Maldives", 

441 "mw": "Malawi", 

442 "mx": "Mexico", 

443 "my": "Malaysia", 

444 "mz": "Mozambique", 

445 "na": "Namibia", 

446 "nc": "New Caledonia", 

447 "ne": "Niger", 

448 "nf": "Norfolk Island", 

449 "ng": "Nigeria", 

450 "ni": "Nicaragua", 

451 "nl": "Netherlands (Kingdom of the)", 

452 "no": "Norway", 

453 "np": "Nepal", 

454 "nr": "Naoero", 

455 "nu": "Niue", 

456 "nz": "New Zealand", 

457 "om": "Oman", 

458 "pa": "Panama", 

459 "pe": "Peru", 

460 "pf": "French Polynesia", 

461 "pg": "Papua New Guinea", 

462 "ph": "Philippines", 

463 "pk": "Pakistan", 

464 "pl": "Poland", 

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

466 "pn": "Pitcairn", 

467 "pr": "Puerto Rico", 

468 "ps": "Palestine, State of", 

469 "pt": "Portugal", 

470 "pw": "Palau", 

471 "py": "Paraguay", 

472 "qa": "Qatar", 

473 "re": "Réunion", 

474 "ro": "Romania", 

475 "rs": "Serbia", 

476 "ru": "Russia", 

477 "rw": "Rwanda", 

478 "sa": "Saudi Arabia", 

479 "sb": "Solomon Islands", 

480 "sc": "Seychelles", 

481 "sd": "Sudan", 

482 "se": "Sweden", 

483 "sg": "Singapore", 

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

485 "si": "Slovenia", 

486 "sj": "Svalbard and Jan Mayen", 

487 "sk": "Slovakia", 

488 "sl": "Sierra Leone", 

489 "sm": "San Marino", 

490 "sn": "Senegal", 

491 "so": "Somalia", 

492 "sr": "Suriname", 

493 "ss": "South Sudan", 

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

495 "sv": "El Salvador", 

496 "sx": "Sint Maarten", 

497 "sy": "Syria", 

498 "sz": "Eswatini", 

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

500 "td": "Chad", 

501 "tf": "French Southern Territories", 

502 "tg": "Togo", 

503 "th": "Thailand", 

504 "tj": "Tajikistan", 

505 "tk": "Tokelau", 

506 "tl": "Timor-Leste", 

507 "tm": "Turkmenistan", 

508 "tn": "Tunisia", 

509 "to": "Tonga", 

510 "tr": "Türkiye", 

511 "tt": "Trinidad and Tobago", 

512 "tv": "Tuvalu", 

513 "tw": "Taiwan", 

514 "tz": "Tanzania", 

515 "ua": "Ukraine", 

516 "ug": "Uganda", 

517 "um": "United States Minor Outlying Islands", 

518 "us": "United States", 

519 "uy": "Uruguay", 

520 "uz": "Uzbekistan", 

521 "va": "Holy See", 

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

523 "ve": "Venezuela", 

524 "vg": "British Virgin Islands", 

525 "vi": "Virgin Islands (U.S.)", 

526 "vn": "Vietnam", 

527 "vu": "Vanuatu", 

528 "wf": "Wallis and Futuna", 

529 "ws": "Samoa", 

530 "ye": "Yemen", 

531 "yt": "Mayotte", 

532 "za": "South Africa", 

533 "zm": "Zambia", 

534 "zw": "Zimbabwe" 

535 } 

536 """ 

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

538 codes used to identify countries. 

539 """ 

540 ISO3TOISO2 = { # Convert iso3 to iso2 codes 

541 "abw": "aw", 

542 "afg": "af", 

543 "ago": "ao", 

544 "aia": "ai", 

545 "ala": "ax", 

546 "alb": "al", 

547 "and": "ad", 

548 "are": "ae", 

549 "arg": "ar", 

550 "arm": "am", 

551 "asm": "as", 

552 "ata": "aq", 

553 "atf": "tf", 

554 "atg": "ag", 

555 "aus": "au", 

556 "aut": "at", 

557 "aze": "az", 

558 "bdi": "bi", 

559 "bel": "be", 

560 "ben": "bj", 

561 "bes": "bq", 

562 "bfa": "bf", 

563 "bgd": "bd", 

564 "bgr": "bg", 

565 "bhr": "bh", 

566 "bhs": "bs", 

567 "bih": "ba", 

568 "blm": "bl", 

569 "blr": "by", 

570 "blz": "bz", 

571 "bmu": "bm", 

572 "bol": "bo", 

573 "bra": "br", 

574 "brb": "bb", 

575 "brn": "bn", 

576 "btn": "bt", 

577 "bvt": "bv", 

578 "bwa": "bw", 

579 "caf": "cf", 

580 "can": "ca", 

581 "cck": "cc", 

582 "che": "ch", 

583 "chl": "cl", 

584 "chn": "cn", 

585 "civ": "ci", 

586 "cmr": "cm", 

587 "cod": "cd", 

588 "cog": "cg", 

589 "cok": "ck", 

590 "col": "co", 

591 "com": "km", 

592 "cpv": "cv", 

593 "cri": "cr", 

594 "cub": "cu", 

595 "cuw": "cw", 

596 "cxr": "cx", 

597 "cym": "ky", 

598 "cyp": "cy", 

599 "cze": "cz", 

600 "deu": "de", 

601 "dji": "dj", 

602 "dma": "dm", 

603 "dnk": "dk", 

604 "dom": "do", 

605 "dza": "dz", 

606 "ecu": "ec", 

607 "egy": "eg", 

608 "eri": "er", 

609 "esh": "eh", 

610 "esp": "es", 

611 "est": "ee", 

612 "eth": "et", 

613 "fin": "fi", 

614 "fji": "fj", 

615 "flk": "fk", 

616 "fra": "fr", 

617 "fro": "fo", 

618 "fsm": "fm", 

619 "gab": "ga", 

620 "gbr": "gb", 

621 "geo": "ge", 

622 "ggy": "gg", 

623 "gha": "gh", 

624 "gib": "gi", 

625 "gin": "gn", 

626 "glp": "gp", 

627 "gmb": "gm", 

628 "gnb": "gw", 

629 "gnq": "gq", 

630 "grc": "gr", 

631 "grd": "gd", 

632 "grl": "gl", 

633 "gtm": "gt", 

634 "guf": "gf", 

635 "gum": "gu", 

636 "guy": "gy", 

637 "hkg": "hk", 

638 "hmd": "hm", 

639 "hnd": "hn", 

640 "hrv": "hr", 

641 "hti": "ht", 

642 "hun": "hu", 

643 "idn": "id", 

644 "imn": "im", 

645 "ind": "in", 

646 "iot": "io", 

647 "irl": "ie", 

648 "irn": "ir", 

649 "irq": "iq", 

650 "isl": "is", 

651 "isr": "il", 

652 "ita": "it", 

653 "jam": "jm", 

654 "jey": "je", 

655 "jor": "jo", 

656 "jpn": "jp", 

657 "kaz": "kz", 

658 "ken": "ke", 

659 "kgz": "kg", 

660 "khm": "kh", 

661 "kir": "ki", 

662 "kna": "kn", 

663 "kor": "kr", 

664 "kwt": "kw", 

665 "lao": "la", 

666 "lbn": "lb", 

667 "lbr": "lr", 

668 "lby": "ly", 

669 "lca": "lc", 

670 "lie": "li", 

671 "lka": "lk", 

672 "lso": "ls", 

673 "ltu": "lt", 

674 "lux": "lu", 

675 "lva": "lv", 

676 "mac": "mo", 

677 "maf": "mf", 

678 "mar": "ma", 

679 "mco": "mc", 

680 "mda": "md", 

681 "mdg": "mg", 

682 "mdv": "mv", 

683 "mex": "mx", 

684 "mhl": "mh", 

685 "mkd": "mk", 

686 "mli": "ml", 

687 "mlt": "mt", 

688 "mmr": "mm", 

689 "mne": "me", 

690 "mng": "mn", 

691 "mnp": "mp", 

692 "moz": "mz", 

693 "mrt": "mr", 

694 "msr": "ms", 

695 "mtq": "mq", 

696 "mus": "mu", 

697 "mwi": "mw", 

698 "mys": "my", 

699 "myt": "yt", 

700 "nam": "na", 

701 "ncl": "nc", 

702 "ner": "ne", 

703 "nfk": "nf", 

704 "nga": "ng", 

705 "nic": "ni", 

706 "niu": "nu", 

707 "nld": "nl", 

708 "nor": "no", 

709 "npl": "np", 

710 "nru": "nr", 

711 "nzl": "nz", 

712 "omn": "om", 

713 "pak": "pk", 

714 "pan": "pa", 

715 "pcn": "pn", 

716 "per": "pe", 

717 "phl": "ph", 

718 "plw": "pw", 

719 "png": "pg", 

720 "pol": "pl", 

721 "pri": "pr", 

722 "prk": "kp", 

723 "prt": "pt", 

724 "pry": "py", 

725 "pse": "ps", 

726 "pyf": "pf", 

727 "qat": "qa", 

728 "reu": "re", 

729 "rou": "ro", 

730 "rus": "ru", 

731 "rwa": "rw", 

732 "sau": "sa", 

733 "sdn": "sd", 

734 "sen": "sn", 

735 "sgp": "sg", 

736 "sgs": "gs", 

737 "shn": "sh", 

738 "sjm": "sj", 

739 "slb": "sb", 

740 "sle": "sl", 

741 "slv": "sv", 

742 "smr": "sm", 

743 "som": "so", 

744 "spm": "pm", 

745 "srb": "rs", 

746 "ssd": "ss", 

747 "stp": "st", 

748 "sur": "sr", 

749 "svk": "sk", 

750 "svn": "si", 

751 "swe": "se", 

752 "swz": "sz", 

753 "sxm": "sx", 

754 "syc": "sc", 

755 "syr": "sy", 

756 "tca": "tc", 

757 "tcd": "td", 

758 "tgo": "tg", 

759 "tha": "th", 

760 "tjk": "tj", 

761 "tkl": "tk", 

762 "tkm": "tm", 

763 "tls": "tl", 

764 "ton": "to", 

765 "tto": "tt", 

766 "tun": "tn", 

767 "tur": "tr", 

768 "tuv": "tv", 

769 "twn": "tw", 

770 "tza": "tz", 

771 "uga": "ug", 

772 "ukr": "ua", 

773 "umi": "um", 

774 "ury": "uy", 

775 "usa": "us", 

776 "uzb": "uz", 

777 "vat": "va", 

778 "vct": "vc", 

779 "ven": "ve", 

780 "vgb": "vg", 

781 "vir": "vi", 

782 "vnm": "vn", 

783 "vut": "vu", 

784 "wlf": "wf", 

785 "wsm": "ws", 

786 "yem": "ye", 

787 "zaf": "za", 

788 "zmb": "zm", 

789 "zwe": "zw", 

790 } 

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

792 

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

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

795 

796 subgroup_mappings = { 

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

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

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

800 } 

801 

802 type = "select.country" 

803 ISO2 = 2 

804 ISO3 = 3 

805 

806 def __init__(self, *, descr="Country", codes=ISO2, values=None, sort=SelectBone.sort_by_label, **kwargs): 

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

808 # assert values is None 

809 

810 if values is None: 

811 values = dict( 

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

813 else: 

814 if isinstance(values, str): 

815 values = self.subgroup_mappings[values] 

816 if codes == self.ISO3: 

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

818 if isinstance(values, list): 

819 if codes == self.ISO2: 

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

821 elif codes == self.ISO3: 821 ↛ 823line 821 didn't jump to line 823 because the condition on line 821 was always true

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

823 if isinstance(values, dict): 

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

825 

826 # The names are translated in viur.core.languages, keyed by their lower-case English name 

827 if isinstance(values, dict): 

828 values = { 

829 code: translate(name.lower(), name) if isinstance(name, str) else name 

830 for code, name in values.items() 

831 } 

832 

833 super().__init__( 

834 values=values, 

835 descr=descr, 

836 sort=sort, 

837 **kwargs 

838 ) 

839 

840 self.codes = codes 

841 

842 def singleValueUnserialize(self, val): 

843 """ 

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

845 

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

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

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

849 returned; otherwise, the original value is returned. 

850 

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

852 

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

854 """ 

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

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

857 try: 

858 return self.ISO3TOISO2[val] 

859 except KeyError: 

860 pass 

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

862 # We got ISO2 code, wanted ISO3 

863 try: 

864 return self.ISO2TOISO3[val] 

865 except KeyError: 

866 pass 

867 return val