From 97492a12a9f0975b80ebd43422aeebfb315dacb2 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 25 May 2016 12:34:20 +0200 Subject: [PATCH] [IMP] gamification: prevent misconfiguration Prevent selecting wrong field or models or computed fields Fixes #8545 --- addons/gamification/i18n/gamification.pot | 44 +++++++++++++++++++++-- addons/gamification/models/goal.py | 17 +++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/addons/gamification/i18n/gamification.pot b/addons/gamification/i18n/gamification.pot index 62def93f0e8..b722d230be8 100644 --- a/addons/gamification/i18n/gamification.pot +++ b/addons/gamification/i18n/gamification.pot @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 8.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-01-21 14:08+0000\n" -"PO-Revision-Date: 2015-01-21 14:08+0000\n" +"POT-Creation-Date: 2016-05-25 12:58+0000\n" +"PO-Revision-Date: 2016-05-25 12:58+0000\n" "Last-Translator: <>\n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -562,6 +562,18 @@ msgstr "" msgid "Display Mode" msgstr "" +#. module: gamification +#: field:gamification.badge,display_name:0 +#: field:gamification.badge.user,display_name:0 +#: field:gamification.badge.user.wizard,display_name:0 +#: field:gamification.challenge,display_name:0 +#: field:gamification.challenge.line,display_name:0 +#: field:gamification.goal,display_name:0 +#: field:gamification.goal.definition,display_name:0 +#: field:gamification.goal.wizard,display_name:0 +msgid "Display Name" +msgstr "" + #. module: gamification #: field:gamification.goal.definition,display_mode:0 msgid "Displayed as" @@ -967,6 +979,18 @@ msgstr "" msgid "Last Message Date" msgstr "" +#. module: gamification +#: field:gamification.badge,__last_update:0 +#: field:gamification.badge.user,__last_update:0 +#: field:gamification.badge.user.wizard,__last_update:0 +#: field:gamification.challenge,__last_update:0 +#: field:gamification.challenge.line,__last_update:0 +#: field:gamification.goal,__last_update:0 +#: field:gamification.goal.definition,__last_update:0 +#: field:gamification.goal.wizard,__last_update:0 +msgid "Last Modified on" +msgstr "" + #. module: gamification #: field:gamification.challenge,last_report_date:0 msgid "Last Report Date" @@ -1585,6 +1609,22 @@ msgstr "" msgid "The maximum number of time this badge can be sent per month per person." msgstr "" +#. module: gamification +#: code:addons/gamification/models/goal.py:160 +#, python-format +msgid "The model configuration for the definition %s seems incorrect, please check it.\n" +"\n" +"%s not found" +msgstr "" + +#. module: gamification +#: code:addons/gamification/models/goal.py:158 +#, python-format +msgid "The model configuration for the definition %s seems incorrect, please check it.\n" +"\n" +"%s not stored" +msgstr "" + #. module: gamification #: help:gamification.goal.definition,model_id:0 msgid "The model object for the field to evaluate" diff --git a/addons/gamification/models/goal.py b/addons/gamification/models/goal.py index ad37762d10b..a63431a83c4 100644 --- a/addons/gamification/models/goal.py +++ b/addons/gamification/models/goal.py @@ -148,18 +148,31 @@ class gamification_goal_definition(osv.Model): raise osv.except_osv(_('Error!'),_("The domain for the definition %s seems incorrect, please check it.\n\n%s" % (definition.name, msg))) return True + def _check_model_validity(self, cr, uid, ids, context=None): + """ make sure the selected field and model are usable""" + for definition in self.browse(cr, uid, ids, context=context): + try: + model = self.pool[definition.model_id.model] + field = model._fields[definition.field_id.name] + if not field.store: + raise UserError(_("The model configuration for the definition %s seems incorrect, please check it.\n\n%s not stored") % (definition.name, definition.field_id.name)) + except KeyError, e: + raise UserError(_("The model configuration for the definition %s seems incorrect, please check it.\n\n%s not found") % (definition.name, e.message)) + def create(self, cr, uid, vals, context=None): res_id = super(gamification_goal_definition, self).create(cr, uid, vals, context=context) if vals.get('computation_mode') in ('count', 'sum'): self._check_domain_validity(cr, uid, [res_id], context=context) - + if vals.get('field_id'): + self._check_model_validity(cr, uid, [res_id], context=context) return res_id def write(self, cr, uid, ids, vals, context=None): res = super(gamification_goal_definition, self).write(cr, uid, ids, vals, context=context) if vals.get('computation_mode', 'count') in ('count', 'sum') and (vals.get('domain') or vals.get('model_id')): self._check_domain_validity(cr, uid, ids, context=context) - + if vals.get('field_id') or vals.get('model_id') or vals.get('batch_mode'): + self._check_model_validity(cr, uid, ids, context=context) return res def on_change_model_id(self, cr, uid, ids, model_id, context=None):