diff --git a/frappe/geo/doctype/country/country.json b/frappe/geo/doctype/country/country.json index 920ef48fb1..088b9b5aa6 100644 --- a/frappe/geo/doctype/country/country.json +++ b/frappe/geo/doctype/country/country.json @@ -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": [ { diff --git a/frappe/geo/doctype/country/country.py b/frappe/geo/doctype/country/country.py index 01e217a3db..bec0e1e175 100644 --- a/frappe/geo/doctype/country/country.py +++ b/frappe/geo/doctype/country/country.py @@ -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(): diff --git a/frappe/geo/doctype/country/test_records.json b/frappe/geo/doctype/country/test_records.json deleted file mode 100644 index 5a7c8a5568..0000000000 --- a/frappe/geo/doctype/country/test_records.json +++ /dev/null @@ -1,6 +0,0 @@ -[ - { - "country_name": "_Test Country", - "doctype": "Country" - } -] \ No newline at end of file