[REF] Cleaned model and code: removed classmethods, moved some code to needaction_users, cleaned models.
bzr revid: tde@openerp.com-20120403145426-gjhdwhxb44bme8mi
This commit is contained in:
parent
157506ef69
commit
aa0205c849
|
@ -662,9 +662,9 @@
|
|||
<menuitem action="ir_action_wizard" id="menu_ir_action_wizard" parent="base.next_id_6"/>
|
||||
|
||||
<!-- Needaction mechanism -->
|
||||
<record model="ir.ui.view" id="view_notification_tree">
|
||||
<field name="name">ir.needaction_users_rel.tree</field>
|
||||
<field name="model">ir.needaction_users_rel</field>
|
||||
<record model="ir.ui.view" id="view_ir_needaction_users_tree">
|
||||
<field name="name">ir.needaction_users.tree</field>
|
||||
<field name="model">ir.needaction_users</field>
|
||||
<field name="type">tree</field>
|
||||
<field name="sequence">10</field>
|
||||
<field name="arch" type="xml">
|
||||
|
@ -676,14 +676,14 @@
|
|||
</field>
|
||||
</record>
|
||||
|
||||
<record id="action_view_needaction_users_rel" model="ir.actions.act_window">
|
||||
<record id="action_view_needaction_users" model="ir.actions.act_window">
|
||||
<field name="name">Need action relationships</field>
|
||||
<field name="res_model">ir.needaction_users_rel</field>
|
||||
<field name="res_model">ir.needaction_users</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">tree,form</field>
|
||||
</record>
|
||||
|
||||
<menuitem id="menu_needaction_users_rel" name="Need actions" parent="base.menu_users" sequence="20" action="action_view_needaction_users_rel" groups="base.group_extended"/>
|
||||
<menuitem id="menu_needaction_users" name="Need actions" parent="base.menu_users" sequence="20" action="action_view_needaction_users" groups="base.group_extended"/>
|
||||
|
||||
<!-- Companies -->
|
||||
<menuitem id="menu_res_company_global"
|
||||
|
|
|
@ -24,9 +24,9 @@ from operator import itemgetter
|
|||
from osv import osv, fields
|
||||
from tools.translate import _
|
||||
|
||||
class ir_needaction_users_rel(osv.osv):
|
||||
class ir_needaction_users(osv.osv):
|
||||
'''
|
||||
ir_needaction_users_rel holds data related to the needaction
|
||||
ir_needaction_users holds data related to the needaction
|
||||
mechanism inside OpenERP. A needaction is characterized by:
|
||||
- res_model: model of the record requiring an action
|
||||
- res_id: ID of the record requiring an action
|
||||
|
@ -34,7 +34,7 @@ class ir_needaction_users_rel(osv.osv):
|
|||
has to perform the action
|
||||
'''
|
||||
|
||||
_name = 'ir.needaction_users_rel'
|
||||
_name = 'ir.needaction_users'
|
||||
_description = 'Needaction relationship table'
|
||||
_rec_name = 'id'
|
||||
_order = 'id desc'
|
||||
|
@ -46,6 +46,41 @@ class ir_needaction_users_rel(osv.osv):
|
|||
'user_id': fields.many2one('res.users', string='Related User',
|
||||
ondelete='cascade', select=1, required=True),
|
||||
}
|
||||
|
||||
def _get_users(self, cr, uid, res_ids, res_model, context=None):
|
||||
"""Given res_ids of res_model, get user_ids present in table"""
|
||||
if context is None:
|
||||
context = {}
|
||||
needact_ids = self.search(cr, uid, [('res_model', '=', res_model), ('res_id', 'in', res_ids)], context=context)
|
||||
return map(itemgetter('res_id'), self.read(cr, uid, needact_ids, context=context))
|
||||
|
||||
def create_users(self, cr, uid, res_ids, res_model, user_ids, context=None):
|
||||
"""Given res_ids of res_model, add user_ids to the relationship table"""
|
||||
if context is None:
|
||||
context = {}
|
||||
for res_id in res_ids:
|
||||
for user_id in user_ids:
|
||||
self.create(cr, uid, {'res_model': res_model, 'res_id': res_id, 'user_id': user_id}, context=context)
|
||||
return True
|
||||
|
||||
def unlink_users(self, cr, uid, res_ids, res_model, context=None):
|
||||
"""Given res_ids of res_model, delete all entries in the relationship table"""
|
||||
if context is None:
|
||||
context = {}
|
||||
to_del_ids = self.search(cr, uid, [('res_model', '=', res_model), ('res_id', 'in', res_ids)], context=context)
|
||||
return self.unlink(cr, uid, to_del_ids, context=context)
|
||||
|
||||
def update_users(self, cr, uid, res_ids, res_model, user_ids, context=None):
|
||||
"""Given res_ids of res_model, update their entries in the relationship table to user_ids"""
|
||||
# read current records
|
||||
cur_users = self._get_users(cr, uid, res_ids, res_model, context=context)
|
||||
if len(cur_users) == len(user_ids) and all([cur_user in user_ids for cur_user in cur_users]):
|
||||
return True
|
||||
# unlink old records
|
||||
self.unlink_users(cr, uid, res_ids, res_model, context=context)
|
||||
# link new records
|
||||
self.create_users(cr, uid, res_ids, res_model, user_ids, context=context)
|
||||
return True
|
||||
|
||||
|
||||
class ir_needaction(osv.osv):
|
||||
|
@ -57,7 +92,7 @@ class ir_needaction(osv.osv):
|
|||
validation by a manager, this mechanism allows to set a list of
|
||||
users asked to perform an action.
|
||||
|
||||
This class wraps a table (base.needaction_users_rel) that behaves
|
||||
This class wraps a table (ir.needaction_users) that behaves
|
||||
like a many2many field. However, no field is added to the model
|
||||
inheriting from base.needaction. The mixin class manages the low-level
|
||||
considerations of updating relationships. Every change made on the
|
||||
|
@ -85,50 +120,6 @@ class ir_needaction(osv.osv):
|
|||
_name = 'ir.needaction'
|
||||
_description = 'Need action of users on records API'
|
||||
|
||||
_columns = {
|
||||
}
|
||||
|
||||
#------------------------------------------------------
|
||||
# need action relationship management
|
||||
#------------------------------------------------------
|
||||
|
||||
def _get_users(self, cr, uid, ids, context=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
needact_obj = self.pool.get('ir.needaction_users_rel')
|
||||
needact_ids = needact_obj.search(cr, uid, [('res_model', '=', self._name), ('res_id', 'in', ids)], context=context)
|
||||
return needact_obj.read(cr, uid, needact_ids, context=context)
|
||||
|
||||
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 = {}
|
||||
needact_obj = self.pool.get('ir.needaction_users_rel')
|
||||
for id in ids:
|
||||
for user_id in user_ids:
|
||||
needact_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_obj = self.pool.get('ir.needaction_users_rel')
|
||||
to_del_ids = needact_obj.search(cr, uid, [('res_model', '=', self._name), ('res_id', 'in', ids)], context=context)
|
||||
return needact_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"""
|
||||
# read current records
|
||||
cur_needact_objs = self._get_users(cr, uid, ids, context=None)
|
||||
if len(cur_needact_objs) == len(user_ids) and all([cur_needact_obj['user_id'] in user_ids for cur_needact_obj in cur_needact_objs]):
|
||||
return True
|
||||
# unlink old records
|
||||
self._unlink_users(cr, uid, ids, context=context)
|
||||
# link new records
|
||||
self._link_users(cr, uid, ids, user_ids, context=context)
|
||||
return True
|
||||
|
||||
#------------------------------------------------------
|
||||
# Addon API
|
||||
#------------------------------------------------------
|
||||
|
@ -139,29 +130,32 @@ class ir_needaction(osv.osv):
|
|||
def create(self, cr, uid, values, context=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
needact_table_obj = self.pool.get('ir.needaction_users')
|
||||
# perform create
|
||||
obj_id = super(ir_needaction, self).create(cr, uid, values, context=context)
|
||||
# link user_ids
|
||||
needaction_user_ids = self.get_needaction_user_ids(cr, uid, [obj_id], context=context)
|
||||
self._link_users(cr, uid, [obj_id], needaction_user_ids[obj_id], context=context)
|
||||
needact_table_obj.create_users(cr, uid, [obj_id], self._name, needaction_user_ids[obj_id], context=context)
|
||||
return obj_id
|
||||
|
||||
def write(self, cr, uid, ids, values, context=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
needact_table_obj = self.pool.get('ir.needaction_users')
|
||||
# perform write
|
||||
write_res = super(ir_needaction, self).write(cr, uid, ids, values, context=context)
|
||||
# get and update user_ids
|
||||
needaction_user_ids = self.get_needaction_user_ids(cr, uid, ids, context=context)
|
||||
for id in ids:
|
||||
self._update_users(cr, uid, [id], needaction_user_ids[id], context=context)
|
||||
needact_table_obj.update_users(cr, uid, [id], self._name, needaction_user_ids[id], context=context)
|
||||
return write_res
|
||||
|
||||
def unlink(self, cr, uid, ids, context=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
# unlink user_ids
|
||||
self._unlink_users(cr, uid, ids, context=context)
|
||||
needact_table_obj = self.pool.get('ir.needaction_users')
|
||||
needact_table_obj.unlink_users(cr, uid, ids, self._name, context=context)
|
||||
# perform unlink
|
||||
return super(ir_needaction, self).unlink(cr, uid, ids, context=context)
|
||||
|
||||
|
@ -169,36 +163,33 @@ class ir_needaction(osv.osv):
|
|||
# Need action API
|
||||
#------------------------------------------------------
|
||||
|
||||
@classmethod
|
||||
def needaction_get_record_ids(cls, cr, uid, model_name, user_id, limit=80, context=None):
|
||||
"""Given a model and a user_id
|
||||
def needaction_get_record_ids(self, cr, uid, user_id, limit=80, context=None):
|
||||
"""Given the current model and a user_id
|
||||
get the number of actions it has to perform"""
|
||||
if context is None:
|
||||
context = {}
|
||||
need_act_obj = pooler.get_pool(cr.dbname).get('ir.needaction_users_rel')
|
||||
need_act_ids = need_act_obj.search(cr, uid, [('res_model', '=', model_name), ('user_id', '=', user_id)], limit=limit, context=context)
|
||||
return map(itemgetter('res_id'), need_act_obj.read(cr, uid, need_act_ids, context=context))
|
||||
needact_table_obj = self.pool.get('ir.needaction_users')
|
||||
needact_table_ids = needact_table_obj.search(cr, uid, [('res_model', '=', self._name), ('user_id', '=', user_id)], limit=limit, context=context)
|
||||
return map(itemgetter('res_id'), needact_table_obj.read(cr, uid, needact_table_ids, context=context))
|
||||
|
||||
@classmethod
|
||||
def needaction_get_action_count(cls, cr, uid, model_name, user_id, limit=80, context=None):
|
||||
"""Given a model and a user_id
|
||||
def needaction_get_action_count(self, cr, uid, user_id, limit=80, context=None):
|
||||
"""Given the current model and a user_id
|
||||
get the number of actions it has to perform"""
|
||||
if context is None:
|
||||
context = {}
|
||||
need_act_obj = pooler.get_pool(cr.dbname).get('ir.needaction_users_rel')
|
||||
return need_act_obj.search(cr, uid, [('res_model', '=', model_name), ('user_id', '=', user_id)], limit=limit, count=True, context=context)
|
||||
needact_table_obj = self.pool.get('ir.needaction_users')
|
||||
return needact_table_obj.search(cr, uid, [('res_model', '=', self._name), ('user_id', '=', user_id)], limit=limit, count=True, context=context)
|
||||
|
||||
@classmethod
|
||||
def needaction_get_record_references(cls, cr, uid, user_id, offset=None, limit=None, order=None, context=None):
|
||||
def needaction_get_record_references(self, cr, uid, user_id, offset=None, limit=None, order=None, context=None):
|
||||
"""For a given user_id, get all the records that asks this user to
|
||||
perform an action. Records are given as references, a list of
|
||||
tuples (model_name, record_id).
|
||||
This method is trans-model."""
|
||||
if context is None:
|
||||
context = {}
|
||||
need_act_obj = pooler.get_pool(cr.dbname).get('ir.needaction_users_rel')
|
||||
need_act_ids = need_act_obj.search(cr, uid, [('user_id', '=', user_id)], offset=offset, limit=limit, order=order, context=context)
|
||||
need_acts = need_act_obj.read(cr, uid, need_act_ids, context=context)
|
||||
return map(itemgetter('res_model', 'id'), need_acts)
|
||||
needact_table_obj = self.pool.get('ir.needaction_users')
|
||||
needact_table_ids = needact_table_obj.search(cr, uid, [('user_id', '=', user_id)], offset=offset, limit=limit, order=order, context=context)
|
||||
needact_records = needact_table_obj.read(cr, uid, needact_table_ids, context=context)
|
||||
return map(itemgetter('res_model', 'id'), needact_records)
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
|
@ -262,7 +262,7 @@ class ir_ui_menu(osv.osv):
|
|||
for menu in self.browse(cr, uid, ids, context=context):
|
||||
res[menu.id] = {}
|
||||
if menu.action and menu.action.type == 'ir.actions.act_window' and menu.action.res_model:
|
||||
menu_needaction_res = osv.osv.get_needaction_info(cr, uid, menu.action.res_model, uid, domain=menu.action.domain, context=context)
|
||||
menu_needaction_res = self.pool.get(menu.action.res_model).get_needaction_info(cr, uid, uid, domain=menu.action.domain, context=context)
|
||||
else:
|
||||
menu_needaction_res = [False, 0, ()]
|
||||
res[menu.id]['needaction_enabled'] = menu_needaction_res[0]
|
||||
|
|
|
@ -122,4 +122,4 @@
|
|||
"access_ir_mail_server_all","ir_mail_server","model_ir_mail_server",,1,0,0,0
|
||||
"access_ir_actions_todo_category","ir_actions_todo_category","model_ir_actions_todo_category","group_system",1,1,1,1
|
||||
"access_ir_actions_client","ir_actions_client all","model_ir_actions_client",,1,0,0,0
|
||||
"access_ir_needaction_users_rel","ir_needaction_users_rel all","model_ir_needaction_users_rel",,1,1,1,1
|
||||
"access_ir_needaction_users","ir_needaction_users","model_ir_needaction_users",,1,1,1,1
|
||||
|
|
|
|
@ -721,36 +721,6 @@ class BaseModel(object):
|
|||
},
|
||||
context=context
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_needaction_info(cr, uid, model_name, user_id, limit=None, order=None, domain=False, context=None):
|
||||
"""Base method for needaction mechanism
|
||||
- see ir.needaction for actual implementation
|
||||
- if the model uses the need action mechanism
|
||||
(hasattr(model_obj, 'needaction_get_record_ids')):
|
||||
- get the record ids on which the user has actions to perform
|
||||
- evaluate the menu domain
|
||||
- compose a new domain: menu domain, limited to ids of
|
||||
records requesting an action
|
||||
- count the number of records maching that domain, that
|
||||
is the number of actions the user has to perform
|
||||
- this method returns default values
|
||||
:param: model_name: the name of the model (ex: hr.holidays)
|
||||
:param: user_id: the id of user
|
||||
:return: [uses_needaction=True/False, needaction_uid_ctr=%d]
|
||||
"""
|
||||
model_obj = pooler.get_pool(cr.dbname).get(model_name)
|
||||
if hasattr(model_obj, 'needaction_get_record_ids'):
|
||||
ids = model_obj.needaction_get_record_ids(cr, uid, model_name, user_id, limit=8096, context=context)
|
||||
if not ids:
|
||||
return (True, 0, [])
|
||||
if domain:
|
||||
new_domain = eval(domain) + [('id', 'in', ids)]
|
||||
else:
|
||||
new_domain = [('id', 'in', ids)]
|
||||
return (True, model_obj.search(cr, uid, new_domain, limit=limit, order=order, count=True), ids)
|
||||
else:
|
||||
return (False, 0, [])
|
||||
|
||||
def view_init(self, cr, uid, fields_list, context=None):
|
||||
"""Override this method to do specific things when a view on the object is opened."""
|
||||
|
@ -4906,7 +4876,35 @@ class BaseModel(object):
|
|||
# backwards compatibility
|
||||
get_xml_id = get_external_id
|
||||
_get_xml_ids = _get_external_ids
|
||||
|
||||
|
||||
def get_needaction_info(self, cr, uid, user_id, limit=None, order=None, domain=False, context=None):
|
||||
"""Base method for needaction mechanism
|
||||
- see ir.needaction for actual implementation
|
||||
- if the model uses the need action mechanism
|
||||
(hasattr(model_obj, 'needaction_get_record_ids')):
|
||||
- get the record ids on which the user has actions to perform
|
||||
- evaluate the menu domain
|
||||
- compose a new domain: menu domain, limited to ids of
|
||||
records requesting an action
|
||||
- count the number of records maching that domain, that
|
||||
is the number of actions the user has to perform
|
||||
- this method returns default values
|
||||
:param: model_name: the name of the model (ex: hr.holidays)
|
||||
:param: user_id: the id of user
|
||||
:return: [uses_needaction=True/False, needaction_uid_ctr=%d]
|
||||
"""
|
||||
if hasattr(self, 'needaction_get_record_ids'):
|
||||
ids = self.needaction_get_record_ids(cr, uid, user_id, limit=8192, context=context)
|
||||
if not ids:
|
||||
return (True, 0, [])
|
||||
if domain:
|
||||
new_domain = eval(domain) + [('id', 'in', ids)]
|
||||
else:
|
||||
new_domain = [('id', 'in', ids)]
|
||||
return (True, self.search(cr, uid, new_domain, limit=limit, order=order, count=True), ids)
|
||||
else:
|
||||
return (False, 0, [])
|
||||
|
||||
# Transience
|
||||
def is_transient(self):
|
||||
""" Return whether the model is transient.
|
||||
|
|
Loading…
Reference in New Issue