[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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -246,18 +246,9 @@
|
||||
<!-- Hide the fields below if manually -->
|
||||
<field name="model_id" class="oe_inline" on_change="on_change_model_id(model_id)"
|
||||
attrs="{'invisible':[('computation_mode','not in',('sum', 'count'))], 'required':[('computation_mode','in',('sum', 'count'))]}"/>
|
||||
<field name="model_inherited_model_ids" invisible="True" />
|
||||
<field name="field_id" class="oe_inline"
|
||||
attrs="{'invisible':[('computation_mode','!=','sum')], 'required':[('computation_mode','=','sum')]}"
|
||||
domain="['|',
|
||||
('model_id', '=', model_id),
|
||||
('model_id', 'in', model_inherited_model_ids and model_inherited_model_ids[0] and model_inherited_model_ids[0][2])]" />
|
||||
<field name="field_date_id" class="oe_inline" attrs="{'invisible':[('computation_mode','not in',('sum', 'count'))]}"
|
||||
domain="[
|
||||
('ttype', 'in', ('date', 'datetime')),
|
||||
'|',
|
||||
('model_id', '=', model_id),
|
||||
('model_id', 'in', model_inherited_model_ids and model_inherited_model_ids[0] and model_inherited_model_ids[0][2])]" />
|
||||
attrs="{'invisible':[('computation_mode','!=','sum')], 'required':[('computation_mode','=','sum')]}"/>
|
||||
<field name="field_date_id" class="oe_inline" attrs="{'invisible':[('computation_mode','not in',('sum', 'count'))]}"/>
|
||||
<field name="domain" attrs="{'invisible':[('computation_mode','not in',('sum', 'count'))], 'required':[('computation_mode','in',('sum', 'count'))]}" class="oe_inline"/>
|
||||
<field name="compute_code" attrs="{'invisible':[('computation_mode','!=','python')], 'required':[('computation_mode','=','python')]}" placeholder="e.g. result = pool.get('mail.followers').search(cr, uid, [('res_model', '=', 'mail.channel'), ('partner_id', '=', object.user_id.partner_id.id)], count=True, context=context)"/>
|
||||
<field name="condition" widget="radio"/>
|
||||
|
||||
Reference in New Issue
Block a user