Skip to content

Commit

Permalink
Merge fb8aa80 into 14fba65
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-willdooit committed Nov 22, 2019
2 parents 14fba65 + fb8aa80 commit 11edfca
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bi_sql_editor/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand Down
1 change: 1 addition & 0 deletions bi_sql_editor/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from . import bi_sql_view
from . import bi_sql_view_field
from . import ir_model
12 changes: 11 additions & 1 deletion bi_sql_editor/models/bi_sql_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from odoo import _, api, fields, models, SUPERUSER_ID
from odoo.exceptions import UserError
from odoo.tools import pycompat, sql
from odoo.tools import pycompat, sql, table_columns
from odoo.addons.base.models.ir_model import IrModel

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -654,3 +654,13 @@ def button_preview_sql_expression(self):
self.button_validate_sql_expression()
res = self._execute_sql_request()
raise UserError('\n'.join(map(lambda x: str(x), res[:100])))

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' in self._fields and\
'bi_sql_view_field_ids' in self._fields and\
model._name.startswith(self._model_prefix):
self.search([('model_name', '=', model._name)]
).bi_sql_view_field_ids.adjust_manual_fields(model)
20 changes: 20 additions & 0 deletions bi_sql_editor/models/bi_sql_view_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions bi_sql_editor/models/ir_model.py
Original file line number Diff line number Diff line change
@@ -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(IrModelFields, self)._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)
2 changes: 2 additions & 0 deletions bi_sql_editor/views/view_bi_sql_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<field name="field_description"/>
<field name="ttype" attrs="{
'required': [('field_description', '!=', False)]}"/>
<field name='group_operator' attrs="{
'invisible': ['!', ('ttype', 'in', ('float', 'integer'))]}"/>
<field name="many2one_model_id" attrs="{
'invisible': [('ttype', '!=', 'many2one')],
'required': [
Expand Down

0 comments on commit 11edfca

Please sign in to comment.