Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/util/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ def force_noupdate(cr, xmlid, noupdate=True, warn=False):
return cr.rowcount


def ensure_xmlid_match_record(cr, xmlid, model, values):
def ensure_xmlid_match_record(cr, xmlid, model, values, noupdate=AUTOMATIC):
"""
Ensure an xml_id references a record with specific values.

Expand All @@ -1024,6 +1024,9 @@ def ensure_xmlid_match_record(cr, xmlid, model, values):
values = {"id": 123}
values = {"name": "INV/2024/0001", "company_id": 1}

:param bool noupdate: value to set on the `noupdate` flag of the xml_id. By default, the
flag is set to `True` when the xml_id is created and left untouched
when it already exists.
:return: the ID of the matched record, `None` if no record found
:rtype: int or None

Expand All @@ -1049,6 +1052,9 @@ def ensure_xmlid_match_record(cr, xmlid, model, values):
)
data_id, res_id = cr.fetchone() or (None, None)

if data_id and noupdate is not AUTOMATIC:
force_noupdate(cr, xmlid, noupdate)

table = table_of_model(cr, model)

# search for existing records matching values
Expand Down Expand Up @@ -1093,7 +1099,7 @@ def ensure_xmlid_match_record(cr, xmlid, model, values):
INSERT INTO ir_model_data(module, name, model, res_id, noupdate)
VALUES (%s, %s, %s, %s, %s)
""",
[module, name, model, new_res_id, True],
[module, name, model, new_res_id, True if noupdate is AUTOMATIC else noupdate],
)

return new_res_id
Expand Down