[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
This commit is contained in:
GuillemCForgeFlow
2024-09-02 11:15:47 +02:00
parent c3f636d57b
commit fdec58782b

View File

@@ -18,8 +18,9 @@ _logger = logging.getLogger(__name__)
@api.model @api.model
def _instanciate(self, model_data): def _instanciate(self, model_data):
""" Return a class for the custom model given by """Return a class for the custom model given by
parameters ``model_data``. """ parameters ``model_data``."""
# This monkey patch is meant to avoid create/search tables for those # This monkey patch is meant to avoid create/search tables for those
# materialized views. Doing "super" doesn't work. # materialized views. Doing "super" doesn't work.
class CustomModel(models.Model): class CustomModel(models.Model):
@@ -281,6 +282,8 @@ class BiSQLView(models.Model):
"If you want to delete them, first set them to draft." "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() return super(BiSQLView, self).unlink()
def copy(self, default=None): def copy(self, default=None):
@@ -310,9 +313,13 @@ class BiSQLView(models.Model):
sql_view._create_index() sql_view._create_index()
if sql_view.is_materialized: if sql_view.is_materialized:
sql_view.cron_id = ( # Create Cron only if it has not been created yet, otherwise unarchive it
self.env["ir.cron"].create(sql_view._prepare_cron()).id 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" sql_view.state = "model_valid"
def button_set_draft(self): def button_set_draft(self):
@@ -324,7 +331,7 @@ class BiSQLView(models.Model):
sql_view.pivot_view_id.unlink() sql_view.pivot_view_id.unlink()
sql_view.search_view_id.unlink() sql_view.search_view_id.unlink()
if sql_view.cron_id: 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"): if sql_view.state in ("model_valid", "ui_valid"):
# Drop SQL View (and indexes by cascade) # Drop SQL View (and indexes by cascade)