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 65a53142c5
commit 87222daf32
8 files changed
+214 -208

No files matched your search

+14 -14
View File
@@ -123,7 +123,7 @@ class TypeTemplate(ModelSQL, ModelView):
vals['company'] = company_id
vals['parent'] = parent_id
new_type = Type.create(vals)
new_type, = Type.create([vals])
prev_lang = self._context.get('language') or Config.get_language()
prev_data = {}
@@ -448,7 +448,7 @@ class AccountTemplate(ModelSQL, ModelView):
vals['type'] = (template2type.get(self.type.id) if self.type
else None)
new_account = Account.create(vals)
new_account, = Account.create([vals])
prev_lang = self._context.get('language') or Config.get_language()
prev_data = {}
@@ -1738,12 +1738,12 @@ class CreateChart(Wizard):
with Transaction().set_user(0):
Property.delete(properties)
if self.properties.account_receivable:
Property.create({
'field': account_receivable_field.id,
'value': 'account.account,' + \
str(self.properties.account_receivable.id),
'company': self.properties.company.id,
})
Property.create([{
'field': account_receivable_field.id,
'value': 'account.account,' + \
str(self.properties.account_receivable.id),
'company': self.properties.company.id,
}])
account_payable_field, = ModelField.search([
('model.model', '=', 'party.party'),
@@ -1757,12 +1757,12 @@ class CreateChart(Wizard):
with Transaction().set_user(0):
Property.delete(properties)
if self.properties.account_payable:
Property.create({
'field': account_payable_field.id,
'value': 'account.account,' + \
str(self.properties.account_payable.id),
'company': self.properties.company.id,
})
Property.create([{
'field': account_payable_field.id,
'value': 'account.account,' + \
str(self.properties.account_payable.id),
'company': self.properties.company.id,
}])
return 'end'
+5 -5
View File
@@ -62,8 +62,8 @@ class Configuration(ModelSingleton, ModelSQL, ModelView):
with Transaction().set_user(0):
Property.delete(properties)
if value:
Property.create({
'field': account_field.id,
'value': 'account.account,%s' % value,
'company': company_id,
})
Property.create([{
'field': account_field.id,
'value': 'account.account,%s' % value,
'company': company_id,
}])
+10 -7
View File
@@ -150,6 +150,7 @@ class FiscalYear(ModelSQL, ModelView):
Create periods for the fiscal years with month interval
'''
Period = Pool().get('account.period')
to_create = []
for fiscalyear in fiscalyears:
period_start_date = fiscalyear.start_date
while period_start_date < fiscalyear.end_date:
@@ -161,7 +162,7 @@ class FiscalYear(ModelSQL, ModelView):
name = datetime_strftime(period_start_date, '%Y-%m')
if name != datetime_strftime(period_end_date, '%Y-%m'):
name += ' - ' + datetime_strftime(period_end_date, '%Y-%m')
Period.create({
to_create.append({
'name': name,
'start_date': period_start_date,
'end_date': period_end_date,
@@ -170,6 +171,8 @@ class FiscalYear(ModelSQL, ModelView):
'type': 'standard',
})
period_start_date = period_end_date + relativedelta(days=1)
if to_create:
Period.create(to_create)
@classmethod
@ModelView.button
@@ -217,12 +220,12 @@ class FiscalYear(ModelSQL, ModelView):
self.raise_user_error('account_balance_not_zero',
error_args=(account.rec_name,))
else:
Deferral.create({
'account': account.id,
'fiscalyear': self.id,
'debit': account.debit,
'credit': account.credit,
})
Deferral.create([{
'account': account.id,
'fiscalyear': self.id,
'debit': account.debit,
'credit': account.credit,
}])
@classmethod
@ModelView.button
+7 -6
View File
@@ -234,13 +234,14 @@ class JournalPeriod(ModelSQL, ModelView):
cls.raise_user_error('modify_del_journal_period')
@classmethod
def create(cls, vals):
def create(cls, vlist):
Period = Pool().get('account.period')
if vals.get('period'):
period = Period(vals['period'])
if period.state == 'close':
cls.raise_user_error('create_journal_period')
return super(JournalPeriod, cls).create(vals)
for vals in vlist:
if vals.get('period'):
period = Period(vals['period'])
if period.state == 'close':
cls.raise_user_error('create_journal_period')
return super(JournalPeriod, cls).create(vlist)
@classmethod
def write(cls, periods, vals):
+100 -95
View File
@@ -219,32 +219,33 @@ class Move(ModelSQL, ModelView):
cls.validate(moves)
@classmethod
def create(cls, vals):
def create(cls, vlist):
pool = Pool()
MoveLine = pool.get('account.move.line')
Sequence = pool.get('ir.sequence')
Journal = pool.get('account.journal')
if not vals.get('number'):
journal_id = (vals.get('journal')
or Transaction().context.get('journal'))
if journal_id:
vals = vals.copy()
journal = Journal(journal_id)
vals['number'] = Sequence.get_id(journal.sequence.id)
vlist = [x.copy() for x in vlist]
for vals in vlist:
if not vals.get('number'):
journal_id = (vals.get('journal')
or Transaction().context.get('journal'))
if journal_id:
journal = Journal(journal_id)
vals['number'] = Sequence.get_id(journal.sequence.id)
move = super(Move, cls).create(vals)
if move.journal.centralised:
line = MoveLine.create({
'account': move.journal.credit_account.id,
'move': move.id,
})
cls.write([move], {
'centralised_line': line.id,
})
if 'lines' in vals:
cls.validate([move])
return move
moves = super(Move, cls).create(vlist)
for move in moves:
if move.journal.centralised:
line, = MoveLine.create([{
'account': move.journal.credit_account.id,
'move': move.id,
}])
cls.write([move], {
'centralised_line': line.id,
})
cls.validate(moves)
return moves
@classmethod
def delete(cls, moves):
@@ -316,12 +317,12 @@ class Move(ModelSQL, ModelView):
credit = - centralised_amount
account_id = move.journal.credit_account.id
if not move.centralised_line:
centralised_line = MoveLine.create({
'debit': debit,
'credit': credit,
'account': account_id,
'move': move.id,
})
centralised_line, = MoveLine.create([{
'debit': debit,
'credit': credit,
'account': account_id,
'move': move.id,
}])
cls.write([move], {
'centralised_line': centralised_line.id,
})
@@ -401,14 +402,15 @@ class Reconciliation(ModelSQL, ModelView):
})
@classmethod
def create(cls, vals):
def create(cls, vlist):
Sequence = Pool().get('ir.sequence')
if 'name' not in vals:
vals = vals.copy()
vals['name'] = Sequence.get('account.move.reconciliation')
vlist = [x.copy() for x in vlist]
for vals in vlist:
if 'name' not in vals:
vals['name'] = Sequence.get('account.move.reconciliation')
return super(Reconciliation, cls).create(vals)
return super(Reconciliation, cls).create(vlist)
@classmethod
def write(cls, moves, vals):
@@ -1117,11 +1119,11 @@ class Line(ModelSQL, ModelView):
if journal_period.state == 'close':
cls.raise_user_error('add_modify_closed_journal_period')
else:
JournalPeriod.create({
'name': journal.name + ' - ' + period.name,
'journal': journal.id,
'period': period.id,
})
JournalPeriod.create([{
'name': journal.name + ' - ' + period.name,
'journal': journal.id,
'period': period.id,
}])
@classmethod
def check_modify(cls, lines):
@@ -1165,40 +1167,44 @@ class Line(ModelSQL, ModelView):
Move.validate(moves)
@classmethod
def create(cls, vals):
def create(cls, vlist):
pool = Pool()
Journal = pool.get('account.journal')
Move = pool.get('account.move')
vals = vals.copy()
if not vals.get('move'):
journal_id = (vals.get('journal')
or Transaction().context.get('journal'))
if not journal_id:
cls.raise_user_error('no_journal')
journal = Journal(journal_id)
if journal.centralised:
moves = Move.search([
('period', '=', vals.get('period')
or Transaction().context.get('period')),
('journal', '=', journal_id),
('state', '!=', 'posted'),
], limit=1)
if moves:
vals['move'] = moves[0].id
vlist = [x.copy() for x in vlist]
for vals in vlist:
if not vals.get('move'):
vals['move'] = Move.create({
'period': (vals.get('period')
or Transaction().context.get('period')),
'journal': journal_id,
'date': vals.get('date'),
}).id
else:
# prevent computation of default date
vals.setdefault('date', None)
line = super(Line, cls).create(vals)
cls.check_journal_period_modify(line.period, line.journal)
Move.validate([line.move])
return line
journal_id = (vals.get('journal')
or Transaction().context.get('journal'))
if not journal_id:
cls.raise_user_error('no_journal')
journal = Journal(journal_id)
if journal.centralised:
moves = Move.search([
('period', '=', vals.get('period')
or Transaction().context.get('period')),
('journal', '=', journal_id),
('state', '!=', 'posted'),
], limit=1)
if moves:
vals['move'] = moves[0].id
if not vals.get('move'):
vals['move'] = Move.create([{
'period': (vals.get('period')
or Transaction().context.get('period')),
'journal': journal_id,
'date': vals.get('date'),
}])[0].id
else:
# prevent computation of default date
vals.setdefault('date', None)
lines = super(Line, cls).create(vlist)
period_and_journals = set((line.period, line.journal)
for line in lines)
for period, journal in period_and_journals:
cls.check_journal_period_modify(period, journal)
Move.validate(list(set(line.move for line in lines)))
return lines
@classmethod
def copy(cls, lines, default=None):
@@ -1289,27 +1295,26 @@ class Line(ModelSQL, ModelView):
account = line.account
amount = account.currency.round(amount)
period_id = Period.find(account.company.id, date=date)
move = Move.create({
'journal': journal.id,
'period': period_id,
'date': date,
'lines': [
('create', {
'account': account.id,
'debit': amount < Decimal('0.0') and - amount \
or Decimal('0.0'),
'credit': amount > Decimal('0.0') and amount \
or Decimal('0.0'),
}),
('create', {
'account': account.id,
'debit': amount > Decimal('0.0') and amount \
or Decimal('0.0'),
'credit': amount < Decimal('0.0') and - amount \
or Decimal('0.0'),
}),
],
})
move, = Move.create([{
'journal': journal.id,
'period': period_id,
'date': date,
'lines': [
('create', [{
'account': account.id,
'debit': (amount < Decimal('0.0')
and - amount or Decimal('0.0')),
'credit': (amount > Decimal('0.0')
and amount or Decimal('0.0')),
}, {
'account': account.id,
'debit': (amount > Decimal('0.0')
and amount or Decimal('0.0')),
'credit': (amount < Decimal('0.0')
and - amount or Decimal('0.0')),
}]),
],
}])
lines += cls.search([
('move', '=', move.id),
('account', '=', account.id),
@@ -1318,9 +1323,9 @@ class Line(ModelSQL, ModelView):
('credit', '=', amount > Decimal('0.0') and amount \
or Decimal('0.0')),
], limit=1)
return Reconciliation.create({
'lines': [('add', [x.id for x in lines])],
})
return Reconciliation.create([{
'lines': [('add', [x.id for x in lines])],
}])[0]
class Move2:
@@ -1394,11 +1399,11 @@ class OpenJournal(Wizard):
('period', '=', period.id),
], limit=1)
if not journal_periods:
journal_period = JournalPeriod.create({
'name': journal.name + ' - ' + period.name,
'journal': journal.id,
'period': period.id,
})
journal_period, = JournalPeriod.create([{
'name': journal.name + ' - ' + period.name,
'journal': journal.id,
'period': period.id,
}])
else:
journal_period, = journal_periods
+11 -9
View File
@@ -188,16 +188,18 @@ class Period(ModelSQL, ModelView):
order=order, count=count, query_string=query_string)
@classmethod
def create(cls, vals):
def create(cls, vlist):
FiscalYear = Pool().get('account.fiscalyear')
vals = vals.copy()
if vals.get('fiscalyear'):
fiscalyear = FiscalYear(vals['fiscalyear'])
if fiscalyear.state == 'close':
cls.raise_user_error('create_period_closed_fiscalyear')
if not vals.get('post_move_sequence'):
vals['post_move_sequence'] = fiscalyear.post_move_sequence.id
return super(Period, cls).create(vals)
vlist = [x.copy() for x in vlist]
for vals in vlist:
if vals.get('fiscalyear'):
fiscalyear = FiscalYear(vals['fiscalyear'])
if fiscalyear.state == 'close':
cls.raise_user_error('create_period_closed_fiscalyear')
if not vals.get('post_move_sequence'):
vals['post_move_sequence'] = (
fiscalyear.post_move_sequence.id)
return super(Period, cls).create(vlist)
@classmethod
def write(cls, periods, vals):
+4 -4
View File
@@ -105,7 +105,7 @@ class TaxCodeTemplate(ModelSQL, ModelView):
vals['company'] = company_id
vals['parent'] = parent_id
new_tax_code = TaxCode.create(vals)
new_tax_code, = TaxCode.create([vals])
prev_lang = self._context.get('language') or Config.get_language()
prev_data = {}
@@ -499,7 +499,7 @@ class TaxTemplate(ModelSQL, ModelView):
else:
vals['credit_note_tax_code'] = None
new_tax = Tax.create(vals)
new_tax, = Tax.create([vals])
prev_lang = self._context.get('language') or Config.get_language()
prev_data = {}
@@ -919,7 +919,7 @@ class TaxRuleTemplate(ModelSQL, ModelView):
if self.id not in template2rule:
vals = self._get_tax_rule_value()
vals['company'] = company_id
new_rule = Rule.create(vals)
new_rule, = Rule.create([vals])
prev_lang = self._context.get('language') or Config.get_language()
prev_data = {}
@@ -1115,7 +1115,7 @@ class TaxRuleLineTemplate(ModelSQL, ModelView):
vals['tax'] = template2tax[self.tax.id]
else:
vals['tax'] = None
new_rule_line = RuleLine.create(vals)
new_rule_line, = RuleLine.create([vals])
template2rule_line[self.id] = new_rule_line.id
return template2rule_line[self.id]
+63 -68
View File
@@ -90,18 +90,18 @@ class AccountTestCase(unittest.TestCase):
context=CONTEXT) as transaction:
today = datetime.date.today()
company, = self.company.search([('name', '=', 'B2CK')])
sequence = self.sequence.create({
'name': '%s' % today.year,
'code': 'account.move',
'company': company.id,
})
fiscalyear = self.fiscalyear.create({
'name': '%s' % today.year,
'start_date': today.replace(month=1, day=1),
'end_date': today.replace(month=12, day=31),
'company': company.id,
'post_move_sequence': sequence.id,
})
sequence, = self.sequence.create([{
'name': '%s' % today.year,
'code': 'account.move',
'company': company.id,
}])
fiscalyear, = self.fiscalyear.create([{
'name': '%s' % today.year,
'start_date': today.replace(month=1, day=1),
'end_date': today.replace(month=12, day=31),
'company': company.id,
'post_move_sequence': sequence.id,
}])
self.fiscalyear.create_period([fiscalyear])
self.assertEqual(len(fiscalyear.periods), 12)
transaction.cursor.commit()
@@ -133,20 +133,19 @@ class AccountTestCase(unittest.TestCase):
('kind', '=', 'payable'),
])
# Create some moves
values = [
vlist = [
{
'period': period.id,
'journal': journal_revenue.id,
'date': period.start_date,
'lines': [
('create', {
'account': revenue.id,
'credit': Decimal(100),
}),
('create', {
'account': receivable.id,
'debit': Decimal(100),
}),
('create', [{
'account': revenue.id,
'credit': Decimal(100),
}, {
'account': receivable.id,
'debit': Decimal(100),
}]),
],
},
{
@@ -154,19 +153,17 @@ class AccountTestCase(unittest.TestCase):
'journal': journal_expense.id,
'date': period.start_date,
'lines': [
('create', {
'account': expense.id,
'debit': Decimal(30),
}),
('create', {
'account': payable.id,
'credit': Decimal(30),
}),
('create', [{
'account': expense.id,
'debit': Decimal(30),
}, {
'account': payable.id,
'credit': Decimal(30),
}]),
],
},
]
for value in values:
self.move.create(value)
self.move.create(vlist)
# Test debit/credit
self.assertEqual((revenue.debit, revenue.credit),
@@ -174,11 +171,11 @@ class AccountTestCase(unittest.TestCase):
self.assertEqual(revenue.balance, Decimal(-100))
# Use next fiscalyear
next_sequence = self.sequence.create({
'name': 'Next Year',
'code': 'account.move',
'company': fiscalyear.company.id,
})
next_sequence, = self.sequence.create([{
'name': 'Next Year',
'code': 'account.move',
'company': fiscalyear.company.id,
}])
next_fiscalyear, = self.fiscalyear.copy([fiscalyear],
default={
'start_date': fiscalyear.end_date + datetime.timedelta(1),
@@ -207,41 +204,39 @@ class AccountTestCase(unittest.TestCase):
journal_sequence, = self.sequence.search([
('code', '=', 'account.journal'),
])
journal_closing = self.journal.create({
'name': 'Closing',
'code': 'CLO',
'type': 'situation',
'sequence': journal_sequence.id,
})
journal_closing, = self.journal.create([{
'name': 'Closing',
'code': 'CLO',
'type': 'situation',
'sequence': journal_sequence.id,
}])
type_equity, = self.account_type.search([
('name', '=', 'Equity'),
])
account_pl = self.account.create({
'name': 'P&L',
'type': type_equity.id,
'deferral': True,
'parent': revenue.parent.id,
'kind': 'other',
})
self.move.create({
'period': fiscalyear.periods[-1].id,
'journal': journal_closing.id,
'date': fiscalyear.periods[-1].end_date,
'lines': [
('create', {
'account': revenue.id,
'debit': Decimal(100),
}),
('create', {
'account': expense.id,
'credit': Decimal(30),
}),
('create', {
'account': account_pl.id,
'credit': Decimal('70'),
}),
],
})
account_pl, = self.account.create([{
'name': 'P&L',
'type': type_equity.id,
'deferral': True,
'parent': revenue.parent.id,
'kind': 'other',
}])
self.move.create([{
'period': fiscalyear.periods[-1].id,
'journal': journal_closing.id,
'date': fiscalyear.periods[-1].end_date,
'lines': [
('create', [{
'account': revenue.id,
'debit': Decimal(100),
}, {
'account': expense.id,
'credit': Decimal(30),
}, {
'account': account_pl.id,
'credit': Decimal('70'),
}]),
],
}])
moves = self.move.search([
('state', '=', 'draft'),
('period.fiscalyear', '=', fiscalyear.id),