[MERGE] Merged branch holding improvements and fixes for the 'need action' mechanism, such as needaction_record_ids remove on ir.ui.menu, and a boolean functional field added in ir.needaction_mixin class for use in views. Please refer to the updated feature documentation for more details.

bzr revid: tde@openerp.com-20120418145648-5kywizj3nhhpntss
This commit is contained in:
Thibault Delavallée 2012-04-18 16:56:48 +02:00
commit 19530df8f3
6 changed files with 120 additions and 74 deletions

View File

@ -1,41 +1,84 @@
Need action mechanism
=====================
ir.needaction mixin class
++++++++++++++++++++++++++
.. versionadded:: 7.0
ir.needaction_mixin class
+++++++++++++++++++++++++
.. versionadded:: openobject-server.4124
This revision adds a mixin class for objects using the need action feature.
Need action feature can be used by objects willing to be able to signal that an action is required on a particular record. If in the business logic an action must be performed by somebody, for instance validation by a manager, this mechanism allows to set a list of users asked to perform an action.
This class wraps a class (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 record calls a method that updates the relationships.
This class wraps a class (ir.ir_needaction_users_rel) that behaves like a many2many field. However, no field is added to the model inheriting from ir.needaction_mixin. The mixin class manages the low-level considerations of updating relationships. Every change made on the record calls a method that updates the relationships.
Objects using the need_action feature should override the ``get_needaction_user_ids`` method. This methods returns a dictionary whose keys are record ids, and values a list of user ids, like in a many2many relationship. Therefore by defining only one method, you can specify if an action is required by defining the users that have to do it, in every possible situation.
This class also offers several global services,:
- ``needaction_get_record_ids``: for the current model and uid, get all record ids that ask this user to perform an action. This mechanism is used for instance to display the number of pending actions in menus, such as Leads (12)
- ``needaction_get_action_count``: as ``needaction_get_record_ids`` but returns only the number of action, not the ids (performs a search with count=True)
- ``needaction_get_user_record_references``: for a given uid, get all the records that ask this user to perform an action. Records are given as references, a list of tuples (model_name, record_id)
- ``needaction_get_user_record_references``: for a given uid, get all the records that ask this user to perform an action. Records are given as references, a list of tuples (model_name, record_id).
.. versionadded:: openobject-server.XXXX
This revision of the needaction_mixin mechanism slighty modifies the class behavior. The ``ir_needaction_mixin`` class now adds a function field on models inheriting from the class. This field allows to state whether a given record has a needaction for the current user. This is usefull if you want to customize views according to the needaction feature. For example, you may want to set records in bold in a list view if the current user has an action to perform on the record. This makes the class not a pure abstract class, but allows to easily use the action information. The field definition is::
def get_needaction_pending(self, cr, uid, ids, name, arg, context=None):
res = {}
needaction_user_ids = self.get_needaction_user_ids(cr, uid, ids, context=context)
for id in ids:
res[id] = uid in needaction_user_ids[id]
return res
_columns = {
'needaction_pending': fields.function(get_needaction_pending, type='boolean',
string='Need action pending',
help='If True, this field states that users have to perform an action. \
This field comes from the needaction mechanism. Please refer \
to the ir.needaction_mixin class.'),
}
ir.needaction_users_rel class
+++++++++++++++++++++++++++++
.. versionadded:: openobject-server.4124
This class essentially wraps a database table that behaves like a many2many.
It holds data related to the needaction mechanism inside OpenERP. A row
in this model is characterized by:
- ``res_model``: model of the record requiring an action
- ``res_id``: ID of the record requiring an action
- ``user_id``: foreign key to the res.users table, to the user that
has to perform the action
This model can be seen as a many2many, linking (res_model, res_id) to
users (those whose attention is required on the record)
Menu modification
+++++++++++++++++
.. versionchanged:: openobject-server.XXXX
This revision adds three functional fields to ``ir.ui.menu`` model :
- ``uses_needaction``: boolean field. If the menu entry action is an act_window action, and if this action is related to a model that uses the need_action mechanism, this field is set to true. Otherwise, it is false.
- ``needaction_uid_ctr``: integer field. If the target model uses the need action mechanism, this field gives the number of actions the current user has to perform.
- ``needaction_record_ids``: many2many field. If the target model uses the need action mechanism, this field holds the ids of the record requesting the user to perform an action.
- **REMOVED** ``needaction_record_ids``: many2many field. If the target model uses the need action mechanism, this field holds the ids of the record requesting the user to perform an action. **This field has been removed on version XXXX**.
Those fields are functional, because they depend on the user and must therefore be computed at every refresh, each time menus are displayed. The use of the need action mechanism is done by taking into account the action domain in order to display accurate results. When computing the value of the functional fields, the ids of records asking the user to perform an action is concatenated to the action domain. A counting search is then performed on the model, giving back the number of action the users has to perform, limited to the domain of the action.
Addon implementation example
++++++++++++++++++++++++++++
In your ``foo`` module, you want to specify that when it is in state ``confirmed``, it has to be validated by a manager, given by the field ``manager_id``. After making ``foo`` inheriting from ``base.needaction``, you override the ``get_needaction_user_ids`` method:
In your ``foo`` module, you want to specify that when it is in state ``confirmed``, it has to be validated by a manager, given by the field ``manager_id``. After making ``foo`` inheriting from ``ir.needaction_mixin``, you override the ``get_needaction_user_ids`` method:
::
[...]
_inherit = [base.needaction]
_inherit = [`ir.needaction_mixin]
[...]
def get_needaction_user_ids(self, cr, uid, ids, context=None):
result = dict.fromkeys(ids)

View File

@ -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_ir_needaction_users_tree">
<field name="name">ir.needaction_users.tree</field>
<field name="model">ir.needaction_users</field>
<record model="ir.ui.view" id="view_ir_needaction_users_rel_tree">
<field name="name">ir.needaction_users_rel.tree</field>
<field name="model">ir.needaction_users_rel</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" model="ir.actions.act_window">
<record id="action_view_needaction_users_rel" model="ir.actions.act_window">
<field name="name">Need action relationships</field>
<field name="res_model">ir.needaction_users</field>
<field name="res_model">ir.needaction_users_rel</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_needaction_users" name="Need actions" parent="base.menu_users" sequence="20" action="action_view_needaction_users" groups="base.group_no_one"/>
<menuitem id="menu_needaction_users_rel" name="Need actions" parent="base.menu_users" sequence="20" action="action_view_needaction_users_rel" groups="base.group_no_one"/>
<!-- Companies -->
<menuitem id="menu_res_company_global"

View File

@ -24,17 +24,18 @@ from operator import itemgetter
from osv import osv, fields
from tools.translate import _
class ir_needaction_users(osv.osv):
class ir_needaction_users_rel(osv.osv):
'''
ir_needaction_users holds data related to the needaction
mechanism inside OpenERP. A needaction is characterized by:
ir_needaction_users_rel holds data related to the needaction
mechanism inside OpenERP. A row in this model is characterized by:
- res_model: model of the record requiring an action
- res_id: ID of the record requiring an action
- user_id: foreign key to the res.users table, to the user that
has to perform the action
'''
This model can be seen as a many2many, linking (res_model, res_id) to
users (those whose attention is required on the record).'''
_name = 'ir.needaction_users'
_name = 'ir.needaction_users_rel'
_description = 'Needaction relationship table'
_rec_name = 'id'
_order = 'id desc'
@ -49,15 +50,11 @@ class ir_needaction_users(osv.osv):
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))
rel_ids = self.search(cr, uid, [('res_model', '=', res_model), ('res_id', 'in', res_ids)], context=context)
return list(set(map(itemgetter('user_id'), self.read(cr, uid, rel_ids, ['user_id'], 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)
@ -65,8 +62,6 @@ class ir_needaction_users(osv.osv):
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)
@ -74,7 +69,7 @@ class ir_needaction_users(osv.osv):
"""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]):
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)
@ -92,13 +87,13 @@ class ir_needaction_mixin(osv.osv):
validation by a manager, this mechanism allows to set a list of
users asked to perform an action.
This class wraps a class (ir.needaction_users) that behaves
This class wraps a class (ir.ir_needaction_users_rel) 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
inheriting from this mixin class. This class handles the low-level
considerations of updating relationships. Every change made on the
record calls a method that updates the relationships.
Objects using the need_action feature should override the
Objects using the 'need_action' feature should override the
``get_needaction_user_ids`` method. This methods returns a dictionary
whose keys are record ids, and values a list of user ids, like
in a many2many relationship. Therefore by defining only one method,
@ -116,9 +111,32 @@ class ir_needaction_mixin(osv.osv):
- ``needaction_get_user_record_references``: for a given uid, get all
the records that ask this user to perform an action. Records
are given as references, a list of tuples (model_name, record_id)
'''
The ``ir_needaction_mixin`` class adds a function field on models inheriting
from the class. This field allows to state whether a given record has
a needaction for the current user. This is usefull if you want to customize
views according to the needaction feature. For example, you may want to
set records in bold in a list view if the current user has an action to
perform on the record. This makes the class not a pure abstract class,
but allows to easily use the action information.'''
_name = 'ir.needaction_mixin'
_description = 'Need action of users on records API'
_description = '"Need action" mixin'
def get_needaction_pending(self, cr, uid, ids, name, arg, context=None):
res = {}
needaction_user_ids = self.get_needaction_user_ids(cr, uid, ids, context=context)
for id in ids:
res[id] = uid in needaction_user_ids[id]
return res
_columns = {
'needaction_pending': fields.function(get_needaction_pending, type='boolean',
string='Need action pending',
help='If True, this field states that users have to perform an action. \
This field comes from the needaction mechanism. Please refer \
to the ir.needaction_mixin class.'),
}
#------------------------------------------------------
# Addon API
@ -131,68 +149,56 @@ class ir_needaction_mixin(osv.osv):
return dict.fromkeys(ids, [])
def create(self, cr, uid, values, context=None):
if context is None:
context = {}
needact_table_obj = self.pool.get('ir.needaction_users')
rel_obj = self.pool.get('ir.needaction_users_rel')
# perform create
obj_id = super(ir_needaction_mixin, self).create(cr, uid, values, context=context)
# link user_ids
needaction_user_ids = self.get_needaction_user_ids(cr, uid, [obj_id], context=context)
needact_table_obj.create_users(cr, uid, [obj_id], self._name, needaction_user_ids[obj_id], context=context)
rel_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')
rel_obj = self.pool.get('ir.needaction_users_rel')
# perform write
write_res = super(ir_needaction_mixin, 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:
needact_table_obj.update_users(cr, uid, [id], self._name, needaction_user_ids[id], context=context)
rel_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
needact_table_obj = self.pool.get('ir.needaction_users')
needact_table_obj.unlink_users(cr, uid, ids, self._name, context=context)
rel_obj = self.pool.get('ir.needaction_users_rel')
rel_obj.unlink_users(cr, uid, ids, self._name, context=context)
# perform unlink
return super(ir_needaction_mixin, self).unlink(cr, uid, ids, context=context)
#------------------------------------------------------
# Need action API
# "Need action" API
#------------------------------------------------------
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 = {}
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))
return the record ids that require the user to perform an
action"""
rel_obj = self.pool.get('ir.needaction_users_rel')
rel_ids = rel_obj.search(cr, uid, [('res_model', '=', self._name), ('user_id', '=', user_id)], limit=limit, context=context)
return map(itemgetter('res_id'), rel_obj.read(cr, uid, rel_ids, ['res_id'], context=context))
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 = {}
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)
rel_obj = self.pool.get('ir.needaction_users_rel')
return rel_obj.search(cr, uid, [('res_model', '=', self._name), ('user_id', '=', user_id)], limit=limit, count=True, context=context)
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 = {}
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)
rel_obj = self.pool.get('ir.needaction_users_rel')
rel_ids = rel_obj.search(cr, uid, [('user_id', '=', user_id)], offset=offset, limit=limit, order=order, context=context)
return map(itemgetter('res_model', 'res_id'), rel_obj.read(cr, uid, rel_ids, ['res_model', 'res_id'], context=context))
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -258,19 +258,15 @@ class ir_ui_menu(osv.osv):
def _get_needaction(self, cr, uid, ids, field_names, args, context=None):
if context is None:
context = {}
res = dict.fromkeys(ids)
res = {}
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 = self.pool.get(menu.action.res_model).get_needaction_info(cr, uid, uid, domain=menu.action.domain, context=context)
# TODO: find the addon that causes a bug on runbot, not on local
if not isinstance(menu_needaction_res[1], (int, long)): menu_needaction_res[1] = 0
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, ()]
menu_needaction_res = [False, 0]
res[menu.id]['needaction_enabled'] = menu_needaction_res[0]
res[menu.id]['needaction_counter'] = menu_needaction_res[1]
# not used currently, therefore set to a void list
res[menu.id]['needaction_record_ids'] = []
return res
_columns = {
@ -291,7 +287,6 @@ class ir_ui_menu(osv.osv):
'web_icon_hover_data':fields.function(_get_image_icon, string='Web Icon Image (hover)', type='binary', readonly=True, store=True, multi='icon'),
'needaction_enabled': fields.function(_get_needaction, string='Target model uses the need action mechanism', type='boolean', help='If the menu entry action is an act_window action, and if this action is related to a model that uses the need_action mechanism, this field is set to true. Otherwise, it is false.', multi='_get_needaction'),
'needaction_counter': fields.function(_get_needaction, string='Number of actions the user has to perform', type='integer', help='If the target model uses the need action mechanism, this field gives the number of actions the current user has to perform.', multi='_get_needaction'),
'needaction_record_ids': fields.function(_get_needaction, string='Ids of records requesting an action from the user', type='many2many', help='If the target model uses the need action mechanism, this field holds the ids of the record requesting the user to perform an action.', multi='_get_needaction'),
'action': fields.function(_action, fnct_inv=_action_inv,
type='reference', string='Action',
selection=[

View File

@ -121,6 +121,6 @@
"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","ir_needaction_users","model_ir_needaction_users",,1,1,1,1
"access_ir_needaction_users_rel","ir_needaction_users_rel","model_ir_needaction_users_rel",,1,1,1,1
"access_ir_needaction_mixin","ir_needaction_mixin","model_ir_needaction_mixin",,1,1,1,1

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
121 access_ir_mail_server_all ir_mail_server model_ir_mail_server 1 0 0 0
122 access_ir_actions_todo_category ir_actions_todo_category model_ir_actions_todo_category group_system 1 1 1 1
123 access_ir_actions_client ir_actions_client all model_ir_actions_client 1 0 0 0
124 access_ir_needaction_users access_ir_needaction_users_rel ir_needaction_users ir_needaction_users_rel model_ir_needaction_users model_ir_needaction_users_rel 1 1 1 1
125 access_ir_needaction_mixin ir_needaction_mixin model_ir_needaction_mixin 1 1 1 1
126

View File

@ -4866,7 +4866,7 @@ class BaseModel(object):
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):
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
@ -4883,16 +4883,18 @@ class BaseModel(object):
:return: [uses_needaction=True/False, needaction_uid_ctr=%d]
"""
if hasattr(self, 'needaction_get_record_ids'):
# Arbitrary limit, but still much lower thant infinity, to avoid
# getting too much data.
ids = self.needaction_get_record_ids(cr, uid, user_id, limit=8192, context=context)
if not ids:
return [True, 0, []]
return [True, 0]
if domain:
new_domain = eval(domain, locals_dict={'uid': user_id}) + [('id', 'in', ids)]
else:
new_domain = [('id', 'in', ids)]
return [True, self.search(cr, uid, new_domain, limit=limit, order=order, count=True, context=context), ids]
return [True, self.search(cr, uid, new_domain, limit=limit, order=order, count=True, context=context)]
else:
return [False, 0, []]
return [False, 0]
# Transience
def is_transient(self):