[REF] Refactored needaction mechanism. Added functional fields in ir.ui.menu so the web client only has to manage the values.

bzr revid: tde@openerp.com-20120328163549-4g3dc5mwdagenrq1
This commit is contained in:
Thibault Delavallée 2012-03-28 18:35:49 +02:00
parent f0ccf9be5e
commit 8196c9e1b1
4 changed files with 53 additions and 3 deletions

View File

@ -163,7 +163,7 @@ class base_needaction(osv.osv):
#------------------------------------------------------
@classmethod
def needaction_get_user_ids(cls, cr, uid, model, context=None):
def needaction_get_user_ids(cls, cr, uid, model_name, user_id, context=None):
"""Given a model
get the user_ids that have to perform at least one action"""
if context is None:
@ -174,13 +174,23 @@ class base_needaction(osv.osv):
return list(set([need_act['user_id'] for need_act in need_acts]))
@classmethod
def needaction_get_action_count(cls, cr, uid, model, user_id, context=None):
def needaction_get_record_ids(cls, cr, uid, model_name, user_id, limit=80, context=None):
"""Given a 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('base.needaction_users_rel')
return need_act_obj.search(cr, uid, [('res_model', '=', model), ('user_id', '=', user_id)], count=True, context=context)
need_act_ids = need_act_obj.search(cr, uid, [('res_model', '=', model_name), ('user_id', '=', user_id)], limit=limit, context=context)
return [need_act['res_id'] for need_act in need_act_obj.read(cr, uid, need_act_ids, context=context)]
@classmethod
def needaction_get_action_count(cls, cr, uid, model_name, user_id, context=None):
"""Given a 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('base.needaction_users_rel')
return need_act_obj.search(cr, uid, [('res_model', '=', model_name), ('user_id', '=', user_id)], count=True, context=context)
@classmethod
def needaction_get_record_references(cls, cr, uid, user_id, offset=None, limit=None, order=None, context=None):

View File

@ -1397,6 +1397,9 @@
<field name="arch" type="xml">
<form string="Menu">
<group col="6">
<field name="has_needaction"/>
<field name="needaction_ctr"/>
<newline/>
<field name="name" string="Menu" />
<field name="parent_id" colspan="2"/>
<field name="sequence" colspan="2"/>

View File

@ -255,6 +255,19 @@ class ir_ui_menu(osv.osv):
return res
def _get_needaction(self, cr, uid, ids, field_names, args, context=None):
if context is None:
context = {}
res = dict.fromkeys(ids, {})
for menu in self.browse(cr, uid, ids, context=context):
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)
else:
menu_needaction_res = [False, 0]
res[menu.id]['has_needaction'] = menu_needaction_res[0]
res[menu.id]['needaction_ctr'] = menu_needaction_res[1]
return res
_columns = {
'name': fields.char('Menu', size=64, required=True, translate=True),
'sequence': fields.integer('Sequence'),
@ -271,6 +284,8 @@ class ir_ui_menu(osv.osv):
'web_icon_hover':fields.char('Web Icon File (hover)', size=128),
'web_icon_data': fields.function(_get_image_icon, string='Web Icon Image', type='binary', readonly=True, store=True, multi='icon'),
'web_icon_hover_data':fields.function(_get_image_icon, string='Web Icon Image (hover)', type='binary', readonly=True, store=True, multi='icon'),
'has_needaction': fields.function(_get_needaction, string='User has actions to perform', type='boolean', help='', multi='has_action'),
'needaction_ctr': fields.function(_get_needaction, string='Action counter', type='integer', help='', multi='has_action'),
'action': fields.function(_action, fnct_inv=_action_inv,
type='reference', string='Action',
selection=[

View File

@ -57,6 +57,7 @@ from lxml import etree
import fields
import openerp
import openerp.netsvc as netsvc
import openerp.pooler as pooler
import openerp.tools as tools
from openerp.tools.config import config
from openerp.tools.safe_eval import safe_eval as eval
@ -716,6 +717,27 @@ 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 base.needaction for actual implementation
- this method returns default values
:return: [has_needaction=False, needaction_ctr=0]
"""
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, context=context)
if not ids:
return [True, 0]
if domain:
domain = eval(domain)
new_domain = domain + [('id', 'in', ids)]
else:
new_domain = [('ids', 'in', ids)]
return [True, model_obj.search(cr, uid, new_domain, limit=limit, order=order, count=True)]
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."""
pass