From 2f0e64754e639e7a87f39ec20c895ad2e21d722f Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 30 Sep 2019 18:06:50 +0200 Subject: [PATCH] Convert onchange to computed --- .../tests/test_account_tax_balance.py | 4 +-- .../wizard/open_tax_balances.py | 31 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/account_tax_balance/tests/test_account_tax_balance.py b/account_tax_balance/tests/test_account_tax_balance.py index 404746c5..eee4a79c 100644 --- a/account_tax_balance/tests/test_account_tax_balance.py +++ b/account_tax_balance/tests/test_account_tax_balance.py @@ -95,8 +95,8 @@ class TestAccountTaxBalance(HttpCase): wizard = self.env['wizard.open.tax.balances'].new({ 'date_range_id': current_range[0].id, }) - wizard.onchange_date_range_id() - wizard._convert_to_write(wizard._cache) + self.assertEqual(wizard.from_date, current_range[0].date_start) + self.assertEqual(wizard.to_date, current_range[0].date_end) action = wizard.open_taxes() self.assertEqual( action['context']['from_date'], current_range[0].date_start) diff --git a/account_tax_balance/wizard/open_tax_balances.py b/account_tax_balance/wizard/open_tax_balances.py index 447133af..e60c200d 100644 --- a/account_tax_balance/wizard/open_tax_balances.py +++ b/account_tax_balance/wizard/open_tax_balances.py @@ -9,23 +9,32 @@ class WizardOpenTaxBalances(models.TransientModel): _description = 'Wizard Open Tax Balances' company_id = fields.Many2one( - 'res.company', 'Company', required=True, + 'res.company', required=True, default=lambda self: self.env.user.company_id) - from_date = fields.Date('From date', required=True) - to_date = fields.Date('To date', required=True) - date_range_id = fields.Many2one('date.range', 'Date range') + from_date = fields.Date( + required=True, + store=True, + readonly=False, + compute='_compute_date_range') + to_date = fields.Date( + required=True, + store=True, + readonly=False, + compute='_compute_date_range') + date_range_id = fields.Many2one('date.range') target_move = fields.Selection([ ('posted', 'All Posted Entries'), ('all', 'All Entries'), ], 'Target Moves', required=True, default='posted') - @api.onchange('date_range_id') - def onchange_date_range_id(self): - if self.date_range_id: - self.from_date = self.date_range_id.date_start - self.to_date = self.date_range_id.date_end - else: - self.from_date = self.to_date = None + @api.depends('date_range_id') + def _compute_date_range(self): + for wizard in self: + if wizard.date_range_id: + wizard.from_date = wizard.date_range_id.date_start + wizard.to_date = wizard.date_range_id.date_end + else: + wizard.from_date = wizard.to_date = None def open_taxes(self): self.ensure_one()