From d02eed5d360fee8c24a7ca7d9d40295373b8292f Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Mon, 15 Apr 2013 16:57:43 +0200 Subject: [PATCH] [IMP] compute with python for goals bzr revid: mat@openerp.com-20130415145743-mme15smubk14dvg4 --- addons/gamification/badge.py | 13 ++---------- addons/gamification/badge_view.xml | 7 +++++-- addons/gamification/goal.py | 23 +++++++++++++++++++-- addons/gamification/goal_view.xml | 7 ++++--- addons/hr_evaluation/hr_evaluation_view.xml | 1 - 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/addons/gamification/badge.py b/addons/gamification/badge.py index cfa130b792f..11aebd51a00 100644 --- a/addons/gamification/badge.py +++ b/addons/gamification/badge.py @@ -161,7 +161,7 @@ class gamification_badge(osv.Model): ('everyone', 'Everyone'), ('users', 'A selected list of users'), ('having', 'People having some badges'), - ('nobody', 'No one'), + ('nobody', 'No one, assigned through challenges'), ], string="Allowed to Grant", help="Who can grant this badge", @@ -185,15 +185,7 @@ class gamification_badge(osv.Model): 'remaining_sending': fields.function(_remaining_sending_calc, type='integer', string='Remaining Sending Allowed', help="If a maxium is set"), - 'rule_automatic': fields.selection([ - ('goals', 'List of goals to reach'), - ('python', 'Custom python code executed'), - ('manual', 'Not automatic'), - ], - string="Automatic Rule", - help="Can this badge be automatically rewarded", - required=True), - 'challenge_ids': fields.one2many('gamification.goal.plan', 'reward_id', + 'plan_ids': fields.one2many('gamification.goal.plan', 'reward_id', string="Granted upon completion of"), 'compute_code': fields.char('Compute Code', @@ -234,7 +226,6 @@ class gamification_badge(osv.Model): 'stat_count_distinct': 0, 'stat_this_month': 0, 'rule_auth': 'everyone', - 'rule_automatic': 'manual', 'compute_code': "self.nobody(cr, uid, context)" } diff --git a/addons/gamification/badge_view.xml b/addons/gamification/badge_view.xml index ef0cb37c71d..f9e17bf2448 100644 --- a/addons/gamification/badge_view.xml +++ b/addons/gamification/badge_view.xml @@ -100,12 +100,12 @@ - + @@ -116,6 +116,9 @@ + + + diff --git a/addons/gamification/goal.py b/addons/gamification/goal.py index 0f62fa09c15..a6de0895d92 100644 --- a/addons/gamification/goal.py +++ b/addons/gamification/goal.py @@ -26,6 +26,10 @@ from templates import TemplateHelper from datetime import date, datetime, timedelta +import logging + +_logger = logging.getLogger(__name__) + class gamification_goal_type(osv.Model): """Goal type definition @@ -67,11 +71,13 @@ class gamification_goal_type(osv.Model): ('manually', 'Recorded manually'), ('count', 'Automatic: number of records'), ('sum', 'Automatic: sum on a field'), + ('python', 'Automatic: execute a specific Python code'), ], string="Computation Mode", required=True), - 'ponctual': fields.boolean("Ponctual Goal", - help="A non-numerical goal is displayed with only two states: TODO or Done."), + 'ponctual': fields.boolean("Non-numerical Goal", + help="Instead of a progress, the goal is displayed with only two states: TODO or Done."), + 'model_id': fields.many2one('ir.model', string='Model', help='The model object for the field to evaluate'), @@ -84,6 +90,9 @@ class gamification_goal_type(osv.Model): 'domain': fields.char("Filter Domain", help="Technical filters rules to apply", required=True), + 'compute_code': fields.char('Compute Code', + help="The name of the python method that will be executed to verify if a user can receive this badge."), + 'condition': fields.selection([ ('higher', 'The higher the better'), ('lower', 'The lower the better') @@ -238,6 +247,16 @@ class gamification_goal(osv.Model): context=context, subtype='mail.mt_comment') + elif goal.type_id.computation_mode == 'python': + values = {'cr': cr, 'uid': uid, 'context': context, 'self': self.pool.get('gamification.goal.type')} + result = safe_eval(goal.type_id.compute_code, values, {}) + + if type(result) == float or type(result) == int: + towrite = {'current': result} + else: + _logger.exception(_('Unvalid return content from the evaluation of %s' % str(goal.type_id.compute_code))) + # raise osv.except_osv(_('Error!'), _('Unvalid return content from the evaluation of %s' % str(goal.type_id.compute_code))) + else: # count or sum obj = self.pool.get(goal.type_id.model_id.model) field_date_name = goal.type_id.field_date_id.name diff --git a/addons/gamification/goal_view.xml b/addons/gamification/goal_view.xml index acd27b1a7a0..925edc76eb5 100644 --- a/addons/gamification/goal_view.xml +++ b/addons/gamification/goal_view.xml @@ -240,10 +240,11 @@ - + - - + + + diff --git a/addons/hr_evaluation/hr_evaluation_view.xml b/addons/hr_evaluation/hr_evaluation_view.xml index b3838707d68..e2a58e38230 100644 --- a/addons/hr_evaluation/hr_evaluation_view.xml +++ b/addons/hr_evaluation/hr_evaluation_view.xml @@ -433,7 +433,6 @@ - Challenges gamification.goal.plan kanban,tree,form