Skip to content

Commit 8b19705

Browse files
committed
[IMP] util.update_record_from_xml
Make the default value of the `force_create` argument dynamic depending on the `fields` argument. closes #240 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 30534da commit 8b19705

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/util/records.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
)
3434
from .indirect_references import indirect_references
3535
from .inherit import direct_inherit_parents, for_each_inherit
36-
from .misc import chunks, parse_version, version_gte
36+
from .misc import Sentinel, chunks, parse_version, version_gte
3737
from .orm import env, flush
3838
from .pg import (
3939
PGRegexp,
@@ -1006,11 +1006,14 @@ def ensure_xmlid_match_record(cr, xmlid, model, values):
10061006
return new_res_id
10071007

10081008

1009+
AUTOMATIC = Sentinel("AUTOMATIC")
1010+
1011+
10091012
def update_record_from_xml(
10101013
cr,
10111014
xmlid,
10121015
reset_write_metadata=True,
1013-
force_create=True,
1016+
force_create=AUTOMATIC,
10141017
from_module=None,
10151018
reset_translations=(),
10161019
ensure_references=False,
@@ -1028,15 +1031,17 @@ def update_record_from_xml(
10281031
10291032
:param str xmlid: record xml_id, under the format `module.name`
10301033
:param bool reset_write_metadata: whether to update the `write_date` of the record
1031-
:param bool force_create: whether the record is created if it does not exist
1034+
:param bool force_create: whether the record is created if it does not exist.
1035+
`True` by default, unless `fields` is not None.
10321036
:param str from_module: name of the module from which to update the record, necessary
10331037
only when the specs are in a different module than the one in
10341038
the xml_id
10351039
:param set(str) reset_translations: field names whose translations get reset
10361040
:param bool ensure_references: whether referred records via `ref` XML attributes
10371041
should also be updated.
10381042
:param set(str) or None fields: optional list of fields to include in the XML declaration.
1039-
If set, all other fields will be ignored.
1043+
If set, all other fields will be ignored. When set, record
1044+
won't be created if missing.
10401045
10411046
.. warning::
10421047
This functions uses the ORM, therefore it can only be used after **all** models
@@ -1045,7 +1050,8 @@ def update_record_from_xml(
10451050
10461051
.. note::
10471052
The standard behavior of ORM is to create the record if it doesn't exist, including
1048-
its xml_id. That will happen on this function as well.
1053+
its xml_id. That will happen on this function as well, unless `force_create` is set
1054+
to `False`.
10491055
"""
10501056
__update_record_from_xml(
10511057
cr,
@@ -1076,6 +1082,9 @@ def __update_record_from_xml(
10761082
if "." not in xmlid:
10771083
raise ValueError("Please use fully qualified name <module>.<name>")
10781084

1085+
if force_create is AUTOMATIC:
1086+
force_create = fields is None # don't force record creation if we are filtering fields
1087+
10791088
module, _, name = xmlid.partition(".")
10801089

10811090
cr.execute(

0 commit comments

Comments
 (0)