diff --git a/l10n_fr_state/pre_init_hook.py b/l10n_fr_state/pre_init_hook.py index 04ac7d11c..fe545590b 100644 --- a/l10n_fr_state/pre_init_hook.py +++ b/l10n_fr_state/pre_init_hook.py @@ -26,35 +26,35 @@ def create_fr_state_xmlid(env): def generic_create_state_xmlid(env, module_name, data_file): """This method is also used by l10n_fr_state and l10n_fr_department_oversea""" - f = file_open(f"{module_name}/{data_file}", "rb") - xml_root = etree.parse(f) - data = {} # key = xmlid, value = {"code": "GP", "country_id": "base.gp"} - for record in xml_root.xpath("//record"): - xmlid = record.attrib["id"] - data[xmlid] = {} - for xfield in record.xpath("field"): - xfield_dict = xfield.attrib - data[xmlid][xfield_dict["name"]] = xfield_dict.get("ref") or xfield.text - logger.debug("generic_create_state_xmlid data=%s", data) - for xmlid, state_data in data.items(): - country_id = env.ref(state_data["country_id"]).id - state = env["res.country.state"].search( - [("code", "=", state_data["code"]), ("country_id", "=", country_id)], - limit=1, - ) - if state: - env["ir.model.data"].create( - { - "name": xmlid, - "res_id": state.id, - "module": module_name, - "model": "res.country.state", - } - ) - logger.info( - "XMLID %s.%s created for state %s code %s", - module_name, - xmlid, - state.name, - state.code, + with file_open(f"{module_name}/{data_file}", "rb") as f: + xml_root = etree.parse(f) + data = {} # key = xmlid, value = {"code": "GP", "country_id": "base.gp"} + for record in xml_root.xpath("//record"): + xmlid = record.attrib["id"] + data[xmlid] = {} + for xfield in record.xpath("field"): + xfield_dict = xfield.attrib + data[xmlid][xfield_dict["name"]] = xfield_dict.get("ref") or xfield.text + logger.debug("generic_create_state_xmlid data=%s", data) + for xmlid, state_data in data.items(): + country_id = env.ref(state_data["country_id"]).id + state = env["res.country.state"].search( + [("code", "=", state_data["code"]), ("country_id", "=", country_id)], + limit=1, ) + if state: + env["ir.model.data"].create( + { + "name": xmlid, + "res_id": state.id, + "module": module_name, + "model": "res.country.state", + } + ) + logger.info( + "XMLID %s.%s created for state %s code %s", + module_name, + xmlid, + state.name, + state.code, + )