Skip to content

Commit e7fbdb6

Browse files
committed
[MIG] bi_sql_editor: Migration to 18.0
1 parent 43a21eb commit e7fbdb6

File tree

9 files changed

+134
-123
lines changed

9 files changed

+134
-123
lines changed

bi_sql_editor/README.rst

+18-10
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ BI SQL Editor
1717
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1818
:alt: License: AGPL-3
1919
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github
20-
:target: https://github.com/OCA/reporting-engine/tree/17.0/bi_sql_editor
20+
:target: https://github.com/OCA/reporting-engine/tree/18.0/bi_sql_editor
2121
:alt: OCA/reporting-engine
2222
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23-
:target: https://translation.odoo-community.org/projects/reporting-engine-17-0/reporting-engine-17-0-bi_sql_editor
23+
:target: https://translation.odoo-community.org/projects/reporting-engine-18-0/reporting-engine-18-0-bi_sql_editor
2424
:alt: Translate me on Weblate
2525
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26-
:target: https://runboat.odoo-community.org/builds?repo=OCA/reporting-engine&target_branch=17.0
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/reporting-engine&target_branch=18.0
2727
:alt: Try me on Runboat
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -126,10 +126,10 @@ to make reporting depending on the current companies of the user.
126126
- Finally, click on 'Create UI', to create new menu, action, graph view
127127
and search view.
128128

129-
.. |image1| image:: https://raw.githubusercontent.com/OCA/reporting-engine/17.0/bi_sql_editor/static/description/01_sql_request.png
130-
.. |image2| image:: https://raw.githubusercontent.com/OCA/reporting-engine/17.0/bi_sql_editor/static/description/02_security_access.png
131-
.. |image3| image:: https://raw.githubusercontent.com/OCA/reporting-engine/17.0/bi_sql_editor/static/description/03_field_mapping.png
132-
.. |image4| image:: https://raw.githubusercontent.com/OCA/reporting-engine/17.0/bi_sql_editor/static/description/04_materialized_view_setting.png
129+
.. |image1| image:: https://raw.githubusercontent.com/OCA/reporting-engine/18.0/bi_sql_editor/static/description/01_sql_request.png
130+
.. |image2| image:: https://raw.githubusercontent.com/OCA/reporting-engine/18.0/bi_sql_editor/static/description/02_security_access.png
131+
.. |image3| image:: https://raw.githubusercontent.com/OCA/reporting-engine/18.0/bi_sql_editor/static/description/03_field_mapping.png
132+
.. |image4| image:: https://raw.githubusercontent.com/OCA/reporting-engine/18.0/bi_sql_editor/static/description/04_materialized_view_setting.png
133133

134134
Usage
135135
=====
@@ -145,15 +145,15 @@ To use this module, you need to:
145145

146146
- You can switch to 'Graph' or 'tree' views as any report.
147147

148-
.. |usage-image1| image:: https://raw.githubusercontent.com/OCA/reporting-engine/17.0/bi_sql_editor/static/description/05_reporting_pivot.png
148+
.. |usage-image1| image:: https://raw.githubusercontent.com/OCA/reporting-engine/18.0/bi_sql_editor/static/description/05_reporting_pivot.png
149149

150150
Bug Tracker
151151
===========
152152

153153
Bugs are tracked on `GitHub Issues <https://github.com/OCA/reporting-engine/issues>`_.
154154
In case of trouble, please check there if your issue has already been reported.
155155
If you spotted it first, help us to smash it by providing a detailed and welcomed
156-
`feedback <https://github.com/OCA/reporting-engine/issues/new?body=module:%20bi_sql_editor%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
156+
`feedback <https://github.com/OCA/reporting-engine/issues/new?body=module:%20bi_sql_editor%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
157157

158158
Do not contact contributors directly about support or help with technical issues.
159159

@@ -176,6 +176,8 @@ Contributors
176176

177177
- Guillem Casassas guillem.casassas@forgeflow.com
178178

179+
- Thien Vo thienvh@trobz.com
180+
179181
- This module is highly inspired by the work of
180182

