feat(Country)!: formalise the code field (#28307)

This commit is contained in:
Raffael Meyer 2024-12-30 13:34:30 +01:00 committed by GitHub
parent c654559a4a
commit e5387a1272
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 12 deletions

View file

@ -8,10 +8,10 @@
"engine": "InnoDB",
"field_order": [
"country_name",
"code",
"date_format",
"time_format",
"time_zones",
"code"
"time_zones"
],
"fields": [
{
@ -44,19 +44,24 @@
"label": "Time Zones"
},
{
"description": "The country's ISO 3166 ALPHA-2 code.",
"fieldname": "code",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Code"
"label": "Code",
"length": 2,
"reqd": 1,
"unique": 1
}
],
"icon": "fa fa-globe",
"idx": 1,
"links": [],
"modified": "2024-11-27 16:56:04.850422",
"modified": "2024-12-29 16:56:04.850422",
"modified_by": "Administrator",
"module": "Geo",
"name": "Country",
"naming_rule": "By fieldname",
"owner": "Administrator",
"permissions": [
{

View file

@ -1,7 +1,10 @@
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import pycountry
import frappe
from frappe import _
from frappe.model.document import Document, bulk_insert
@ -14,7 +17,7 @@ class Country(Document):
if TYPE_CHECKING:
from frappe.types import DF
code: DF.Data | None
code: DF.Data
country_name: DF.Data
date_format: DF.Data | None
time_format: DF.Data | None
@ -22,7 +25,16 @@ class Country(Document):
# end: auto-generated types
# NOTE: During installation country docs are bulk inserted.
pass
def validate(self):
error_msg = _("{0} is not a valid ISO 3166 ALPHA-2 code.").format(self.code)
try:
country = pycountry.countries.lookup(self.code)
except LookupError:
frappe.throw(error_msg)
if country.alpha_2 != self.code.upper():
frappe.throw(error_msg)
def import_country_and_currency():

View file

@ -1,6 +0,0 @@
[
{
"country_name": "_Test Country",
"doctype": "Country"
}
]