From e82391d39625f55fdf875a25f97b89ec5b0c1438 Mon Sep 17 00:00:00 2001 From: Richard deMeester Date: Thu, 14 Nov 2019 16:11:35 +1100 Subject: [PATCH 1/5] [ENH] Allow definition of group operator Adds the possibility, for float and integer columns, to apply a group operator (average, min, max). --- bi_sql_editor/__manifest__.py | 2 +- bi_sql_editor/models/__init__.py | 1 + bi_sql_editor/models/bi_sql_view_field.py | 20 ++++++++++++++++++++ bi_sql_editor/models/ir_model.py | 16 ++++++++++++++++ bi_sql_editor/views/view_bi_sql_view.xml | 2 ++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 bi_sql_editor/models/ir_model.py diff --git a/bi_sql_editor/__manifest__.py b/bi_sql_editor/__manifest__.py index 012dceac64..1ce605594e 100644 --- a/bi_sql_editor/__manifest__.py +++ b/bi_sql_editor/__manifest__.py @@ -5,7 +5,7 @@ { 'name': 'BI SQL Editor', 'summary': 'BI Views builder, based on Materialized or Normal SQL Views', - 'version': '12.0.1.1.0', + 'version': '12.0.1.2.0', 'license': 'AGPL-3', 'category': 'Reporting', 'author': 'GRAP,Odoo Community Association (OCA)', diff --git a/bi_sql_editor/models/__init__.py b/bi_sql_editor/models/__init__.py index e27e52a86f..ef99aabca6 100644 --- a/bi_sql_editor/models/__init__.py +++ b/bi_sql_editor/models/__init__.py @@ -2,3 +2,4 @@ from . import bi_sql_view from . import bi_sql_view_field +from . import ir_model diff --git a/bi_sql_editor/models/bi_sql_view_field.py b/bi_sql_editor/models/bi_sql_view_field.py index 1836784276..c3bc506975 100644 --- a/bi_sql_editor/models/bi_sql_view_field.py +++ b/bi_sql_editor/models/bi_sql_view_field.py @@ -24,6 +24,13 @@ class BiSQLViewField(models.Model): ('selection', 'selection'), ] + _GROUP_OPERATOR_SELECTION = [ + ('sum', 'Sum'), + ('avg', 'Average'), + ('min', 'Minimum'), + ('max', 'Maximum'), + ] + _GRAPH_TYPE_SELECTION = [ ('col', 'Column'), ('row', 'Row'), @@ -89,6 +96,11 @@ class BiSQLViewField(models.Model): " create a new field. If empty, this field will not be displayed" " neither available for search or group by function") + 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") + selection = fields.Text( string='Selection Options', default='[]', help="For 'Selection' Odoo field.\n" @@ -234,3 +246,11 @@ def _prepare_search_filter_field(self): self.field_description, self.name ) return res + + def adjust_manual_fields(self, model): + for sql_field in self: + if sql_field.ttype in ('integer', 'float') and\ + sql_field.group_operator and\ + sql_field.name in model._fields: + model._fields[sql_field.name].group_operator =\ + sql_field.group_operator diff --git a/bi_sql_editor/models/ir_model.py b/bi_sql_editor/models/ir_model.py new file mode 100644 index 0000000000..c07e9a8e75 --- /dev/null +++ b/bi_sql_editor/models/ir_model.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# 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(IrModelFields, self)._add_manual_fields(model) + if 'bi.sql.view' in self.env: + Sql = self.env['bi.sql.view'] + if 'model_id' in Sql._fields: + Sql.search([('model_name', '=', model._name)] + ).bi_sql_view_field_ids.adjust_manual_fields(model) diff --git a/bi_sql_editor/views/view_bi_sql_view.xml b/bi_sql_editor/views/view_bi_sql_view.xml index 3b14342bf6..28b25a4cfd 100644 --- a/bi_sql_editor/views/view_bi_sql_view.xml +++ b/bi_sql_editor/views/view_bi_sql_view.xml @@ -80,6 +80,8 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + + 'invisible': ['!', ('ttype', 'in', ('float', 'integer'))]}"/>