From d5f334d5384bdf7532aea1ed55549bb5574547c6 Mon Sep 17 00:00:00 2001 From: Albert Cervera i Areny Date: Sun, 6 Jan 2013 22:18:13 +0100 Subject: [PATCH] Use a list of dictionaries in create(). issue2358 review266002 --- invoice.py | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/invoice.py b/invoice.py index ff38301443..f932edf744 100644 --- a/invoice.py +++ b/invoice.py @@ -87,23 +87,25 @@ class InvoiceLine: return res @classmethod - def create(cls, vals): + def create(cls, vlist): Selection = Pool().get('analytic_account.account.selection') - vals = vals.copy() - selection_vals = {} - for field in vals.keys(): - if field.startswith('analytic_account_'): - if vals[field]: - selection_vals.setdefault('accounts', []) - selection_vals['accounts'].append(('add', [vals[field]])) - del vals[field] - if vals.get('analytic_accounts'): - Selection.write([Selection(vals['analytic_accounts'])], - selection_vals) - elif vals.get('type', 'line') == 'line': - selection = Selection.create(selection_vals) - vals['analytic_accounts'] = selection.id - return super(InvoiceLine, cls).create(vals) + vlist = [x.copy() for x in vlist] + for vals in vlist: + selection_vals = {} + for field in vals.keys(): + if field.startswith('analytic_account_'): + if vals[field]: + selection_vals.setdefault('accounts', []) + selection_vals['accounts'].append(('add', + [vals[field]])) + del vals[field] + if vals.get('analytic_accounts'): + Selection.write([Selection(vals['analytic_accounts'])], + selection_vals) + elif vals.get('type', 'line') == 'line': + selection, = Selection.create([selection_vals]) + vals['analytic_accounts'] = selection.id + return super(InvoiceLine, cls).create(vlist) @classmethod def write(cls, lines, vals): @@ -123,7 +125,7 @@ class InvoiceLine: if not line.analytic_accounts: # Create missing selection with Transaction().set_user(0): - selection = Selection.create({}) + selection, = Selection.create([{}]) cls.write([line], { 'analytic_accounts': selection.id, }) @@ -184,6 +186,7 @@ class InvoiceLine: if self.analytic_accounts and self.analytic_accounts.accounts: for value in values: value['analytic_lines'] = [] + to_create = [] for account in self.analytic_accounts.accounts: vals = {} vals['name'] = self.description @@ -194,7 +197,9 @@ class InvoiceLine: vals['date'] = self.invoice.invoice_date vals['reference'] = self.invoice.reference vals['party'] = self.invoice.party.id - value['analytic_lines'].append(('create', vals)) + to_create.append(vals) + if to_create: + value['analytic_lines'] = [('create', to_create)] return values @@ -210,13 +215,13 @@ class Account(ModelSQL, ModelView): InvoiceLine._fields_view_get_cache.clear() @classmethod - def create(cls, vals): + def create(cls, vlist): InvoiceLine = Pool().get('account.invoice.line') - account = super(Account, cls).create(vals) + accounts = super(Account, cls).create(vlist) # Restart the cache on the fields_view_get method of # account.invoice.line InvoiceLine._fields_view_get_cache.clear() - return account + return accounts @classmethod def write(cls, accounts, vals):