Skip to content

Commit

Permalink
[IMP] l10n_fr_state: use with statement for file_open()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-via committed Nov 26, 2024
1 parent 8ab583f commit 0ab7c49
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions l10n_fr_state/pre_init_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 0ab7c49

Please sign in to comment.