181183
- Onestein: (http://www.onestein.nl/) Module:
@@ -189,6 +191,12 @@ Contributors
189191
grap/odoo-addons-misc/pos_sale_reporting link:
190192
https://github.com/grap/odoo-addons-misc/tree/7.0/pos_sale_reporting
191193

194+
Other credits
195+
-------------
196+
197+
The migration of this module from 17.0 to 18.0 was financially supported
198+
by Camptocamp.
199+
192200
Maintainers
193201
-----------
194202

@@ -210,6 +218,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
210218

211219
|maintainer-legalsylvain|
212220

213-
This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/17.0/bi_sql_editor>`_ project on GitHub.
221+
This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/18.0/bi_sql_editor>`_ project on GitHub.
214222

215223
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

bi_sql_editor/__manifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"name": "BI SQL Editor",
77
"summary": "BI Views builder, based on Materialized or Normal SQL Views",
8-
"version": "17.0.1.1.0",
8+
"version": "18.0.1.0.0",
99
"license": "AGPL-3",
1010
"category": "Reporting",
1111
"author": "GRAP,Odoo Community Association (OCA)",

bi_sql_editor/models/bi_sql_view.py

+50-54
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from datetime import datetime, timedelta
77

88
from psycopg2 import ProgrammingError
9+
from psycopg2.sql import SQL, Identifier
910

1011
from odoo import SUPERUSER_ID, _, api, fields, models
1112
from odoo.exceptions import UserError, ValidationError
12-
from odoo.tools import sql, table_columns
13+
from odoo.tools import sql
1314
from odoo.tools.safe_eval import safe_eval
1415

1516
_logger = logging.getLogger(__name__)
@@ -44,14 +45,12 @@ class BiSQLView(models.Model):
4445

4546
view_name = fields.Char(
4647
compute="_compute_view_name",
47-
readonly=True,
4848
store=True,
4949
help="Full name of the SQL view",
5050
)
5151

5252
model_name = fields.Char(
5353
compute="_compute_model_name",
54-
readonly=True,
5554
store=True,
5655
help="Full Qualified Name of the transient model that will" " be created.",
5756
)
@@ -65,16 +64,15 @@ class BiSQLView(models.Model):
6564

6665
size = fields.Char(
6766
string="Database Size",
68-
readonly=True,
6967
help="Size of the materialized view and its indexes",
7068
)
7169

7270
state = fields.Selection(selection_add=_STATE_SQL_EDITOR)
7371

7472
view_order = fields.Char(
7573
required=True,
76-
default="pivot,graph,tree",
77-
help="Comma-separated text. Possible values:" ' "graph", "pivot" or "tree"',
74+
default="pivot,graph,list",
75+
help="Comma-separated text. Possible values:" ' "graph", "pivot" or "list"',
7876
)
7977

8078
query = fields.Text(
@@ -111,9 +109,7 @@ class BiSQLView(models.Model):
111109
inverse_name="bi_sql_view_id",
112110
)
113111

114-
model_id = fields.Many2one(
115-
string="Odoo Model", comodel_name="ir.model", readonly=True
116-
)
112+
model_id = fields.Many2one(string="Odoo Model", comodel_name="ir.model")
117113
# UI related fields
118114
# 1. Editable fields, which can be set by the user (optional) before
119115
# creating the UI elements
@@ -133,39 +129,30 @@ def _default_parent_menu_id(self):
133129
)
134130

135131
# 2. Readonly fields, non editable by the user
136-
tree_view_id = fields.Many2one(
137-
string="Odoo Tree View", comodel_name="ir.ui.view", readonly=True
138-
)
132+
tree_view_id = fields.Many2one(string="Odoo List View", comodel_name="ir.ui.view")
139133

140-
graph_view_id = fields.Many2one(
141-
string="Odoo Graph View", comodel_name="ir.ui.view", readonly=True
142-
)
134+
graph_view_id = fields.Many2one(string="Odoo Graph View", comodel_name="ir.ui.view")
143135

144-
pivot_view_id = fields.Many2one(
145-
string="Odoo Pivot View", comodel_name="ir.ui.view", readonly=True
146-
)
136+
pivot_view_id = fields.Many2one(string="Odoo Pivot View", comodel_name="ir.ui.view")
147137

148138
search_view_id = fields.Many2one(
149-
string="Odoo Search View", comodel_name="ir.ui.view", readonly=True
139+
string="Odoo Search View", comodel_name="ir.ui.view"
150140
)
151141

152142
action_id = fields.Many2one(
153-
string="Odoo Action", comodel_name="ir.actions.act_window", readonly=True
143+
string="Odoo Action", comodel_name="ir.actions.act_window"
154144
)
155145

