From 122c15c48d430c502adf2df45bad4405a864b5ce Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 17 Dec 2013 17:15:41 +0100 Subject: [REF] gamification: pretty much changing half of the code to make tde happy... bzr revid: mat@openerp.com-20131217161541-oxsgy7gmko2x6qui --- addons/gamification/models/res_users.py | 177 ++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 addons/gamification/models/res_users.py (limited to 'addons/gamification/models/res_users.py') diff --git a/addons/gamification/models/res_users.py b/addons/gamification/models/res_users.py new file mode 100644 index 00000000000..38e784247fd --- /dev/null +++ b/addons/gamification/models/res_users.py @@ -0,0 +1,177 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2013 OpenERP SA () +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# +############################################################################## + +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.challenge linked to + this group, and the user. This is done by overriding the write method. + """ + _name = 'res.users' + _inherit = ['res.users'] + + def write(self, cr, uid, ids, vals, context=None): + 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, uid, [('autojoin_group_id', 'in', user_group_ids)], context=context) + if challenge_ids: + challenge_obj.write(cr, uid, challenge_ids, {'user_ids': [(4, user_id) for user_id in ids]}, context=context) + + if vals.get('image'): + goal_definition_id = self.pool.get('ir.model.data').get_object(cr, uid, 'gamification', 'definition_base_avatar', context) + goal_ids = self.pool.get('gamification.goal').search(cr, uid, [('definition_id', '=', goal_definition_id.id), ('user_id', 'in', ids)], context=context) + values = {'state': 'reached', 'current': 1} + self.pool.get('gamification.goal').write(cr, uid, goal_ids, values, context=context) + return write_res + + def get_goals_todo_info(self, cr, uid, context=None): + """Return the list of goals assigned to the user, grouped by challenge + + This method intends to return processable data in javascript in the + goal_list_to_do template. The output format is not constant as the + required information is different between individual and board goal + definitions + :return: list of dictionnaries for each goal to display + """ + all_goals_info = [] + challenge_obj = self.pool.get('gamification.challenge') + + challenge_ids = challenge_obj.search(cr, uid, [('user_ids', 'in', uid), ('state', '=', 'inprogress')], context=context) + for challenge in challenge_obj.browse(cr, uid, challenge_ids, context=context): + # serialize goals info to be able to use it in javascript + serialized_goals_info = { + 'id': challenge.id, + 'name': challenge.name, + 'visibility_mode': challenge.visibility_mode, + } + user = self.browse(cr, uid, uid, context=context) + serialized_goals_info['currency'] = user.company_id.currency_id.id + + if challenge.visibility_mode == 'board': + # board report should be grouped by line for all users + goals_info = challenge_obj.get_board_goal_info(cr, uid, challenge, subset_goal_ids=False, context=context) + + if len(goals_info) == 0: + # challenge with no valid lines + continue + + serialized_goals_info['lines'] = [] + for line_board in goals_info: + vals = {'definition_name': line_board['goal_definition'].name, + 'definition_description': line_board['goal_definition'].description, + 'definition_condition': line_board['goal_definition'].condition, + 'computation_mode': line_board['goal_definition'].computation_mode, + 'definition_monetary': line_board['goal_definition'].monetary, + 'definition_suffix': line_board['goal_definition'].suffix, + 'definition_action': True if line_board['goal_definition'].action_id else False, + 'definition_display': line_board['goal_definition'].display_mode, + 'target_goal': line_board['target_goal'], + 'goals': []} + for goal in line_board['board_goals']: + # Keep only the Top 3 and the current user + if goal[0] > 2 and goal[1].user_id.id != uid: + continue + + vals['goals'].append({ + 'rank': goal[0] + 1, + 'id': goal[1].id, + 'user_id': goal[1].user_id.id, + 'user_name': goal[1].user_id.name, + 'state': goal[1].state, + 'completeness': goal[1].completeness, + 'current': goal[1].current, + 'target_goal': goal[1].target_goal, + }) + if uid == goal[1].user_id.id: + vals['own_goal_id'] = goal[1].id + serialized_goals_info['lines'].append(vals) + + else: + # individual report are simply a list of goal + goals_info = challenge_obj.get_indivual_goal_info(cr, uid, uid, challenge, subset_goal_ids=False, context=context) + + if not goals_info: + continue + + serialized_goals_info['goals'] = [] + for goal in goals_info: + serialized_goals_info['goals'].append({ + 'id': goal.id, + 'definition_name': goal.definition_id.name, + 'definition_description': goal.definition_description, + 'definition_condition': goal.definition_id.condition, + 'definition_monetary': goal.definition_id.monetary, + 'definition_suffix': goal.definition_id.suffix, + 'definition_action': True if goal.definition_id.action_id else False, + 'definition_display': goal.definition_id.display_mode, + 'state': goal.state, + 'completeness': goal.completeness, + 'computation_mode': goal.computation_mode, + 'current': goal.current, + 'target_goal': goal.target_goal, + }) + + all_goals_info.append(serialized_goals_info) + return all_goals_info + + def get_challenge_suggestions(self, cr, uid, context=None): + """Return the list of challenges suggested to the user""" + challenge_info = [] + challenge_obj = self.pool.get('gamification.challenge') + challenge_ids = challenge_obj.search(cr, uid, [('invited_user_ids', 'in', uid), ('state', '=', 'inprogress')], context=context) + for challenge in challenge_obj.browse(cr, uid, challenge_ids, context=context): + values = { + 'id': challenge.id, + 'name': challenge.name, + 'description': challenge.description, + } + 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' + + def write(self, cr, uid, ids, vals, context=None): + 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, uid, [('autojoin_group_id', 'in', ids)], context=context) + if challenge_ids: + challenge_obj.write(cr, uid, challenge_ids, {'user_ids': [(4, user_id) for user_id in user_ids]}, context=context) + return write_res +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: -- cgit v1.2.3 From c184adab447610a8191fde58c522e881e9d9bbf3 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 17 Dec 2013 17:46:19 +0100 Subject: [REF] gamification bzr revid: mat@openerp.com-20131217164619-ewgj8cclza3u44pp --- addons/gamification/models/res_users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/gamification/models/res_users.py') diff --git a/addons/gamification/models/res_users.py b/addons/gamification/models/res_users.py index 38e784247fd..0f0144e0710 100644 --- a/addons/gamification/models/res_users.py +++ b/addons/gamification/models/res_users.py @@ -76,7 +76,7 @@ class res_users_gamification_group(osv.Model): # board report should be grouped by line for all users goals_info = challenge_obj.get_board_goal_info(cr, uid, challenge, subset_goal_ids=False, context=context) - if len(goals_info) == 0: + if not goals_info: # challenge with no valid lines continue -- cgit v1.2.3 From 4bc29b9ffae6177dfcc42c69216f29e482c0716e Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 17 Dec 2013 18:02:32 +0100 Subject: [REF] gamification bzr revid: mat@openerp.com-20131217170232-e0zruxzl1wlycaji --- addons/gamification/models/res_users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/gamification/models/res_users.py') diff --git a/addons/gamification/models/res_users.py b/addons/gamification/models/res_users.py index 0f0144e0710..ef544c95692 100644 --- a/addons/gamification/models/res_users.py +++ b/addons/gamification/models/res_users.py @@ -72,7 +72,7 @@ class res_users_gamification_group(osv.Model): user = self.browse(cr, uid, uid, context=context) serialized_goals_info['currency'] = user.company_id.currency_id.id - if challenge.visibility_mode == 'board': + if challenge.visibility_mode == 'ranking': # board report should be grouped by line for all users goals_info = challenge_obj.get_board_goal_info(cr, uid, challenge, subset_goal_ids=False, context=context) -- cgit v1.2.3 From b63950e0dc49220bed8e9e2c1a7f922f5300b490 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 18 Dec 2013 09:59:07 +0100 Subject: [REF] gamification: mode to adequate folders bzr revid: mat@openerp.com-20131218085907-6501sfdif2rk4a0b --- addons/gamification/models/res_users.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'addons/gamification/models/res_users.py') diff --git a/addons/gamification/models/res_users.py b/addons/gamification/models/res_users.py index ef544c95692..6c8ca7646b3 100644 --- a/addons/gamification/models/res_users.py +++ b/addons/gamification/models/res_users.py @@ -61,6 +61,7 @@ class res_users_gamification_group(osv.Model): all_goals_info = [] challenge_obj = self.pool.get('gamification.challenge') + user = self.browse(cr, uid, uid, context=context) challenge_ids = challenge_obj.search(cr, uid, [('user_ids', 'in', uid), ('state', '=', 'inprogress')], context=context) for challenge in challenge_obj.browse(cr, uid, challenge_ids, context=context): # serialize goals info to be able to use it in javascript @@ -68,9 +69,8 @@ class res_users_gamification_group(osv.Model): 'id': challenge.id, 'name': challenge.name, 'visibility_mode': challenge.visibility_mode, + 'currency': user.company_id.currency_id.id } - user = self.browse(cr, uid, uid, context=context) - serialized_goals_info['currency'] = user.company_id.currency_id.id if challenge.visibility_mode == 'ranking': # board report should be grouped by line for all users -- cgit v1.2.3 From e7887b8b8f892832c5b65b82358d022496a2729d Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 18 Dec 2013 12:12:43 +0100 Subject: [REF] gamification: python tests and other improvements bzr revid: mat@openerp.com-20131218111243-npnauv6mxhb4nttg --- addons/gamification/models/res_users.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'addons/gamification/models/res_users.py') diff --git a/addons/gamification/models/res_users.py b/addons/gamification/models/res_users.py index 6c8ca7646b3..d831ab2ea25 100644 --- a/addons/gamification/models/res_users.py +++ b/addons/gamification/models/res_users.py @@ -31,6 +31,7 @@ class res_users_gamification_group(osv.Model): _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]} @@ -41,12 +42,20 @@ class res_users_gamification_group(osv.Model): challenge_ids = challenge_obj.search(cr, uid, [('autojoin_group_id', 'in', user_group_ids)], context=context) if challenge_ids: challenge_obj.write(cr, uid, challenge_ids, {'user_ids': [(4, user_id) for user_id in ids]}, context=context) + return write_res - if vals.get('image'): - goal_definition_id = self.pool.get('ir.model.data').get_object(cr, uid, 'gamification', 'definition_base_avatar', context) - goal_ids = self.pool.get('gamification.goal').search(cr, uid, [('definition_id', '=', goal_definition_id.id), ('user_id', 'in', ids)], context=context) - values = {'state': 'reached', 'current': 1} - self.pool.get('gamification.goal').write(cr, uid, goal_ids, values, context=context) + 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, uid, [('autojoin_group_id', 'in', user_group_ids)], context=context) + if challenge_ids: + challenge_obj.write(cr, uid, challenge_ids, {'user_ids': [(4, write_res)]}, context=context) return write_res def get_goals_todo_info(self, cr, uid, context=None): @@ -174,4 +183,3 @@ class res_groups_gamification_group(osv.Model): if challenge_ids: challenge_obj.write(cr, uid, challenge_ids, {'user_ids': [(4, user_id) for user_id in user_ids]}, context=context) return write_res -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: -- cgit v1.2.3 From f77aefc07ace66587bc13f7d2fa55755968437c5 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Fri, 20 Dec 2013 15:45:21 +0100 Subject: [REF] gamification: cleaning methods and reports bzr revid: mat@openerp.com-20131220144521-wyu1gx8bcitu6nk4 --- addons/gamification/models/res_users.py | 99 ++++++++------------------------- 1 file changed, 23 insertions(+), 76 deletions(-) (limited to 'addons/gamification/models/res_users.py') diff --git a/addons/gamification/models/res_users.py b/addons/gamification/models/res_users.py index d831ab2ea25..408ddddf082 100644 --- a/addons/gamification/models/res_users.py +++ b/addons/gamification/models/res_users.py @@ -20,7 +20,7 @@ ############################################################################## from openerp.osv import osv - +from challenge import MAX_VISIBILITY_RANKING class res_users_gamification_group(osv.Model): """ Update of res.users class @@ -58,14 +58,23 @@ class res_users_gamification_group(osv.Model): challenge_obj.write(cr, uid, challenge_ids, {'user_ids': [(4, write_res)]}, context=context) return write_res - def get_goals_todo_info(self, cr, uid, context=None): - """Return the list of goals assigned to the user, grouped by challenge + # def get_goals_todo_info(self, cr, uid, context=None): + + def get_serialised_gamification_summary(self, cr, uid, context=None): + return self._serialised_goals_summary(cr, uid, user_id=uid, context=context) - This method intends to return processable data in javascript in the - goal_list_to_do template. The output format is not constant as the - required information is different between individual and board goal - definitions - :return: list of dictionnaries for each goal to display + def _serialised_goals_summary(self, cr, uid, user_id, context=None): + """Return a serialised list of goals assigned to the user, grouped by challenge + + [ + { + 'id': , + 'name': , + 'visibility_mode': , + 'currency': , + 'lines': [(see gamification_challenge._get_serialized_challenge_lines() format)] + }, + ] """ all_goals_info = [] challenge_obj = self.pool.get('gamification.challenge') @@ -74,78 +83,14 @@ class res_users_gamification_group(osv.Model): challenge_ids = challenge_obj.search(cr, uid, [('user_ids', 'in', uid), ('state', '=', 'inprogress')], context=context) for challenge in challenge_obj.browse(cr, uid, challenge_ids, context=context): # serialize goals info to be able to use it in javascript - serialized_goals_info = { + all_goals_info.append({ 'id': challenge.id, 'name': challenge.name, 'visibility_mode': challenge.visibility_mode, - 'currency': user.company_id.currency_id.id - } + 'currency': user.company_id.currency_id.id, + 'lines': challenge_obj._get_serialized_challenge_lines(cr, uid, challenge, user_id, restrict_top=MAX_VISIBILITY_RANKING, context=context), + }) - if challenge.visibility_mode == 'ranking': - # board report should be grouped by line for all users - goals_info = challenge_obj.get_board_goal_info(cr, uid, challenge, subset_goal_ids=False, context=context) - - if not goals_info: - # challenge with no valid lines - continue - - serialized_goals_info['lines'] = [] - for line_board in goals_info: - vals = {'definition_name': line_board['goal_definition'].name, - 'definition_description': line_board['goal_definition'].description, - 'definition_condition': line_board['goal_definition'].condition, - 'computation_mode': line_board['goal_definition'].computation_mode, - 'definition_monetary': line_board['goal_definition'].monetary, - 'definition_suffix': line_board['goal_definition'].suffix, - 'definition_action': True if line_board['goal_definition'].action_id else False, - 'definition_display': line_board['goal_definition'].display_mode, - 'target_goal': line_board['target_goal'], - 'goals': []} - for goal in line_board['board_goals']: - # Keep only the Top 3 and the current user - if goal[0] > 2 and goal[1].user_id.id != uid: - continue - - vals['goals'].append({ - 'rank': goal[0] + 1, - 'id': goal[1].id, - 'user_id': goal[1].user_id.id, - 'user_name': goal[1].user_id.name, - 'state': goal[1].state, - 'completeness': goal[1].completeness, - 'current': goal[1].current, - 'target_goal': goal[1].target_goal, - }) - if uid == goal[1].user_id.id: - vals['own_goal_id'] = goal[1].id - serialized_goals_info['lines'].append(vals) - - else: - # individual report are simply a list of goal - goals_info = challenge_obj.get_indivual_goal_info(cr, uid, uid, challenge, subset_goal_ids=False, context=context) - - if not goals_info: - continue - - serialized_goals_info['goals'] = [] - for goal in goals_info: - serialized_goals_info['goals'].append({ - 'id': goal.id, - 'definition_name': goal.definition_id.name, - 'definition_description': goal.definition_description, - 'definition_condition': goal.definition_id.condition, - 'definition_monetary': goal.definition_id.monetary, - 'definition_suffix': goal.definition_id.suffix, - 'definition_action': True if goal.definition_id.action_id else False, - 'definition_display': goal.definition_id.display_mode, - 'state': goal.state, - 'completeness': goal.completeness, - 'computation_mode': goal.computation_mode, - 'current': goal.current, - 'target_goal': goal.target_goal, - }) - - all_goals_info.append(serialized_goals_info) return all_goals_info def get_challenge_suggestions(self, cr, uid, context=None): @@ -171,7 +116,9 @@ class res_groups_gamification_group(osv.Model): _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]} -- cgit v1.2.3