bi_sql_editor: add support for group operators
this commit allows specifying the group operator for each field. This functionality was originally included in a seperate module "bi_sql_editor_aggregate" in version 12.pull/670/head
parent
24ae610f8c
commit
02807d7ae2
|
@ -2,3 +2,4 @@
|
|||
|
||||
from . import bi_sql_view
|
||||
from . import bi_sql_view_field
|
||||
from . import ir_model
|
||||
|
|
|
@ -9,7 +9,7 @@ from psycopg2 import ProgrammingError
|
|||
|
||||
from odoo import SUPERUSER_ID, _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.tools import pycompat, sql
|
||||
from odoo.tools import pycompat, sql, table_columns
|
||||
from odoo.tools.safe_eval import safe_eval
|
||||
|
||||
from odoo.addons.base.models.ir_model import IrModel
|
||||
|
@ -726,6 +726,40 @@ class BiSQLView(models.Model):
|
|||
self._log_execute(req)
|
||||
sql_view.size = self.env.cr.fetchone()[0]
|
||||
|
||||
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 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]
|
||||
|
||||
def button_preview_sql_expression(self):
|
||||
self.button_validate_sql_expression()
|
||||
res = self._execute_sql_request()
|
||||
|
|
|
@ -49,6 +49,13 @@ class BiSQLViewField(models.Model):
|
|||
"timestamp without time zone": "datetime",
|
||||
}
|
||||
|
||||
_GROUP_OPERATOR_SELECTION = [
|
||||
("sum", "Sum"),
|
||||
("avg", "Average"),
|
||||
("min", "Minimum"),
|
||||
("max", "Maximum"),
|
||||
]
|
||||
|
||||
name = fields.Char(string="Name", required=True, readonly=True)
|
||||
|
||||
sql_type = fields.Char(
|
||||
|
@ -114,6 +121,13 @@ class BiSQLViewField(models.Model):
|
|||
help="For 'Many2one' Odoo field.\n" " Comodel of the field.",
|
||||
)
|
||||
|
||||
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",
|
||||
)
|
||||
|
||||
# Constrains Section
|
||||
@api.constrains("is_index")
|
||||
def _check_index_materialized(self):
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
from odoo import models
|
||||
|
||||
|
||||
class IrModelFields(models.Model):
|
||||
_inherit = "ir.model.fields"
|
||||
|
||||
def _add_manual_fields(self, model):
|
||||
super()._add_manual_fields(model)
|
||||
self.env["bi.sql.view"].check_manual_fields(model)
|
|
@ -1,4 +1,6 @@
|
|||
* Sylvain LE GAL (https://twitter.com/legalsylvain)
|
||||
* Richard deMeester, WilldooIT (http://www.willdooit.com/)
|
||||
* David James, WilldooIT (http://www.willdooit.com/)
|
||||
|
||||
* This module is highly inspired by the work of
|
||||
* Onestein: (http://www.onestein.nl/)
|
||||
|
|
|
@ -149,6 +149,11 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|||
<field name="name" />
|
||||
<field name="sql_type" />
|
||||
<field name="field_description" />
|
||||
<field
|
||||
name='group_operator'
|
||||
attrs="{
|
||||
'invisible': ['!', ('ttype', 'in', ('float', 'integer'))]}"
|
||||
/>
|
||||
<field
|
||||
name="ttype"
|
||||
attrs="{
|
||||
|
|
Loading…
Reference in New Issue