From b1f53deade2da546731a64423dd1ffd6b786a0b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 23 Mar 2012 18:14:18 +0100 Subject: [PATCH] [REF] Refactored code and API. TODO: more cleaning. bzr revid: tde@openerp.com-20120323171418-liiguk4juvqlwsks --- openerp/addons/base/base_needaction.py | 55 +++++++++++++++++--------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/openerp/addons/base/base_needaction.py b/openerp/addons/base/base_needaction.py index ae1dfacfedf..2888c6b6746 100644 --- a/openerp/addons/base/base_needaction.py +++ b/openerp/addons/base/base_needaction.py @@ -19,6 +19,7 @@ # ############################################################################## +import openerp.pooler as pooler from osv import osv, fields from tools.translate import _ @@ -84,30 +85,41 @@ class base_needaction(osv.osv): # need action relationship management #------------------------------------------------------ - def _link_users(self, cr, uid, ids, user_ids, context=None): + def _get_user_ids(self, cr, uid, ids, context=None): + """Given ids of model self._name, find the user_ids that have an action to perform""" if context is None: context = {} - needact_rel_obj = self.pool.get('base.needaction_users_rel') + action_obj = self.pool.get('base.needaction_users_rel') + action_ids = needact_rel_obj.search(cr, uid, [('res_model', '=', self._nanme), ('res_id', 'in', ids)], context=context) + actions = needact_rel_obj.read(cr, uid, action_ids, context=context) + return [action['user_id'] for action in actions] + + def _link_users(self, cr, uid, ids, user_ids, context=None): + """Given ids of model self._name, add user_ids to the relationship table""" + if context is None: + context = {} + action_obj = self.pool.get('base.needaction_users_rel') for id in ids: for user_id in user_ids: - needact_rel_obj.create(cr, uid, {'res_model': self._name, 'res_id': id, 'user_id': user_id}, context=context) + action_obj.create(cr, uid, {'res_model': self._name, 'res_id': id, 'user_id': user_id}, context=context) return True def _unlink_users(self, cr, uid, ids, context=None): + """Given ids of model self._name, delete all entries in the relationship table""" if context is None: context = {} - needact_rel_obj = self.pool.get('base.needaction_users_rel') - to_del_ids = needact_rel_obj.search(cr, uid, [('res_model', '=', self._name), ('res_id', 'in', ids)], context=context) - return needact_rel_obj.unlink(cr, uid, to_del_ids, context=context) + action_obj = self.pool.get('base.needaction_users_rel') + to_del_ids = action_obj.search(cr, uid, [('res_model', '=', self._name), ('res_id', 'in', ids)], context=context) + return action_obj.unlink(cr, uid, to_del_ids, context=context) def _update_users(self, cr, uid, ids, user_ids, context=None): + """Given ids of model self._name, update their entries in the relationship table to user_ids""" if context is None: context = {} # unlink old records self._unlink_users(cr, uid, ids, context=context) # link new records - for res_id in ids: - self._link_users(cr, uid, ids, user_ids, context=context) + self._link_users(cr, uid, ids, user_ids, context=context) return True #------------------------------------------------------ @@ -115,8 +127,7 @@ class base_needaction(osv.osv): #------------------------------------------------------ def get_needaction_user_ids(self, cr, uid, ids, context=None): - result = dict.fromkeys(ids, []) - return result + return dict.fromkeys(ids, []) def create(self, cr, uid, values, context=None): if context is None: @@ -151,14 +162,22 @@ class base_needaction(osv.osv): # Need action API #------------------------------------------------------ - def needaction_get_records_user_ids(self, cr, uid, ids, context=None): - """Find all users that have to perform an action related to - records, given their ids""" - all_user_ids = [] - needaction_user_ids = self.get_needaction_user_ids(cr, uid, ids, context=context) - for record_id, record_user_ids in needaction_user_ids.iteritems(): - all_user_ids += record_user_ids - return all_user_ids + @classmethod + def needaction_get_user_ids(cld, cr, uid, model, context=None): + """Given a model + get the user_ids that have to perform at least one action""" + requ_act_obj = self.pool.get('base.needaction_users_rel') + requ_act_ids = requ_act_obj.search(cr, uid, [('res_model', '=', model)], context=context) + requ_act_objs = requ_act_obj.read(cr, uid, requ_act_ids, context=context) + return list(set([requ_act_obj['user_id'] for requ_act_obj in requ_act_objs])) + + @classmethod + def needaction_get_action_count(cls, cr, uid, model, user_id, context=None): + """Given a model and a user_id + get the number of actions it has to perform""" + print cls._name + requ_act_obj = pooler.get_pool(cr.dbname).get('base.needaction_users_rel') + return requ_act_obj.search(cr, uid, [('res_model', '=', model), ('user_id', '=', user_id)], count=True, context=context) def _needaction_get_user_table_ids(self, cr, uid, ids, user_id, offset=0, limit=None, order=None, count=False, context=None): """General method