[IMP] compute with python for goals

bzr revid: mat@openerp.com-20130415145743-mme15smubk14dvg4
This commit is contained in:
Martin Trigaux 2013-04-15 16:57:43 +02:00
parent b4d1d803fd
commit d02eed5d36
5 changed files with 32 additions and 19 deletions

View File

@ -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)"
}

View File

@ -100,12 +100,12 @@
</div>
</div>
</group>
<group string="Rules for Automatic Granting">
<!-- <group string="Rules for Automatic Granting">
<field name="rule_automatic" widget="radio"/>
<field name="compute_code" attrs="{'invisible': [('rule_automatic','!=','python')]}"/>
<field name="goal_type_ids" attrs="{'invisible': [('rule_automatic','!=','goals')]}" widget="many2many_tags"/>
</group>
<group string="Statistics">
--> <group string="Statistics">
<group>
<field name="stat_count"/>
<field name="stat_this_month"/>
@ -116,6 +116,9 @@
<field name="stat_my_this_month"/>
</group>
</group>
<group string="Reward for Challenges">
<field name="plan_ids" widget="many2many_kanban" nolabel="1" />
</group>
</sheet>
</form>
</field>

View File

@ -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

View File

@ -240,10 +240,11 @@
<field widget="radio" name="computation_mode"/>
<!-- Hide the fields below if manually -->
<field name="model_id" attrs="{'invisible':[('computation_mode','=','manually')], 'required':[('computation_mode','!=','manually')]}" class="oe_inline"/>
<field name="model_id" attrs="{'invisible':[('computation_mode','not in',('sum', 'count'))], 'required':[('computation_mode','in',('sum', 'count'))]}" class="oe_inline"/>
<field name="field_id" attrs="{'invisible':[('computation_mode','!=','sum')], 'required':[('computation_mode','=','sum')]}" domain="[('model_id','=',model_id)]" class="oe_inline"/>
<field name="field_date_id" attrs="{'invisible':[('computation_mode','=','manually')]}" domain="[('ttype', 'in', ('date', 'datetime')), ('model_id','=',model_id)]" class="oe_inline"/>
<field name="domain" attrs="{'invisible':[('computation_mode','=','manually')], 'required':[('computation_mode','!=','manually')]}" class="oe_inline"/>
<field name="field_date_id" attrs="{'invisible':[('computation_mode','not in',('sum', 'count'))]}" domain="[('ttype', 'in', ('date', 'datetime')), ('model_id','=',model_id)]" class="oe_inline"/>
<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')]}"/>
<field name="condition" widget="radio"/>
<field name="ponctual" />

View File

@ -433,7 +433,6 @@
<record id="goal_plan_list_action2" model="ir.actions.act_window">
<field name="inherit_id" ref="gamification.goal_plan_list_action"/>
<field name="name">Challenges</field>
<field name="res_model">gamification.goal.plan</field>
<field name="view_mode">kanban,tree,form</field>