diff --git a/CHANGELOG b/CHANGELOG
index ca0a0b0d60..9eed3a2ae3 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,4 @@
+* Add UN M49 regions
* Add flag symbol for country and subdivision
Version 6.4.0 - 2022-05-02
diff --git a/__init__.py b/__init__.py
index 80dff9d0ce..b229037c5f 100644
--- a/__init__.py
+++ b/__init__.py
@@ -8,6 +8,7 @@ def register():
# Prevent to import backend when importing scripts
from . import country
Pool.register(
+ country.Region,
country.Country,
country.Subdivision,
country.PostalCode,
diff --git a/country.py b/country.py
index a83cc8efc6..a53df52a01 100644
--- a/country.py
+++ b/country.py
@@ -1,13 +1,49 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond import backend
-from trytond.model import DeactivableMixin, ModelSQL, ModelView, fields
+from trytond.model import DeactivableMixin, ModelSQL, ModelView, fields, tree
from trytond.pool import Pool
from trytond.pyson import Eval
from trytond.tools import lstrip_wildcard
from trytond.transaction import Transaction
+class Region(tree(), ModelSQL, ModelView):
+ "Region"
+ __name__ = 'country.region'
+
+ name = fields.Char("Name", required=True, translate=True)
+ code_numeric = fields.Char(
+ "Numeric Code", size=3,
+ help="UN M49 region code.")
+ parent = fields.Many2One('country.region', "Parent")
+ subregions = fields.One2Many('country.region', 'parent', "Subregions")
+ countries = fields.One2Many(
+ 'country.country', 'region', "Countries",
+ add_remove=[
+ ('region', '=', None),
+ ])
+
+ @classmethod
+ def __setup__(cls):
+ super().__setup__()
+ cls._order.insert(0, ('name', 'ASC'))
+
+ @classmethod
+ def search_rec_name(cls, name, clause):
+ if clause[1].startswith('!') or clause[1].startswith('not '):
+ bool_op = 'AND'
+ else:
+ bool_op = 'OR'
+ code_value = clause[2]
+ if clause[1].endswith('like'):
+ code_value = lstrip_wildcard(clause[2])
+ return [bool_op,
+ ('name',) + tuple(clause[1:]),
+ ('code_numeric', clause[1], code_value) + tuple(clause[3:]),
+ ]
+
+
class Country(DeactivableMixin, ModelSQL, ModelView):
'Country'
__name__ = 'country.country'
@@ -20,6 +56,8 @@ class Country(DeactivableMixin, ModelSQL, ModelView):
code_numeric = fields.Char('Numeric Code', select=True,
help="The ISO numeric country code.")
flag = fields.Function(fields.Char("Flag"), 'on_change_with_flag')
+ region = fields.Many2One(
+ 'country.region', "Region", ondelete='SET NULL')
subdivisions = fields.One2Many('country.subdivision',
'country', 'Subdivisions')
diff --git a/country.xml b/country.xml
index 55d22192f7..f848c2ebde 100644
--- a/country.xml
+++ b/country.xml
@@ -8,6 +8,96 @@ this repository contains the full copyright notices and license terms. -->
icons/tryton-country.svg
+
+
+
+
+ country.region
+ form
+ region_form
+
+
+
+ country.region
+ tree
+
+ region_list
+
+
+
+ country.region
+ tree
+
+ subregions
+ region_tree
+
+
+
+ Areas
+ country.region
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Regions
+ country.region
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
country.country
form
@@ -33,15 +123,23 @@ this repository contains the full copyright notices and license terms. -->
-