156-
menu_id = fields.Many2one(
157-
string="Odoo Menu", comodel_name="ir.ui.menu", readonly=True
158-
)
146+
menu_id = fields.Many2one(string="Odoo Menu", comodel_name="ir.ui.menu")
159147

160148
cron_id = fields.Many2one(
161149
string="Odoo Cron",
162150
comodel_name="ir.cron",
163-
readonly=True,
164151
help="Cron Task that will refresh the materialized view",
165152
ondelete="cascade",
166153
)
167154

168-
rule_id = fields.Many2one(string="Odoo Rule", comodel_name="ir.rule", readonly=True)
155+
rule_id = fields.Many2one(string="Odoo Rule", comodel_name="ir.rule")
169156

170157
sequence = fields.Integer(string="sequence")
171158

@@ -183,9 +170,9 @@ def _check_view_order(self):
183170
for rec in self:
184171
if rec.view_order:
185172
for vtype in rec.view_order.split(","):
186-
if vtype not in ("graph", "pivot", "tree"):
173+
if vtype not in ("graph", "pivot", "list"):
187174
raise UserError(
188-
_("Only graph, pivot or tree views are supported")
175+
_("Only graph, pivot or list views are supported")
189176
)
190177

191178
# Compute Section
@@ -244,15 +231,15 @@ def write(self, vals):
244231
rec.menu_id.sequence = rec.sequence
245232
return res
246233

247-
def unlink(self):
234+
@api.ondelete(at_uninstall=False)
235+
def _check_unlink_constraints(self):
248236
if any(view.state not in ("draft", "sql_valid") for view in self):
249237
raise UserError(
250238
_(
251-
"You can only unlink draft views."
239+
"You can only unlink draft views. "
252240
"If you want to delete them, first set them to draft."
253241
)
254242
)
255-
return super().unlink()
256243

257244
def copy(self, default=None):
258245
self.ensure_one()
@@ -395,8 +382,7 @@ def _prepare_cron(self):
395382
.search([("model", "=", self._name)], limit=1)
396383
.id,
397384
"state": "code",
398-
"code": "model._refresh_materialized_view_cron(%s)" % self.ids,
399-
"numbercall": -1,
385+
"code": f"model._refresh_materialized_view_cron({self.ids})",
400386
"interval_number": 1,
401387
"interval_type": "days",
402388
"nextcall": now + timedelta(days=1),
@@ -416,11 +402,11 @@ def _prepare_tree_view(self):
416402
self.ensure_one()
417403
return {
418404
"name": self.name,
419-
"type": "tree",
405+
"type": "list",
420406
"model": self.model_id.model,
421407
"arch": """<?xml version="1.0"?>"""
422-
"""<tree name="Analysis">{}"""
423-
"""</tree>""".format(
408+
"""<list name="Analysis">{}"""
409+
"""</list>""".format(
424410
"".join([x._prepare_tree_field() for x in self.bi_sql_view_field_ids])
425411
),
426412
}
@@ -477,7 +463,7 @@ def _prepare_action(self):
477463
self.ensure_one()
478464
view_mode = self.view_order
479465
first_view = view_mode.split(",")[0]
480-
if first_view == "tree":
466+
if first_view == "list":
481467
view_id = self.tree_view_id.id
482468
elif first_view == "pivot":
483469
view_id = self.pivot_view_id.id
@@ -510,19 +496,22 @@ def _prepare_menu(self):
510496
return {
511497
"name": self.name,
512498
"parent_id": self.parent_menu_id.id,
513-
"action": "ir.actions.act_window,%s" % self.action_id.id,
499+
"action": f"ir.actions.act_window,{self.action_id.id}",
514500
"sequence": self.sequence,
515501
}
516502

517503
# Custom Section
518504
def _log_execute(self, req):
519-
_logger.info("Executing SQL Request %s ..." % req)
505+
_logger.info(f"Executing SQL Request {req} ...")
520506
self.env.cr.execute(req)
521507

522508
def _drop_view(self):
523509
for sql_view in self:
524510
self._log_execute(
525-
f"DROP {sql_view.materialized_text} VIEW IF EXISTS {sql_view.view_name}"
511+
SQL("DROP {materialized_text} VIEW IF EXISTS {view_name}").format(
512+
materialized_text=SQL(sql_view.materialized_text),
513+
view_name=Identifier(sql_view.materialized_text),
514+
)
526515
)
527516
sql_view.size = False
528517

@@ -551,8 +540,11 @@ def _create_index(self):
551540
lambda x: x.is_index is True
552541
):
553542
self._log_execute(
554-
f"CREATE INDEX {sql_field.index_name} ON {sql_view.view_name} "
555-
f"({sql_field.name});"
543+
SQL("CREATE INDEX {index_name} ON {view_name} ({name});").format(
544+
index_name=SQL(sql_field.index_name),
545+
view_name=Identifier(sql_view.view_name),
546+
name=Identifier(sql_field.name),
547+
)
556548
)
557549

