From 71a88097b998077ef04a138ae646f5ffeea259c9 Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Thu, 14 Mar 2019 10:33:27 +0100 Subject: [PATCH] [IMP] - improve post_init_hook and _generate_forecast_periods --- contract_forecast/hooks.py | 14 +++++++++----- contract_forecast/models/contract_line.py | 9 ++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/contract_forecast/hooks.py b/contract_forecast/hooks.py index be5c513e..5119f79e 100644 --- a/contract_forecast/hooks.py +++ b/contract_forecast/hooks.py @@ -19,8 +19,12 @@ def post_init_hook(cr, registry): ) with api.Environment.manage(): env = api.Environment(cr, SUPERUSER_ID, {}) - contract_lines = env["account.analytic.invoice.line"].search( - [('is_canceled', '=', False)] - ) - for contract_line in contract_lines: - contract_line.with_delay()._generate_forecast_periods() + offset = 0 + while True: + contract_lines = env["account.analytic.invoice.line"].search( + [('is_canceled', '=', False)], limit=100, offset=offset + ) + contract_lines.with_delay()._generate_forecast_periods() + if len(contract_lines) < 100: + break + offset += 100 diff --git a/contract_forecast/models/contract_line.py b/contract_forecast/models/contract_line.py index f89d0080..30c31e58 100644 --- a/contract_forecast/models/contract_line.py +++ b/contract_forecast/models/contract_line.py @@ -66,6 +66,7 @@ class AccountAnalyticInvoiceLine(models.Model): def _generate_forecast_periods(self): values = [] for rec in self: + rec.forecast_period_ids.unlink() if rec.recurring_next_date: last_date_invoiced = ( rec.last_date_invoiced @@ -99,11 +100,6 @@ class AccountAnalyticInvoiceLine(models.Model): ) return self.env["contract.line.forecast.period"].create(values) - @api.multi - @job(default_channel=QUEUE_CHANNEL) - def _unlink_forecast_periods(self): - return self.mapped("forecast_period_ids").unlink() - @api.model def create(self, values): contract_lines = super(AccountAnalyticInvoiceLine, self).create(values) @@ -140,6 +136,5 @@ class AccountAnalyticInvoiceLine(models.Model): ] ): for rec in self: - rec._unlink_forecast_periods() - rec._generate_forecast_periods() + rec.with_delay()._generate_forecast_periods() return res