From 430adf2e511341ec1f010bfe4a05e929c0f431a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 20 Jan 2022 23:04:05 +0100 Subject: [PATCH] Manage unknown subdivision type issue11128 review397091002 --- CHANGELOG | 1 + country.py | 8 +++++++- scripts/import_countries.py | 13 ++++++++++++- setup.py | 4 ++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d33477b124..3f58c7f029 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Manage unknown subdivision type * Add support for Python 3.10 * Remove support for Python 3.6 diff --git a/country.py b/country.py index 9848acbe4f..e34903f743 100644 --- a/country.py +++ b/country.py @@ -94,6 +94,7 @@ class Subdivision(DeactivableMixin, ModelSQL, ModelView): code = fields.Char('Code', required=True, select=True, help="The ISO code of the subdivision.") type = fields.Selection([ + (None, ""), ('administration', 'Administration'), ('administrative area', 'Administrative area'), ('administrative atoll', 'Administrative atoll'), @@ -198,7 +199,7 @@ class Subdivision(DeactivableMixin, ModelSQL, ModelView): ('unitary authority (england)', 'Unitary authority (england)'), ('unitary authority (wales)', 'Unitary authority (wales)'), ('zone', 'zone'), - ], 'Type', required=True) + ], "Type") parent = fields.Many2One('country.subdivision', 'Parent', domain=[ ('country', '=', Eval('country', -1)), @@ -220,10 +221,15 @@ class Subdivision(DeactivableMixin, ModelSQL, ModelView): super().__register__(module_name) + table_h = cls.__table_handler__(module_name) + # Migration from 5.2: remove country data cursor.execute(*data.delete(where=(data.module == 'country') & (data.model == cls.__name__))) + # Migration from 6.2: remove type required + table_h.not_null_action('type', action='remove') + @classmethod def search_rec_name(cls, name, clause): return ['OR', diff --git a/scripts/import_countries.py b/scripts/import_countries.py index 482597dc0d..98021b1b05 100755 --- a/scripts/import_countries.py +++ b/scripts/import_countries.py @@ -96,6 +96,8 @@ def update_subdivisions(countries, subdivisions): print("Update subdivisions", file=sys.stderr) Subdivision = Model.get('country.subdivision') + types = dict(Subdivision._fields['type']['selection']) + unknown_types = set() records = [] for subdivision in _progress(pycountry.subdivisions): code = subdivision.code @@ -105,7 +107,16 @@ def update_subdivisions(countries, subdivisions): else: record = Subdivision(code=code, country=countries[country_code]) record.name = _remove_forbidden_chars(subdivision.name) - record.type = subdivision.type.lower() + type_ = subdivision.type.lower() + if type_ in types: + record.type = subdivision.type.lower() + else: + record.type = None + if type_ not in unknown_types: + print( + "Unknown subdivision type: %s" % subdivision.type, + file=sys.stderr) + unknown_types.add(type_) records.append(record) Subdivision.save(records) diff --git a/setup.py b/setup.py index e7ee7cca55..bd7d30223e 100755 --- a/setup.py +++ b/setup.py @@ -67,7 +67,7 @@ for dep in info.get('depends', []): requires.append(get_require_version('trytond_%s' % dep)) requires.append(get_require_version('trytond')) -tests_require = [get_require_version('proteus'), 'pycountry<=20.7.3'] +tests_require = [get_require_version('proteus'), 'pycountry'] dependency_links = [] if minor_version % 2: dependency_links.append( @@ -143,7 +143,7 @@ setup(name=name, python_requires='>=3.7', install_requires=requires, extras_require={ - 'data': ['pycountry<=20.7.3', get_require_version('proteus')], + 'data': ['pycountry', get_require_version('proteus')], 'GeoNames': [get_require_version('proteus')], }, dependency_links=dependency_links,