From fdec58782bd4cbf2eaa548e60ad6cf5e3eccff3e Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Mon, 2 Sep 2024 11:15:47 +0200 Subject: [PATCH] [IMP]bi_sql_editor: archive Scheduled Action to not recreate when resetting the SQL View Based on the agreed solution: https://github.com/OCA/reporting-engine/pull/903#pullrequestreview-2273835493. It is best to archive the Scheduled Action every time you set back to the draft state. Then, when creating the SQL view, if it is a materialized view, unarchive the Scheduled Action if already exists, otherwise create it from scratch. Then, if the SQL View is deleted, also make sure to remove the Scheduled Action if there is one assigned as it has not been done in the `button_set_draft` method --- bi_sql_editor/models/bi_sql_view.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/bi_sql_editor/models/bi_sql_view.py b/bi_sql_editor/models/bi_sql_view.py index d6ec426a46..991b4003db 100644 --- a/bi_sql_editor/models/bi_sql_view.py +++ b/bi_sql_editor/models/bi_sql_view.py @@ -18,8 +18,9 @@ @api.model def _instanciate(self, model_data): - """ Return a class for the custom model given by - parameters ``model_data``. """ + """Return a class for the custom model given by + parameters ``model_data``.""" + # This monkey patch is meant to avoid create/search tables for those # materialized views. Doing "super" doesn't work. class CustomModel(models.Model): @@ -281,6 +282,8 @@ def unlink(self): "If you want to delete them, first set them to draft." ) ) + # Remove Scheduled Actions, as they were not removed in `button_set_draft` + self.mapped("cron_id").unlink() return super(BiSQLView, self).unlink() def copy(self, default=None): @@ -310,9 +313,13 @@ def button_create_sql_view_and_model(self): sql_view._create_index() if sql_view.is_materialized: - sql_view.cron_id = ( - self.env["ir.cron"].create(sql_view._prepare_cron()).id - ) + # Create Cron only if it has not been created yet, otherwise unarchive it + if sql_view.cron_id and not sql_view.cron_id.active: + sql_view.cron_id.write({"active": True}) + else: + sql_view.cron_id = ( + self.env["ir.cron"].create(sql_view._prepare_cron()).id + ) sql_view.state = "model_valid" def button_set_draft(self): @@ -324,7 +331,7 @@ def button_set_draft(self): sql_view.pivot_view_id.unlink() sql_view.search_view_id.unlink() if sql_view.cron_id: - sql_view.cron_id.unlink() + sql_view.cron_id.write({"active": False}) if sql_view.state in ("model_valid", "ui_valid"): # Drop SQL View (and indexes by cascade)