From e136a7c266fffeb17d791f785cf5bfad46260a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 2 Sep 2013 17:34:46 +0200 Subject: [PATCH] Add new XML attribute "grouped" on data tag review1005002 --- CHANGELOG | 1 + doc/topics/modules/index.rst | 5 +-- trytond/convert.py | 62 +++++++++++++++++++++++++----------- trytond/tryton.rnc | 2 ++ 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d7ed712ed2..c9b127fcd4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add grouped attribute to data tag * Store selection tree state * Add order to Action Window * Add factor on number fields diff --git a/doc/topics/modules/index.rst b/doc/topics/modules/index.rst index a52cde30de..fe1a7e4443 100644 --- a/doc/topics/modules/index.rst +++ b/doc/topics/modules/index.rst @@ -110,8 +110,9 @@ Here is the list of the tags: * ``tryton``: The main tag of the xml * ``data``: Define a set of data inside the file. It can have the - attributes ``noupdate`` to prevent the framework to update the records - and ``skiptest`` to prevent import of data when running tests. + attributes ``noupdate`` to prevent the framework to update the records, + ``skiptest`` to prevent import of data when running tests and ``grouped`` + to create records at the end with a grouped call. * ``record``: Create a record of the model defined by the attribute ``model`` in the database. The ``id`` attribute can be used to refer to diff --git a/trytond/convert.py b/trytond/convert.py index 852a252571..b3eea7771e 100644 --- a/trytond/convert.py +++ b/trytond/convert.py @@ -8,6 +8,9 @@ import logging import traceback import sys import re +from itertools import izip +from collections import defaultdict + from trytond.version import VERSION from trytond.tools import safe_eval from trytond.transaction import Transaction @@ -384,6 +387,8 @@ class TrytondXmlHandler(sax.handler.ContentHandler): self.fs2db = Fs2bdAccessor(self.ModelData, pool) self.to_delete = self.populate_to_delete() self.noupdate = None + self.grouped = None + self.grouped_creations = defaultdict(dict) self.skip_data = False # Tag handlders are used to delegate the processing @@ -430,6 +435,7 @@ class TrytondXmlHandler(sax.handler.ContentHandler): elif name == "data": self.noupdate = bool(int(attributes.get("noupdate", '0'))) + self.grouped = bool(int(attributes.get('grouped', 0))) if self.pool.test and \ bool(int(attributes.get("skiptest", '0'))): self.skip_data = True @@ -452,6 +458,11 @@ class TrytondXmlHandler(sax.handler.ContentHandler): def endElement(self, name): + if name == 'data' and self.grouped: + for model, values in self.grouped_creations.iteritems(): + self.create_records(model, values.values(), values.keys()) + self.grouped_creations.clear() + # Closing tag found, if we are in a delegation the handler # know what to do: if self.taghandler and not self.skip_data: @@ -675,30 +686,43 @@ class TrytondXmlHandler(sax.handler.ContentHandler): self.fs2db.reset_browsercord(module, Model.__name__, [record.id]) else: - # this record is new, create it in the db: - with Transaction().set_context(module=module, language='en_US'): - record, = Model.create([values]) + if self.grouped: + self.grouped_creations[model][fs_id] = values + else: + self.create_records(model, [values], [fs_id]) - # re-read it: this ensure that we store the real value - # in the model_data table: - record = Model(record.id) + def create_records(self, model, vlist, fs_ids): + Model = self.pool.get(model) + + with Transaction().set_context(module=self.module, language='en_US'): + records = Model.create(vlist) + + mdata_values = [] + for record, values, fs_id in izip(records, vlist, fs_ids): for key in values: values[key] = self._clean_value(key, record) - mdata, = self.ModelData.create([{ - 'fs_id': fs_id, - 'model': model, - 'module': module, - 'db_id': record.id, - 'values': str(values), - 'noupdate': self.noupdate, - }]) + mdata_values.append({ + 'fs_id': fs_id, + 'model': model, + 'module': self.module, + 'db_id': record.id, + 'values': str(values), + 'noupdate': self.noupdate, + }) - # update fs2db: - self.fs2db.set(module, fs_id, { - "db_id": record.id, "model": model, - "id": mdata.id, "values": str(values)}) - self.fs2db.reset_browsercord(module, model, [record.id]) + models_data = self.ModelData.create(mdata_values) + + for record, values, fs_id, mdata in izip( + records, vlist, fs_ids, models_data): + self.fs2db.set(self.module, fs_id, { + 'db_id': record.id, + 'model': model, + 'id': mdata.id, + 'values': str(values), + }) + self.fs2db.reset_browsercord(self.module, model, + [r.id for r in records]) def post_import(pool, module, to_delete): diff --git a/trytond/tryton.rnc b/trytond/tryton.rnc index cb4cff5c88..5eef7ca1ca 100644 --- a/trytond/tryton.rnc +++ b/trytond/tryton.rnc @@ -5,6 +5,8 @@ attlist.tryton &= empty data = element data { attlist.data, (record | menuitem)* } attlist.data &= [ a:defaultValue = "0" ] attribute noupdate { "0" | "1" }? +attlist.data &= + [ a:defaultValue = "0" ] attribute grouped { "0" | "1" }? record = element record { attlist.record, field* } attlist.record &= attribute model { text } attlist.record &= attribute id { text }