Ir al contenido
Menú
Se marcó esta pregunta
21 Respuestas
14228 Vistas

I have several menus pointing to a same object. I would like to display a needaction count in the menuitem, but so far I was only able to display the same count in all menuitems pointing to my object.

Is it possible to have a different counts for the same object but different menuitems ? Or at least, is it possible to show the count in only one menuitem and hide it in the others ?

Avatar
Descartar
Mejor respuesta

Check how it is done in quotations/sale order as example

Edited

===========================================

When you put your model to inherit from the model ir.needaction_mixin, it's because you wanna indicate that there are some records that need an action and to indicate who are those records you need to override the method _needaction_domain_get to return a domain that will be used in combination with the menu action domain to filter the model records to display the count result. In the case of the sale.order it inherit from mail.thread that implements the _needaction_domain_get to display unread messages in the records.

For your case you just need to define a different domain for every menu action for the same object and of course the method _needaction_domain_get that build the domain for select the record count that need an action.

If your requirement it's not based on the domain of the menu action? that affects also your records universe in that menu, then you can go ok by specifying some different value in the context of the menus actions and use the value to build the domain in the method _needaction_domain_get based on the value of the context, something like this:

def _needaction_domain_get(self, cr, uid, context=None):
    if context.get('count_action') == 'menu1':
        return [('active_code','=','1')]
    elif context.get('count_action') == 'menu2':
        return [('active_code','=','2')]
    #else not need for menu3
    return [('active_code','=','3')]

then for the 3 menus you need to put the count_action key with the respective value.

================================================================

How to receive the action context in the _needaction_domain_get? You need to override the get_needaction_data of ir.ui.view to do that by simply copy the original code and changing the right lines, there is no other way. I highlights the changes:

class ir_ui_menu(osv.osv):
_inherit = 'ir.ui.menu'


def get_needaction_data(self, cr, uid, ids, context=None):
""" Return for each menu entry of ids :
- if it uses the needaction mechanism (needaction_enabled)
- the needaction counter of the related action, taking into account
the action domain
"""
if context is None:
context = {}
res = {}
menu_ids = set()
for menu in self.browse(cr, uid, ids, context=context):
menu_ids.add(menu.id)
ctx = None
if menu.action and menu.action.type in ('ir.actions.act_window', 'ir.actions.client') and menu.action.context:
try:
# use magical UnquoteEvalContext to ignore undefined client-side variables such as `active_id`
eval_ctx = tools.UnquoteEvalContext(**context)
ctx = eval(menu.action.context, locals_dict=eval_ctx, nocopy=True) or None
except Exception:
# if the eval still fails for some reason, we'll simply skip this menu
pass
menu_ref = ctx and ctx.get('needaction_menu_ref')
if menu_ref:
if not isinstance(menu_ref, list):
menu_ref = [menu_ref]
model_data_obj = self.pool.get('ir.model.data')
for menu_data in menu_ref:
try:
model, id = model_data_obj.get_object_reference(cr, uid, menu_data.split('.')[0], menu_data.split('.')[1])
if (model == 'ir.ui.menu'):
menu_ids.add(id)
except Exception:
pass

menu_ids = list(menu_ids)

for menu in self.browse(cr, uid, menu_ids, context=context):
res[menu.id] = {
'needaction_enabled': False,
'needaction_counter': False,
}
if menu.action and menu.action.type in ('ir.actions.act_window', 'ir.actions.client') and menu.action.res_model:
if menu.action.res_model in self.pool:
obj = self.pool[menu.action.res_model]
if obj._needaction:
if menu.action.type == 'ir.actions.act_window':
dom = menu.action.domain and eval(menu.action.domain, {'uid': uid}) or []
else:
dom = eval(menu.action.params_store or '{}', {'uid': uid}).get('domain')
res[menu.id]['needaction_enabled'] = obj._needaction
if menu.action.context:
act_ctx = dict(context, **menu.action.context)
else:
act_ctx = context

res[menu.id]['needaction_counter'] = obj._needaction_count(cr, uid, dom, context=act_ctx)
return res


 

Avatar
Descartar
Autor

Thank you, can you at least point me where to look at ? The only thing I can see in sale module is that sale_order inherits from "ir.needaction_mixin"

yes, let me explain a little more

my answer is edited with the explanation

Autor

I tried with the context, but the context defined in the action is not passed to _needaction_domain_get method. I am using version 8 just in case.

Sorry for the delay to respond, you are right, let me complete the answer one more time to allow you to receive the action context.

My first answer was updated to add the needed extension. Odoo forum have the behavior of not notify me about some comments or updates

Autor

Thanks to your answer I was able to make it !! However I didn't have to copy paste the whole method. In version, all I needed to do was this : ``` @api.multi def get_needaction_data(self): res = dict() for menu in self: if menu.action and menu.action.context: new_context = dict() try: new_context = ast.literal_eval(menu.action.context) new_context.update(self.env.context) except: new_context = self.env.context res.update( super(IrUiMenu, menu.with_context(new_context)).get_needaction_data()) return res ```

