[ADD] function for compute badge counts

bzr revid: mat@openerp.com-20130311110423-d1g44gngitmrxull
This commit is contained in:
Martin Trigaux 2013-03-11 12:04:23 +01:00
parent a8ff1f88b1
commit d8c8475949
4 changed files with 93 additions and 34 deletions

View File

@ -23,6 +23,7 @@ from openerp.osv import fields, osv
from openerp import tools
from templates import TemplateHelper
from datetime import date
class gamification_badge_user(osv.Model):
@ -34,7 +35,7 @@ class gamification_badge_user(osv.Model):
_columns = {
'employee_id': fields.many2one("hr.employee", string='Employee', required=True),
'user_id': fields.related('employee_id', 'user_id', string="User"),
'badge_ids': fields.many2many('gamification.badge', 'rel_badge_users', string='Badge'), # or many2one ??
'badge_id': fields.many2one('gamification.badge', string='Badge'), # or many2one ??
}
@ -50,15 +51,63 @@ class gamification_badge(osv.Model):
result[obj.id] = tools.image_get_resized_images(obj.image)
return result
def _get_global_count(self, cr, uid, ids, name, args, context=None):
"""Return the number of time this badge has been granted"""
result = dict.fromkeys(ids, False)
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = len(self.pool.get('gamification.badge.user').search(
cr, uid, [('badge_id', '=', obj.id)], context=context))
return result
def _get_unique_global_count(self, cr, uid, ids, name, args, context=None):
"""Return the number of time this badge has been granted to individual users"""
result = dict.fromkeys(ids, False)
for obj in self.browse(cr, uid, ids, context=context):
res = self.pool.get('gamification.badge.user').read_group(
cr, uid, domain=[('badge_id', '=', obj.id)],
fields=['badge_id', 'employee_id'],
groupby=['employee_id'], context=context)
result[obj.id] = len(res)
return result
def _get_month_count(self, cr, uid, ids, name, args, context=None):
"""Return the number of time this badge has been granted this month"""
result = dict.fromkeys(ids, False)
first_month_day = date.today().replace(day=1).isoformat()
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = len(self.pool.get('gamification.badge.user').search(
cr, uid, [('badge_id', '=', obj.id),
('create_date', '>=', first_month_day)], context=context))
return result
def _get_global_my_count(self, cr, uid, ids, name, args, context=None):
"""Return the number of time this badge has been granted to the current user"""
result = dict.fromkeys(ids, False)
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = len(self.pool.get('gamification.badge.user').search(
cr, uid, [('badge_id', '=', obj.id), ('user_id', '=', uid)],
context=context))
return result
def _get_month_my_count(self, cr, uid, ids, name, args, context=None):
"""Return the number of time this badge has been granted to the current user this month"""
result = dict.fromkeys(ids, False)
first_month_day = date.today().replace(day=1).isoformat()
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = len(self.pool.get('gamification.badge.user').search(
cr, uid, [('badge_id', '=', obj.id), ('user_id', '=', uid)
('create_date', '>=', first_month_day)], context=context))
return result
_columns = {
'name': fields.char('Badge', required=True, translate=True),
'description': fields.text('Description'),
'image' : fields.binary("Image",
'image': fields.binary("Image",
help="This field holds the image used for the badge, limited to 256x256"),
# image_select: selection with a on_change to fill image with predefined picts
'rule_auth': fields.selection([
('everyone', 'Everyone'),
('list', 'A selected list of users'),
('users', 'A selected list of users'),
('having', 'People having some badges'),
('computed', 'Nobody, Computed'),
],
@ -76,24 +125,31 @@ class gamification_badge(osv.Model):
help="This badge can not be send indefinitely"),
'rule_max_number': fields.integer('Limitation Number',
help="The maximum number of time this badge can be sent per month."),
'compute_code': fields.text('Compute Code',
help="The python code that will be executed to verify if a user can receive this badge."),
'goal_type_ids': fields.many2many('gamification.goal.type',
string='Goals Linked',
help="The users that have succeeded theses goals will receive automatically the badge."),
'public': fields.boolean('Public',
help="A message will be posted on the user profile or just sent to him"),
'owner_ids': fields.many2many('gamification.badge.user', 'rel_badge_users',
string='Owners',
help='The list of users having receive this badge'),
'stat_count': fields.integer('Total Count',
'stat_count': fields.function(_get_global_count, string='Total',
help="The number of time this badge has been received."),
'stat_count_distinct': fields.integer('Uniaue Count',
help="The number of time this badge has been received by individual users."),
'stat_this_month': fields.integer('Monthly Count',
'stat_count_distinct': fields.function(_get_unique_global_count,
string='Unique Count',
help="The number of time this badge has been received by individual employees."),
'stat_this_month': fields.function(_get_month_count,
string='Monthly Count',
help="The number of time this badge has been received this month."),
# stat_my
# stat_my_this_month
'compute_code': fields.text('Compute Code',
help="The python code that will be executed to verify if a user can receive this badge.")
'stat_my': fields.function(_get_global_my_count, string='My Total',
help="The number of time the current user has received this badge."),
'stat_my_this_month': fields.function(_get_month_my_count,
string='My Monthly Total',
help="The number of time the current user has received this badge this month."),
}
_default = {

View File

@ -25,6 +25,7 @@
<tree string="Badge List">
<field name="name"/>
<field name="stat_count"/>
<field name="stat_my"/>
<field name="rule_auth"/>
</tree>
</field>
@ -49,11 +50,11 @@
<field name="description" />
<group string="Rules">
<field name="rule_auth"/>
<field name="rule_auth_user_ids"/>
<field name="rule_auth_badge_ids"/>
<field name="rule_auth_user_ids" attrs="{'invisible': [('rule_auth','!=','users')]}"/>
<field name="rule_auth_badge_ids" attrs="{'invisible': [('rule_auth','!=','having')]}"/>
<field name="compute_code" attrs="{'invisible': [('rule_auth','!=','computed')]}"/>
<field name="rule_max"/>
<field name="rule_max_number"/>
<field name="compute_code"/>
<field name="rule_max_number" attrs="{'invisible': [('rule_max','=',False)]}"/>
</group>
</sheet>
</form>

View File

@ -40,11 +40,12 @@ class gamification_goal_type(osv.Model):
_columns = {
'name': fields.char('Type Name', required=True, translate=True),
'description': fields.text('Description'),
'unit': fields.char('Unit', help="The unit of the target and current values", translate=True),
'unit': fields.char('Unit',
help="The unit of the target and current values", translate=True),
'computation_mode': fields.selection([
('sum','Sum'),
('count','Count'),
('manually','Manually')
('sum', 'Sum'),
('count', 'Count'),
('manually', 'Manually')
],
string="Mode of Computation",
help="""How is computed the goal value :\n
@ -54,24 +55,24 @@ class gamification_goal_type(osv.Model):
required=True),
'model_id': fields.many2one('ir.model',
string='Model',
help='The model object for the field to evaluate' ),
help='The model object for the field to evaluate'),
'field_id': fields.many2one('ir.model.fields',
string='Evaluated Field',
help='The field containing the value to evaluate' ),
help='The field containing the value to evaluate'),
'field_date_id': fields.many2one('ir.model.fields',
string='Evaluated Date Field',
help='The date to use for the time period evaluated'),
'domain': fields.char("Domain",
help="Technical filters rules to apply",
required=True), # how to apply it ?
'condition' : fields.selection([
('lower','<='),
('higher','>=')
'condition': fields.selection([
('lower', '<='),
('higher', '>=')
],
string='Validation Condition',
help='A goal is considered as completed when the current value is compared to the value to reach',
required=True),
'sequence' : fields.integer('Sequence',
'sequence': fields.integer('Sequence',
help='Sequence number for ordering',
required=True),
}
@ -144,7 +145,7 @@ class gamification_goal(osv.Model):
],
string='State',
required=True,
track_visibility = 'always'),
track_visibility='always'),
'computation_mode': fields.related('type_id','computation_mode',
type='char',
@ -153,7 +154,7 @@ class gamification_goal(osv.Model):
help="The number of days after which the user assigned to a manual goal will be reminded. Never reminded if no value is specified."),
'last_update' : fields.date('Last Update',
help="In case of manual goal, reminders are sent if the goal as not been updated for a while (defined in goal plan). Ignored in case of non-manual goal or goal not linked to a plan."),
'type_description': fields.related('type_id','description',
type='char',
string='Type Description'),
@ -207,12 +208,12 @@ class gamification_goal(osv.Model):
partner_ids=[goal.user_id.partner_id.id],
context=context,
subtype='mail.mt_comment')
else: # count or sum
else: # count or sum
obj = self.pool.get(goal.type_id.model_id.model)
field_date_name = goal.type_id.field_date_id.name
domain = safe_eval(goal.type_id.domain,
domain = safe_eval(goal.type_id.domain,
{'user_id': goal.user_id.id})
if goal.start_date:
domain.append((field_date_name, '>=', goal.start_date))
@ -224,11 +225,11 @@ class gamification_goal(osv.Model):
res = obj.read_group(cr, uid, domain, [field_name],
[''], context=context)
towrite = {'current': res[0][field_name]}
else: # computation mode = count
else: # computation mode = count
res = obj.search(cr, uid, domain, context=context)
towrite = {'current': len(res)}
# check goal target reached
if (goal.type_id.condition == 'higher' \
and towrite['current'] >= goal.target_goal) \

View File

@ -1,5 +1,6 @@
from openerp.osv import osv
class res_users_gamification_group(osv.Model):
""" Update of res.users class
- if adding groups to an user, check gamification.goal.plan linked to