From b83c4fff3453210808473fbca1c41aabdd8f2265 Mon Sep 17 00:00:00 2001 From: william Date: Thu, 3 Mar 2022 12:03:33 +0100 Subject: [PATCH] [IMP] convert: autofill sequence fields Motivations: * `sequence` is not super intuitive for new devs * It makes "useless" noise in data files * It is redundant when what we want to see in the file is the same as what we want to see on the user screen. Do you understand anything related to sequences in here? https://github.com/odoo/odoo/blob/15.0/addons/l10n_lu/data/account_tax_report_line.xml Explicit is not always better than implicit. * It is not implicit like using `id` it is natural and visual and saves a lot of chore for adding and maintaining sequences. Implementation * It is optional, with the option set on root xml tags Part-of: odoo/odoo#85750 --- odoo/import_xml.rng | 1 + odoo/tools/convert.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/odoo/import_xml.rng b/odoo/import_xml.rng index 6c1095bdd28..034a3bc89f0 100644 --- a/odoo/import_xml.rng +++ b/odoo/import_xml.rng @@ -314,6 +314,7 @@ data + diff --git a/odoo/tools/convert.py b/odoo/tools/convert.py index 91c4a10e4f2..2df1c5a5ccf 100644 --- a/odoo/tools/convert.py +++ b/odoo/tools/convert.py @@ -229,7 +229,7 @@ class xml_import(object): **safe_eval(context, { 'ref': self.id_get, **(eval_context or {}) - }) + }), } ) return self.env @@ -446,6 +446,10 @@ form: module.record_id""" % (xml_id,) res[f_name] = f_val if extra_vals: res.update(extra_vals) + if 'sequence' not in res and 'sequence' in model._fields: + sequence = self.next_sequence() + if sequence: + res['sequence'] = sequence data = dict(xml_id=xid, values=res, noupdate=self.noupdate) record = model._load_records([data], self.mode == 'update') @@ -546,6 +550,7 @@ form: module.record_id""" % (xml_id,) self.envs.append(self.get_env(el)) self._noupdate.append(nodeattr2bool(el, 'noupdate', self.noupdate)) + self._sequences.append(0 if nodeattr2bool(el, 'auto_sequence', False) else None) try: f(rec) except ParseError: @@ -568,6 +573,7 @@ form: module.record_id""" % (xml_id,) finally: self._noupdate.pop() self.envs.pop() + self._sequences.pop() @property def env(self): @@ -577,12 +583,19 @@ form: module.record_id""" % (xml_id,) def noupdate(self): return self._noupdate[-1] + def next_sequence(self): + value = self._sequences[-1] + if value is not None: + value = self._sequences[-1] = value + 10 + return value + def __init__(self, env, module, idref, mode, noupdate=False, xml_filename=None): self.mode = mode self.module = module self.envs = [env(context=dict(env.context, lang=None))] self.idref = {} if idref is None else idref self._noupdate = [noupdate] + self._sequences = [] self.xml_filename = xml_filename self._tags = { 'record': self._tag_record,