Use a list of dictionaries in create().

issue2358
review266002
This commit is contained in:
Albert Cervera i Areny committed 2013-01-06 22:18:13 +01:00
1 parent eaac29fdc2
commit d5f334d538
1 file changed
+26 -21
+26 -21
View File
@@ -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):