Skip to content

Commit

Permalink
[IMP]bi_sql_editor: scheduled action periodicity settings
Browse files Browse the repository at this point in the history
Based on changes done in: #903
  • Loading branch information
GuillemCForgeFlow committed Aug 30, 2024
1 parent c3f636d commit 504b952
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bi_sql_editor/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ promote its widespread use.

This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/13.0/bi_sql_editor>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
38 changes: 30 additions & 8 deletions bi_sql_editor/models/bi_sql_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down
13 changes: 9 additions & 4 deletions bi_sql_editor/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion bi_sql_editor/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,4 @@ <h3><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h3>
</div>
</div>
</body>
</html>
</html>
49 changes: 30 additions & 19 deletions bi_sql_editor/views/view_bi_sql_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,39 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<group string="Context">
<field name="action_context" nolabel="1" colspan="4" />
</group>
<group
name="ir_cron_settings"
string="Scheduled Action settings"
>
<field
name="cron_interval_number"
attrs="{'readonly': [('state', '=', 'ui_valid')]}"
/>
<field
name="cron_interval_type"
attrs="{'readonly': [('state', '=', 'ui_valid')]}"
/>
</group>
</page>
<page string="Extras Information">
<group>
<group string="Model">
<field name="model_name" />
<field
name="model_id"
attrs="{'invisible': [('state', '=', 'draft')]}"
/>
<group string="Model">
<field name="model_name" />
<field
name="model_id"
attrs="{'invisible': [('state', '=', 'draft')]}"
/>
</group>
<group string="User Interface">
<group string="UI Parameters">
<field name="parent_menu_id" />
</group>
<group string="User Interface">
<group string="UI Parameters">
<field name="parent_menu_id" />
</group>
<group string="UI Instances">
<field name="tree_view_id" />
<field name="graph_view_id" />
<field name="pivot_view_id" />
<field name="search_view_id" />
<field name="action_id" />
<field name="menu_id" />
</group>
<group string="UI Instances">
<field name="tree_view_id" />
<field name="graph_view_id" />
<field name="pivot_view_id" />
<field name="search_view_id" />
<field name="action_id" />
<field name="menu_id" />
</group>
</group>
</page>
Expand Down

0 comments on commit 504b952

Please sign in to comment.