From 02174099e2d8b6727751a06527e9ac6c057a0b2f Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 9 Dec 2015 16:31:06 +0100 Subject: [PATCH] [FIX] gamification: goal definition for models without inherits When attempting to define a goal using a model that does not have any `inherits` - which happens most the time-, it was not possible to choose the `Field to Sum` and the `Date Field`, due to the domain set in the view which is no longer working in 9.0. The domain is no longer working because: - The onchange result for a one2many for which the value is set to `[(6, 0, [])]` is now `[]`, while, in 8.0, the tuple with the command `6` and the empty list was preserved - the empty list `model_inherited_model_ids` is considered as `True` by py.js, and therefore it fallbacks to `model_inherited_model_ids[0]`, but as this is an empty list, it crashes. The solution proposed in this revision solves the issue, while making the domain applied on these fields less fragile. opw-658892 --- addons/gamification/models/goal.py | 14 +++++++++----- addons/gamification/views/goal.xml | 13 ++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/addons/gamification/models/goal.py b/addons/gamification/models/goal.py index 5934c9a445b..2903814f9ba 100644 --- a/addons/gamification/models/goal.py +++ b/addons/gamification/models/goal.py @@ -2,7 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from openerp import SUPERUSER_ID -from openerp.osv import fields, osv +from openerp.osv import fields, osv, expression from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF from openerp.tools.safe_eval import safe_eval from openerp.tools.translate import _ @@ -64,6 +64,8 @@ class gamification_goal_definition(osv.Model): 'model_id': fields.many2one('ir.model', string='Model', help='The model object for the field to evaluate'), + # model_inherited_model_ids can be removed in master. + # It was only used to force a domain in the form view which is now set by `on_change_model_id` 'model_inherited_model_ids': fields.related('model_id', 'inherited_model_ids', type="many2many", obj="ir.model", string="Inherited models", readonly="True"), 'field_id': fields.many2one('ir.model.fields', @@ -146,12 +148,14 @@ class gamification_goal_definition(osv.Model): return res def on_change_model_id(self, cr, uid, ids, model_id, context=None): - """Prefill field model_inherited_model_ids""" + """Force domain for the `field_id` and `field_date_id` fields""" if not model_id: - return {'value': {'model_inherited_model_ids': []}} + return {'domain': {'field_id': expression.FALSE_DOMAIN, 'field_date_id': expression.FALSE_DOMAIN}} model = self.pool['ir.model'].browse(cr, uid, model_id, context=context) - # format (6, 0, []) to construct the domain ('model_id', 'in', m and m[0] and m[0][2]) - return {'value': {'model_inherited_model_ids': [(6, 0, [m.id for m in model.inherited_model_ids])]}} + model_fields_domain = ['|', ('model_id', '=', model_id), ('model_id', 'in', model.inherited_model_ids.ids)] + model_date_fields_domain = expression.AND([[('ttype', 'in', ('date', 'datetime'))], model_fields_domain]) + return {'domain': {'field_id': model_fields_domain, 'field_date_id': model_date_fields_domain}} + class gamification_goal(osv.Model): """Goal instance for a user diff --git a/addons/gamification/views/goal.xml b/addons/gamification/views/goal.xml index 6c19e960cff..2b57c2aa071 100644 --- a/addons/gamification/views/goal.xml +++ b/addons/gamification/views/goal.xml @@ -246,18 +246,9 @@ - - + attrs="{'invisible':[('computation_mode','!=','sum')], 'required':[('computation_mode','=','sum')]}"/> +