558550
def _create_model_and_fields(self):
@@ -562,7 +554,7 @@ def _create_model_and_fields(self):
562554
sql_view.rule_id = self.env["ir.rule"].create(self._prepare_rule()).id
563555
# Drop table, created by the ORM
564556
if sql.table_exists(self._cr, sql_view.view_name):
565-
req = "DROP TABLE %s" % sql_view.view_name
557+
req = SQL("DROP TABLE {}").format(Identifier(sql_view.view_name))
566558
self._log_execute(req)
567559

568560
def _create_model_access(self):
@@ -585,28 +577,29 @@ def _drop_model_and_fields(self):
585577

586578
def _hook_executed_request(self):
587579
self.ensure_one()
588-
req = (
580+
req = SQL(
589581
"""
590582
SELECT attnum,
591583
attname AS column,
592584
format_type(atttypid, atttypmod) AS type
593585
FROM pg_attribute
594-
WHERE attrelid = '%s'::regclass
586+
WHERE attrelid = '{view_name}'::regclass
595587
AND NOT attisdropped
596588
AND attnum > 0
597589
ORDER BY attnum;"""
598-
% self.view_name
599-
)
590+
).format(view_name=Identifier(self.view_name))
600591
self._log_execute(req)
601592
return self.env.cr.fetchall()
602593

603594
def _prepare_request_check_execution(self):
604595
self.ensure_one()
605-
return f"CREATE VIEW {self.view_name} AS ({self.query});"
596+
return SQL("CREATE VIEW {view_name} AS ({query});").format(
597+
view_name=Identifier(self.view_name), query=SQL(self.query)
598+
)
606599

607600
def _prepare_request_for_execution(self):
608601
self.ensure_one()
609-
query = (
602+
query = SQL(
610603
"""
611604
SELECT
612605
CAST(row_number() OVER () as integer) AS id,
@@ -616,11 +609,14 @@ def _prepare_request_for_execution(self):
616609
CAST(Null as integer) as write_uid,
617610
my_query.*
618611
FROM
619-
(%s) as my_query
612+
({}) as my_query
620613
"""
621-
% self.query
614+
).format(SQL(self.query))
615+
return SQL("CREATE {materialized_text} VIEW {view_name} AS ({query});").format(
616+
materialized_text=SQL(self.materialized_text),
617+
view_name=Identifier(self.view_name),
618+
query=query,
622619
)
623-
return f"CREATE {self.materialized_text} VIEW {self.view_name} AS ({query});"
624620

625621
def _check_execution(self):
626622
"""Ensure that the query is valid, trying to execute it.
@@ -689,8 +685,8 @@ def _refresh_materialized_view(self):
689685

690686
def _refresh_size(self):
691687
for sql_view in self:
692-
req = "SELECT pg_size_pretty(pg_total_relation_size('%s'));" % (
693-
sql_view.view_name
688+
req = SQL("SELECT pg_size_pretty(pg_total_relation_size('{}'));").format(
689+
Identifier(sql_view.view_name)
694690
)
695691
self._log_execute(req)
696692
sql_view.size = self.env.cr.fetchone()[0]
@@ -700,7 +696,7 @@ def check_manual_fields(self, model):
700696
# early on install / startup - particularly problematic during upgrade
701697
if model._name.startswith(
702698
self._model_prefix
703-
) and "group_operator" in table_columns(self.env.cr, "bi_sql_view_field"):
699+
) and "group_operator" in sql.table_columns(self.env.cr, "bi_sql_view_field"):
704700
# Use SQL instead of ORM, as ORM might not be fully initialised -
705701
# we have no control over the order that fields are defined!
706702
# We are not concerned about user security rules.

0 commit comments

Comments
 (0)