[REF] Refactored code and API. TODO: more cleaning.
bzr revid: tde@openerp.com-20120323171418-liiguk4juvqlwsks
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user