From 3e61ea05b42f7e87c1590e0153cdace5ee46645a Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Fri, 30 Aug 2024 12:20:04 +0200 Subject: [PATCH] [IMP]bi_sql_editor: scheduled action periodicity settings Based on changes done in: https://github.com/OCA/reporting-engine/pull/903 --- bi_sql_editor/models/bi_sql_view.py | 38 ++++++++++++++---- bi_sql_editor/readme/CONFIGURE.rst | 13 +++++-- bi_sql_editor/views/view_bi_sql_view.xml | 49 +++++++++++++++--------- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/bi_sql_editor/models/bi_sql_view.py b/bi_sql_editor/models/bi_sql_view.py index d6ec426a46..21bb713147 100644 --- a/bi_sql_editor/models/bi_sql_view.py +++ b/bi_sql_editor/models/bi_sql_view.py @@ -3,7 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import logging -from datetime import datetime +from datetime import datetime, timedelta from psycopg2 import ProgrammingError @@ -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): @@ -181,33 +182,39 @@ def _default_parent_menu_id(self): tree_view_id = fields.Many2one( string="Odoo Tree View", comodel_name="ir.ui.view", readonly=True ) - graph_view_id = fields.Many2one( string="Odoo Graph View", comodel_name="ir.ui.view", readonly=True ) - pivot_view_id = fields.Many2one( string="Odoo Pivot View", comodel_name="ir.ui.view", readonly=True ) - search_view_id = fields.Many2one( string="Odoo Search View", comodel_name="ir.ui.view", readonly=True ) - action_id = fields.Many2one( string="Odoo Action", comodel_name="ir.actions.act_window", readonly=True ) - menu_id = fields.Many2one( string="Odoo Menu", comodel_name="ir.ui.menu", readonly=True ) + # Scheduled Action related fields cron_id = fields.Many2one( string="Odoo Cron", comodel_name="ir.cron", readonly=True, help="Cron Task that will refresh the materialized view", ) + cron_interval_number = fields.Integer( + help="Scheduled Action interval number", default=1, required=True + ) + cron_interval_type = fields.Selection( + selection=lambda self: self._get_ir_cron_interval_type_selection(), + default="days", + required=True, + help="Scheduled Action interval type, same values as the ones " + "selectable in the Scheduled Action.", + ) rule_id = fields.Many2one(string="Odoo Rule", comodel_name="ir.rule", readonly=True) @@ -219,6 +226,13 @@ def _default_parent_menu_id(self): sequence = fields.Integer(string="sequence") + # Gets section + @api.model + def _get_ir_cron_interval_type_selection(self): + return self.env["ir.cron"].fields_get(allfields=["interval_type"])[ + "interval_type" + ]["selection"] + # Constrains Section @api.constrains("is_materialized") def _check_index_materialized(self): @@ -403,6 +417,10 @@ def _prepare_model_access(self): def _prepare_cron(self): self.ensure_one() + now = datetime.now() + interval_number = self.cron_interval_number + interval_type = self.cron_interval_type + date_interval_dict = {interval_type: interval_number} return { "name": _("Refresh Materialized View %s") % self.view_name, "user_id": SUPERUSER_ID, @@ -412,6 +430,10 @@ def _prepare_cron(self): "state": "code", "code": "model._refresh_materialized_view_cron(%s)" % self.ids, "numbercall": -1, + "interval_number": interval_number, + "interval_type": interval_type, + "nextcall": now + timedelta(**date_interval_dict), + "active": True, } def _prepare_rule(self): diff --git a/bi_sql_editor/readme/CONFIGURE.rst b/bi_sql_editor/readme/CONFIGURE.rst index e528a24eea..4a97cd7c6d 100644 --- a/bi_sql_editor/readme/CONFIGURE.rst +++ b/bi_sql_editor/readme/CONFIGURE.rst @@ -34,10 +34,15 @@ To configure this module, you need to: .. figure:: ../static/description/04_materialized_view_setting.png :width: 800 px -* Before applying the final step, you will need to add a specific Parent Menu to - use when creating the UI Menu for the report. By default, it will be set with - the `SQL Views` menu, which can be changed before creating the UI elements in - order to have the report accessible from a different place within Odoo. +* Before creating the UI elements, you can modify two specific settings based on your + needs: + + * **Parent Menu**: Apply a Parent Menu to use for when creating the UI elements. By + default, it will be set with the ``SQL Views`` menu, but you can change it to make + the report accessible from a different place within Odoo. + + * **Scheduled Action periodicity**: To customize the frequency for running the + Scheduled Action that refreshes the Materialized view, go to the Settings page. * Finally, click on 'Create UI', to create new menu, action, graph view and search view. diff --git a/bi_sql_editor/views/view_bi_sql_view.xml b/bi_sql_editor/views/view_bi_sql_view.xml index 5dc41bba6a..fa89d92590 100644 --- a/bi_sql_editor/views/view_bi_sql_view.xml +++ b/bi_sql_editor/views/view_bi_sql_view.xml @@ -202,28 +202,39 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + + + + - - - - + + + + + + + - - - - - - - - - - - - + + + + + + +