Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to display needaction count only in one menu ?

By
Emanuel Cino
on 9/15/15, 9:56 AM 2,263 views

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 ?

2

Axel Mendoza

--Axel Mendoza--
10238
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 9/15/15, 1:53 PM

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


 

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"

Emanuel Cino
on 9/16/15, 2:30 AM

yes, let me explain a little more

Axel Mendoza
on 9/16/15, 3:06 AM

my answer is edited with the explanation

Axel Mendoza
on 9/16/15, 3:26 AM

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.

Emanuel Cino
on 9/16/15, 7:57 AM

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.

Axel Mendoza
on 9/19/15, 2:56 AM

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

Axel Mendoza
on 9/19/15, 3:09 AM

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 ```

Emanuel Cino
on 9/23/15, 10:11 AM

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

Emanuel Cino
on 9/23/15, 10:13 AM
2

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 9/16/15, 3:10 AM

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.......

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

Emanuel Cino
on 9/16/15, 8:02 AM

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

Pawan
on 9/16/15, 9:52 AM

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...

Pawan
on 9/16/15, 9:55 AM

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

Emanuel Cino
on 9/16/15, 10:32 AM

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 ?

Emanuel Cino
on 9/17/15, 2:38 AM

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.....

Pawan
on 9/17/15, 11:56 AM

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).

Emanuel Cino
on 9/18/15, 3:14 AM

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

Pawan
on 9/19/15, 5:53 AM

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

Emanuel Cino
on 9/23/15, 10:09 AM
0
Marco Costella
On 6/30/16, 2:35 PM

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

0
Emanuel Cino
On 9/23/15, 10:39 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 9/15/15, 9:56 AM
Seen: 2263 times
Last updated: 6/30/16, 2:35 PM