summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorMartin Trigaux <mat@openerp.com>2013-02-22 12:44:21 +0100
committerMartin Trigaux <mat@openerp.com>2013-02-22 12:44:21 +0100
commit4c3ff4303a7efe8b22cd0e5817e4577f6032c830 (patch)
treeb067d372c2e228221c059b20ac6b64d2523210d3 /addons
parentbe75098490cb7b59664eaef2d299237eef231212 (diff)
[ADD] gamification: add autosubscription from groups
bzr revid: mat@openerp.com-20130222114421-dueg3178wdtv2gk8
Diffstat (limited to 'addons')
-rw-r--r--addons/gamification/__init__.py3
-rw-r--r--addons/gamification/goal.py25
-rw-r--r--addons/gamification/res_users.py45
-rw-r--r--addons/gamification/view/goal.xml2
4 files changed, 69 insertions, 6 deletions
diff --git a/addons/gamification/__init__.py b/addons/gamification/__init__.py
index 7c0f769d0b0..a406bf3a621 100644
--- a/addons/gamification/__init__.py
+++ b/addons/gamification/__init__.py
@@ -1 +1,2 @@
-import goal \ No newline at end of file
+import goal
+import res_users \ No newline at end of file
diff --git a/addons/gamification/goal.py b/addons/gamification/goal.py
index 95341d4a004..8861a5b6bb8 100644
--- a/addons/gamification/goal.py
+++ b/addons/gamification/goal.py
@@ -24,6 +24,8 @@ from openerp.tools.safe_eval import safe_eval
from datetime import date, timedelta
import calendar
+import itertools
+
class gamification_goal_type(osv.Model):
"""Goal type definition
@@ -112,7 +114,6 @@ class gamification_goal(osv.Model):
return {'value':{'type_id': False}}
goal_type = goal_type.browse(cr, uid, type_id, context=context)
ret = {'value' : {'computation_mode' : goal_type.computation_mode}}
- print(ret)
return ret
_columns = {
@@ -168,6 +169,7 @@ class gamification_goal(osv.Model):
"""Update every goal in progress"""
if not ids:
ids = self.search(cr, uid, [('state', 'in', ('inprogress','inprogress_update', 'reached'))])
+
return self.update(cr, uid, ids, context=context)
def update(self, cr, uid, ids, context=None):
@@ -247,11 +249,10 @@ class gamification_goal(osv.Model):
('user_id', '=', user_id),
('start_date', '=', start_date.isoformat())]
goal_ids = obj.search(cr, uid, domain, context=context)
- print(domain, goal_ids)
if len(goal_ids) > 0:
# already exist, skip
return True
- print("creating goal for", planline_id, user_id, start_date)
+
planline = self.pool.get('gamification.goal.planline').browse(cr, uid, planline_id, context)
values = {
'type_id':planline.type_id.id,
@@ -399,7 +400,7 @@ class gamification_goal_plan(osv.Model):
if not ids:
ids = self.search(cr, uid, [('state', '=', 'inprogress'),
('period', '!=', 'once')])
- print("_update_all plans", ids)
+
return self.generate_goals_from_plan(cr, uid, ids, context=context)
def action_start(self, cr, uid, ids, context=None):
@@ -478,6 +479,22 @@ class gamification_goal_plan(osv.Model):
return True
+
+ def plan_subscribe_users(self, cr, uid, ids, user_ids, context=None):
+ """ Add the following users to plans
+
+ :param ids: ids of plans to which the users will be added
+ :param user_ids: ids of the users to add"""
+
+ print("subscibe users", ids, user_ids)
+ for plan in self.browse(cr,uid, ids, context):
+ subscription = [user.id for user in plan.user_ids]
+ subscription.extend(user_ids)
+ unified_subscription = list(set(subscription))
+ self.write(cr, uid, ids, {'user_ids': [(4, uid) for uid in unified_subscription]}, context=context)
+ return True
+
+
class gamification_goal_planline(osv.Model):
"""Gamification goal planline
diff --git a/addons/gamification/res_users.py b/addons/gamification/res_users.py
new file mode 100644
index 00000000000..f2f54d9b324
--- /dev/null
+++ b/addons/gamification/res_users.py
@@ -0,0 +1,45 @@
+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
+ 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):
+ print("Overwrite res_users_gamification_group", ids)
+ 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]]
+
+ goal_plan_obj = self.pool.get('gamification.goal.plan')
+ plan_ids = goal_plan_obj.search(cr, uid, [('autojoin_group_id', 'in', user_group_ids)], context=context)
+
+ goal_plan_obj.plan_subscribe_users(cr, uid, plan_ids, ids, context=context)
+ return write_res
+
+class res_groups_gamification_group(osv.Model):
+ """ Update of res.groups class
+ - if adding users from a group, check gamification.goal.plan 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):
+ print("Overwrite res_groups_gamification_group", ids)
+ 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]]
+
+ goal_plan_obj = self.pool.get('gamification.goal.plan')
+ plan_ids = goal_plan_obj.search(cr, uid, [('autojoin_group_id', 'in', ids)], context=context)
+
+ goal_plan_obj.plan_subscribe_users(cr, uid, plan_ids, user_ids, context=context)
+ return write_res
diff --git a/addons/gamification/view/goal.xml b/addons/gamification/view/goal.xml
index a66af16d6a9..b871fd9cbf1 100644
--- a/addons/gamification/view/goal.xml
+++ b/addons/gamification/view/goal.xml
@@ -67,7 +67,7 @@
</group>
<group string="Data">
<field name="target_goal"/>
- <field name="current"/>
+ <field name="current" attrs="{'readonly':[('computation_mode','!=','manually')]}"/>
</group>
</group>
</sheet>