[FIX] gamification: convert autojoin_group_id to a domain representing the list of users
bzr revid: mat@openerp.com-20140416151026-lmvyo8k0wo4hw9gn
This commit is contained in:
parent
fafeaff7db
commit
d79cbd3fb8
|
@ -2,7 +2,7 @@
|
|||
<openerp>
|
||||
<!-- Mail template is done in a NOUPDATE block
|
||||
so users can freely customize/delete them -->
|
||||
<data noupdate="0">
|
||||
<data noupdate="1">
|
||||
<!--Email template -->
|
||||
|
||||
<record id="email_template_goal_reminder" model="email.template">
|
||||
|
@ -164,7 +164,7 @@
|
|||
<field name="period">once</field>
|
||||
<field name="visibility_mode">personal</field>
|
||||
<field name="report_message_frequency">never</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')" />
|
||||
<field name="user_domain">[('groups_id', 'in', ref('base.group_user'))]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">other</field>
|
||||
</record>
|
||||
|
@ -174,7 +174,7 @@
|
|||
<field name="period">once</field>
|
||||
<field name="visibility_mode">personal</field>
|
||||
<field name="report_message_frequency">never</field>
|
||||
<field name="user_ids" eval="[(4, ref('base.user_root'))]" />
|
||||
<field name="user_domain">[('groups_id', 'in', ref('base.user_root'))]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">other</field>
|
||||
</record>
|
||||
|
|
|
@ -22,11 +22,13 @@
|
|||
from openerp import SUPERUSER_ID
|
||||
from openerp.osv import fields, osv
|
||||
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF
|
||||
from openerp.tools.safe_eval import safe_eval as eval
|
||||
from openerp.tools.translate import _
|
||||
|
||||
from datetime import date, datetime, timedelta
|
||||
import calendar
|
||||
import logging
|
||||
import functools
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
# display top 3 in ranking, could be db variable
|
||||
|
@ -115,6 +117,12 @@ class gamification_challenge(osv.Model):
|
|||
except ValueError:
|
||||
return False
|
||||
|
||||
def _get_challenger_users(self, cr, uid, domain, context=None):
|
||||
ref = functools.partial(self.pool['ir.model.data'].xmlid_to_res_id, cr, uid)
|
||||
user_domain = eval(domain, {'ref': ref})
|
||||
return self.pool['res.users'].search(cr, uid, user_domain, context=context)
|
||||
|
||||
|
||||
_order = 'end_date, start_date, name, id'
|
||||
_columns = {
|
||||
'name': fields.char('Challenge Name', required=True, translate=True),
|
||||
|
@ -131,9 +139,7 @@ class gamification_challenge(osv.Model):
|
|||
'user_ids': fields.many2many('res.users', 'user_ids',
|
||||
string='Users',
|
||||
help="List of users participating to the challenge"),
|
||||
'autojoin_group_id': fields.many2one('res.groups',
|
||||
string='Auto-subscription Group',
|
||||
help='Group of users whose members will be automatically added to user_ids once the challenge is started'),
|
||||
'user_domain': fields.char('User domain', help="Alternative to a list of users"),
|
||||
|
||||
'period': fields.selection([
|
||||
('once', 'Non recurring'),
|
||||
|
@ -213,12 +219,12 @@ class gamification_challenge(osv.Model):
|
|||
"""Overwrite the create method to add the user of groups"""
|
||||
|
||||
# add users when change the group auto-subscription
|
||||
if vals.get('autojoin_group_id'):
|
||||
new_group = self.pool.get('res.groups').browse(cr, uid, vals['autojoin_group_id'], context=context)
|
||||
if vals.get('user_domain'):
|
||||
user_ids = self._get_challenger_users(cr, uid, vals.get('user_domain'), context=context)
|
||||
|
||||
if not vals.get('user_ids'):
|
||||
vals['user_ids'] = []
|
||||
vals['user_ids'] += [(4, user.id) for user in new_group.users]
|
||||
vals['user_ids'] += [(4, user_id) for user_id in user_ids]
|
||||
|
||||
create_res = super(gamification_challenge, self).create(cr, uid, vals, context=context)
|
||||
|
||||
|
@ -234,23 +240,12 @@ class gamification_challenge(osv.Model):
|
|||
if isinstance(ids, (int,long)):
|
||||
ids = [ids]
|
||||
|
||||
# add users when change the group auto-subscription
|
||||
if vals.get('autojoin_group_id'):
|
||||
new_group = self.pool.get('res.groups').browse(cr, uid, vals['autojoin_group_id'], context=context)
|
||||
|
||||
if not vals.get('user_ids'):
|
||||
vals['user_ids'] = []
|
||||
vals['user_ids'] += [(4, user.id) for user in new_group.users]
|
||||
|
||||
if vals.get('state') == 'inprogress':
|
||||
# starting a challenge
|
||||
if not vals.get('autojoin_group_id'):
|
||||
# starting challenge, add users in autojoin group
|
||||
if not vals.get('user_ids'):
|
||||
vals['user_ids'] = []
|
||||
for challenge in self.browse(cr, uid, ids, context=context):
|
||||
if challenge.autojoin_group_id:
|
||||
vals['user_ids'] += [(4, user.id) for user in challenge.autojoin_group_id.users]
|
||||
for challenge in self.browse(cr, uid, ids, context=context):
|
||||
user_ids = self._get_challenger_users(cr, uid, challenge.user_domain, context=context)
|
||||
write_op = [(4, user_id) for user_id in user_ids]
|
||||
self.write(cr, uid, [challenge.id], {'user_ids': write_op}, context=context)
|
||||
self.message_subscribe_users(cr, uid, [challenge.id], user_ids, context=context)
|
||||
|
||||
self.generate_goals_from_challenge(cr, uid, ids, context=context)
|
||||
|
||||
|
@ -264,11 +259,6 @@ class gamification_challenge(osv.Model):
|
|||
|
||||
write_res = super(gamification_challenge, self).write(cr, uid, ids, vals, context=context)
|
||||
|
||||
# subscribe new users to the challenge
|
||||
if vals.get('user_ids'):
|
||||
# done with browse after super if changes in groups
|
||||
for challenge in self.browse(cr, uid, ids, context=context):
|
||||
self.message_subscribe_users(cr, uid, [challenge.id], [user.id for user in challenge.user_ids], context=context)
|
||||
|
||||
return write_res
|
||||
|
||||
|
@ -325,9 +315,16 @@ class gamification_challenge(osv.Model):
|
|||
goal_obj.update(cr, uid, goal_ids, context=context)
|
||||
|
||||
for challenge in self.browse(cr, uid, ids, context=context):
|
||||
if challenge.autojoin_group_id:
|
||||
# check in case of new users in challenge, this happens if manager removed users in challenge manually
|
||||
self.write(cr, uid, [challenge.id], {'user_ids': [(4, user.id) for user in challenge.autojoin_group_id.users]}, context=context)
|
||||
# in case of new users matching the domain
|
||||
old_user_ids = [user.id for user in challenge.user_ids]
|
||||
new_user_ids = self._get_challenger_users(cr, uid, challenge.user_domain, context=context)
|
||||
to_remove_ids = list(set(old_user_ids) - set(new_user_ids))
|
||||
to_add_ids = list(set(new_user_ids) - set(old_user_ids))
|
||||
|
||||
write_op = [(3, user_id) for user_id in to_remove_ids]
|
||||
write_op += [(4, user_id) for user_id in to_add_ids]
|
||||
self.write(cr, uid, [challenge.id], {'user_ids': write_op}, context=context)
|
||||
|
||||
self.generate_goals_from_challenge(cr, uid, [challenge.id], context=context)
|
||||
|
||||
# goals closed but still opened at the last report date
|
||||
|
|
|
@ -31,36 +31,6 @@ class res_users_gamification_group(osv.Model):
|
|||
_name = 'res.users'
|
||||
_inherit = ['res.users']
|
||||
|
||||
def write(self, cr, uid, ids, vals, context=None):
|
||||
"""Overwrite to autosubscribe users if added to a group marked as autojoin, user will be added to challenge"""
|
||||
write_res = super(res_users_gamification_group, self).write(cr, uid, ids, vals, context=context)
|
||||
if vals.get('groups_id'):
|
||||
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
|
||||
user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
|
||||
user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
|
||||
|
||||
challenge_obj = self.pool.get('gamification.challenge')
|
||||
challenge_ids = challenge_obj.search(cr, SUPERUSER_ID, [('autojoin_group_id', 'in', user_group_ids)], context=context)
|
||||
if challenge_ids:
|
||||
challenge_obj.write(cr, SUPERUSER_ID, challenge_ids, {'user_ids': [(4, user_id) for user_id in ids]}, context=context)
|
||||
challenge_obj.generate_goals_from_challenge(cr, SUPERUSER_ID, challenge_ids, context=context)
|
||||
return write_res
|
||||
|
||||
def create(self, cr, uid, vals, context=None):
|
||||
"""Overwrite to autosubscribe users if added to a group marked as autojoin, user will be added to challenge"""
|
||||
write_res = super(res_users_gamification_group, self).create(cr, uid, vals, context=context)
|
||||
if vals.get('groups_id'):
|
||||
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
|
||||
user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
|
||||
user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
|
||||
|
||||
challenge_obj = self.pool.get('gamification.challenge')
|
||||
challenge_ids = challenge_obj.search(cr, SUPERUSER_ID, [('autojoin_group_id', 'in', user_group_ids)], context=context)
|
||||
if challenge_ids:
|
||||
challenge_obj.write(cr, SUPERUSER_ID, challenge_ids, {'user_ids': [(4, write_res)]}, context=context)
|
||||
challenge_obj.generate_goals_from_challenge(cr, SUPERUSER_ID, challenge_ids, context=context)
|
||||
return write_res
|
||||
|
||||
def get_serialised_gamification_summary(self, cr, uid, excluded_categories=None, context=None):
|
||||
return self._serialised_goals_summary(cr, uid, user_id=uid, excluded_categories=excluded_categories, context=context)
|
||||
|
||||
|
@ -112,28 +82,3 @@ class res_users_gamification_group(osv.Model):
|
|||
}
|
||||
challenge_info.append(values)
|
||||
return challenge_info
|
||||
|
||||
|
||||
class res_groups_gamification_group(osv.Model):
|
||||
""" Update of res.groups class
|
||||
- if adding users from a group, check gamification.challenge linked to
|
||||
this group, and the user. This is done by overriding the write method.
|
||||
"""
|
||||
_name = 'res.groups'
|
||||
_inherit = 'res.groups'
|
||||
|
||||
# No need to overwrite create as very unlikely to be the value in the autojoin_group_id field
|
||||
def write(self, cr, uid, ids, vals, context=None):
|
||||
"""Overwrite to autosubscribe users if add users to a group marked as autojoin, these will be added to the challenge"""
|
||||
write_res = super(res_groups_gamification_group, self).write(cr, uid, ids, vals, context=context)
|
||||
if vals.get('users'):
|
||||
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
|
||||
user_ids = [command[1] for command in vals['users'] if command[0] == 4]
|
||||
user_ids += [id for command in vals['users'] if command[0] == 6 for id in command[2]]
|
||||
|
||||
challenge_obj = self.pool.get('gamification.challenge')
|
||||
challenge_ids = challenge_obj.search(cr, SUPERUSER_ID, [('autojoin_group_id', 'in', ids)], context=context)
|
||||
if challenge_ids:
|
||||
challenge_obj.write(cr, SUPERUSER_ID, challenge_ids, {'user_ids': [(4, user_id) for user_id in user_ids]}, context=context)
|
||||
challenge_obj.generate_goals_from_challenge(cr, SUPERUSER_ID, challenge_ids, context=context)
|
||||
return write_res
|
||||
|
|
|
@ -45,9 +45,9 @@
|
|||
<h1>
|
||||
<field name="name" placeholder="e.g. Monthly Sales Objectives"/>
|
||||
</h1>
|
||||
<label for="user_ids" class="oe_edit_only" string="Assign Challenge To"/>
|
||||
<label for="user_domain" class="oe_edit_only" string="Assign Challenge To"/>
|
||||
<div>
|
||||
<field name="user_ids" widget="many2many_tags" />
|
||||
<field name="user_domain" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -94,7 +94,6 @@
|
|||
</page>
|
||||
<page string="Advanced Options">
|
||||
<group string="Subscriptions">
|
||||
<field name="autojoin_group_id" />
|
||||
<field name="invited_user_ids" widget="many2many_tags" />
|
||||
</group>
|
||||
<group string="Notification Messages">
|
||||
|
|
|
@ -130,7 +130,7 @@
|
|||
<field name="name">Monthly Sales Targets</field>
|
||||
<field name="period">monthly</field>
|
||||
<field name="visibility_mode">ranking</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_sale_salesman')" />
|
||||
<field name="user_domain" eval="[('groups_id', 'in', ref('base.group_sale_salesman'))]" />
|
||||
<field name="report_message_frequency">weekly</field>
|
||||
</record>
|
||||
|
||||
|
@ -138,7 +138,7 @@
|
|||
<field name="name">Lead Acquisition</field>
|
||||
<field name="period">monthly</field>
|
||||
<field name="visibility_mode">ranking</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_sale_salesman')" />
|
||||
<field name="user_domain" eval="[('groups_id', 'in', ref('base.group_sale_salesman'))]" />
|
||||
<field name="report_message_frequency">weekly</field>
|
||||
</record>
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
<field name="report_message_frequency">never</field>
|
||||
<field name="reward_id" eval="ref('badge_a_1')" />
|
||||
<field name="reward_realtime">True</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')"/>
|
||||
<field name="user_domain">[('karma', '>', 0)]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">forum</field>
|
||||
</record>
|
||||
|
@ -62,7 +62,7 @@
|
|||
<field name="report_message_frequency">never</field>
|
||||
<field name="reward_id" eval="ref('badge_a_2')" />
|
||||
<field name="reward_realtime">True</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')"/>
|
||||
<field name="user_domain">[('karma', '>', 0)]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">forum</field>
|
||||
</record>
|
||||
|
@ -96,7 +96,7 @@
|
|||
<field name="report_message_frequency">never</field>
|
||||
<field name="reward_id" eval="ref('badge_a_3')" />
|
||||
<field name="reward_realtime">True</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')"/>
|
||||
<field name="user_domain">[('karma', '>', 0)]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">forum</field>
|
||||
</record>
|
||||
|
@ -130,7 +130,7 @@
|
|||
<field name="report_message_frequency">never</field>
|
||||
<field name="reward_id" eval="ref('badge_a_4')" />
|
||||
<field name="reward_realtime">True</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')"/>
|
||||
<field name="user_domain">[('karma', '>', 0)]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">forum</field>
|
||||
</record>
|
||||
|
@ -166,7 +166,7 @@
|
|||
<field name="report_message_frequency">never</field>
|
||||
<field name="reward_id" eval="ref('badge_a_5')" />
|
||||
<field name="reward_realtime">True</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')"/>
|
||||
<field name="user_domain">[('karma', '>', 0)]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">forum</field>
|
||||
</record>
|
||||
|
@ -200,7 +200,7 @@
|
|||
<field name="report_message_frequency">never</field>
|
||||
<field name="reward_id" eval="ref('badge_a_6')" />
|
||||
<field name="reward_realtime">True</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')" />
|
||||
<field name="user_domain">[('karma', '>', 0)]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">forum</field>
|
||||
</record>
|
||||
|
@ -276,7 +276,7 @@ for post in Post.browse(cr, uid, user_posts, context=context):
|
|||
<field name="report_message_frequency">never</field>
|
||||
<field name="reward_id" eval="ref('badge_a_8')" />
|
||||
<field name="reward_realtime">True</field>
|
||||
<field name="autojoin_group_id" eval="ref('base.group_user')"/>
|
||||
<field name="user_domain">[('karma', '>', 0)]</field>
|
||||
<field name="state">inprogress</field>
|
||||
<field name="category">forum</field>
|
||||
</record>
|
||||
|
|
Loading…
Reference in New Issue