mirror of
https://github.com/OCA/reporting-engine.git
synced 2025-02-16 16:30:38 +02:00
[12.0][ENH] sql view - allow group operator
Replaces #345 Adds the possibility, for float and integer columns, to apply a group operator (average, min, max).
This commit is contained in:
5
bi_sql_editor_aggregate/models/__init__.py
Normal file
5
bi_sql_editor_aggregate/models/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from . import bi_sql_view
|
||||
from . import bi_sql_view_field
|
||||
from . import ir_model
|
||||
38
bi_sql_editor_aggregate/models/bi_sql_view.py
Normal file
38
bi_sql_editor_aggregate/models/bi_sql_view.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import models, tools
|
||||
|
||||
|
||||
class BiSQLView(models.Model):
|
||||
_inherit = 'bi.sql.view'
|
||||
|
||||
def check_manual_fields(self, model):
|
||||
# check the fields we need are defined on self, to stop it going
|
||||
# early on install / startup - particularly problematic during upgrade
|
||||
if 'group_operator' in tools.table_columns(
|
||||
self.env.cr, 'bi_sql_view_field') and\
|
||||
model._name.startswith(self._model_prefix):
|
||||
# Use SQL instead of ORM, as ORM might not be fully initialised -
|
||||
# we have no control over the order that fields are defined!
|
||||
# We are not concerned about user security rules.
|
||||
self.env.cr.execute(
|
||||
"""
|
||||
SELECT
|
||||
f.name,
|
||||
f.ttype,
|
||||
f.group_operator
|
||||
FROM
|
||||
bi_sql_view v
|
||||
LEFT JOIN bi_sql_view_field f ON f.bi_sql_view_id = v.id
|
||||
WHERE
|
||||
v.model_name = %s
|
||||
;
|
||||
""", (model._name,)
|
||||
)
|
||||
sql_fields = self.env.cr.fetchall()
|
||||
|
||||
for sql_field in sql_fields:
|
||||
if sql_field[0] in model._fields and\
|
||||
sql_field[1] in ('integer', 'float') and\
|
||||
sql_field[2]:
|
||||
model._fields[sql_field[0]].group_operator = sql_field[2]
|
||||
25
bi_sql_editor_aggregate/models/bi_sql_view_field.py
Normal file
25
bi_sql_editor_aggregate/models/bi_sql_view_field.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
|
||||
|
||||
class BiSQLViewField(models.Model):
|
||||
_inherit = 'bi.sql.view.field'
|
||||
|
||||
_GROUP_OPERATOR_SELECTION = [
|
||||
('sum', 'Sum'),
|
||||
('avg', 'Average'),
|
||||
('min', 'Minimum'),
|
||||
('max', 'Maximum'),
|
||||
]
|
||||
|
||||
_GRAPH_TYPE_SELECTION = [
|
||||
('col', 'Column'),
|
||||
('row', 'Row'),
|
||||
('measure', 'Measure'),
|
||||
]
|
||||
|
||||
group_operator = fields.Selection(
|
||||
string='Group Operator', selection=_GROUP_OPERATOR_SELECTION,
|
||||
help="By default, Odoo will sum the values when grouping. If you wish"
|
||||
" to alter the behaviour, choose an alternate Group Operator")
|
||||
14
bi_sql_editor_aggregate/models/ir_model.py
Normal file
14
bi_sql_editor_aggregate/models/ir_model.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class IrModelFields(models.Model):
|
||||
_inherit = 'ir.model.fields'
|
||||
|
||||
def _add_manual_fields(self, model):
|
||||
super()._add_manual_fields(model)
|
||||
if 'bi.sql.view' in self.env:
|
||||
Sql = self.env['bi.sql.view']
|
||||
if hasattr(Sql, 'check_manual_fields'):
|
||||
Sql.check_manual_fields(model)
|
||||
Reference in New Issue
Block a user