Autor

Oh this doesn't format well the code in the comments... I will just post it underneath but I accepted your answer. Thanks again !

Mejor respuesta

Emanuel, in order to show counts in each menu of same object, you can proceed in th eorder below.

'needaction_menu_ref': ['list_of_other_menu_ids_of_same_object']

to your respective Menu's action's context in xml file....

suppose on menu with id 'a' you will have :

'needaction_menu_ref': ['b', 'c']

suppose on menu with id 'b' you will have :

'needaction_menu_ref': ['a', 'c']

and so on...... then add this function to your object inheriting"ir.needaction_mixin":

@api.model

def _needaction_domain_get(self):

    return[('active', '=', True)] # you can modify as per your requirement.....

and you can also refer to similar post: https://www.odoo.com/forum/help-1/question/how-can-i-count-records-in-domain-and-display-it-in-line-with-menu-89943#answer-89945

Hope this helps you.......

Avatar
Descartar
Autor

Hey, this is exactly what I don't want to do. I want count in only one menu, or different counts for different menus (but all for same object). I hope it's clearer

hi,
your statement:different counts for different menus (but all for same object)
My statement:in order to show counts in each menu of same object i can't get where i misunderstood you...., i gave you the solution to get the [different]counts on different(each n every) menus of same object(but all for same object). Please clear me if i am wrong...
thanks

however, apart from all these, you need to provide domain(like for states, [('state', '=', 'draft)] in one menu and so on....) in every actions for the data you want to see in each menu...

Autor

Ok, I thought when reading your answer your solution was to display count on all menus (same count). I will try

Autor

Hi again, I tried but it doesn't work. In fact I have the same domain for different menus (I display the same items but with different views) and I would like to be able to specify different needactions for these menus. There should be a way of returning a different domain in method _needaction_domain_get depending on which action called it. In that sense, Axel Mendoza's answer was close to what I need, unfortunately the context is not passed in _needaction_domain_get method. Do you have any other idea how to achieve this ?

the domain which you will pass in the "domain" property of your action, you can get it in your _needaction_domain_get by calling its SUPER function, then you can use it as either to return same value or manipluate it(applying your "If's") as per your wish..... or next thing if you are on odoo 8 , you can browse context using "self.context", for Axel's method.....

Autor

I tried again but calling SUPER inside _needaction returns always [('message_unread', '=', True)] no matter in which action I am. And again, the context inside the method (using self.env.context) does not contain the values I add in the xml action context field. It only contains basic values (language, user, timezone).

OK, so for your requirement you can override "_needaction_count()" method, and there you will get the domain passed in your action, which you can use as u want....
def _needaction_count(self, cr, uid, domain=None, context=None):
""" Get the number of actions uid has to perform. """
print domain, res, 'YOUR DOMAIN and TOTAL ROCORD ITEMS (for your manipulations) '
res = super(crm_lead, self)._needaction_count(cr, uid, domain, context)
return res

Autor

_needaction_count gets neither the context... however Axel Mendoza solved the issue.

Autor Mejor respuesta

My final code in model ir_ui_menu (inherited) :

@api.multi

def get_needaction_data(self):
res = dict()
for menu in self:
if menu.action and menu.action.context:
new_context = dict()
try:
new_context = ast.literal_eval(menu.action.context)
new_context.update(self.env.context)
except:
new_context = self.env.context
res.update(
super(IrUiMenu,
menu.with_context(new_context)).get_needaction_data())
return res


In my view's action:

<record model="ir.actions.act_window" id="action_follow_sds">

     <field name="context">{'count_menu': 'menu_follow_sds'}</field>
</record>

In my model :

@api.model

def _needaction_domain_get(self):
if self.env.context.get('count_menu') == 'menu_follow_sds':
return [('sds_state', '=', 'sub_waiting')]
return False
Avatar
Descartar
Mejor respuesta

Hello,

I had the same problem (openerp v7) and i found this post. I like no one of the proposed solution so i read the code and i found this:

res[menu.id]['needaction_enabled'] = obj._needaction_needaction
res[menu.id]['needaction_counter'] = obj._needaction_count(cr, uid, dom, context=context)

You can inherit the function _needaction_count in your object and set the results has you prefer:

def _needaction_count(self, cr, uid, dom, context=None):
context = context or {}
if (dom):
res = len(self.search(cr, uid, dom, context=context))
else:
res = super(todo_todo, self)._needaction_count(cr, uid, dom, context=context)
return res

The dom parameter of the function is the domain of the menu action:

<field name="domain">[('state', '=', 'created')]</field>

If the result of the function is 0 the counter is not show.


I hope this will help!

Ciao

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
3
dic 21
6330
7
sept 20
6827
0
dic 18
2138
7
oct 18
21342
1
nov 17
3425