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 automatically add followers to chatter depending on user group?

By
Yenthe
on 1/26/16, 5:39 AM 902 views

Hi guys,

I've made a new model 'sel.mdo' on which I've added the whole chatter functionality:

class sel_mdo(models.Model):
    _name = 'sel.mdo'
    _inherit = ['mail.thread', 'ir_needaction_mixin']
Now when a button is pressed in this view I'd like to post a new message in this chatter. I do it like this:
@api.multi
def mdo_indienen(self):
    self.ensure_one()
    
    self.write({
        'state': 'aanvraag',
    })

    body = _(u'My custom notification!')
    return self.message_post(body=body)
This all works fine but I have one problem at this point: all users that belong to the group 'aa_sel_kempen.group_sel_manager' should be added as followers before this notification is sent and all the users that belong to this group should get this message inside their Odoo inbox. I've tried the following:

@api.multi
def mdo_indienen(self):
    self.ensure_one()

    user_ids = self.pool.get('res.users').search(self._cr, self._uid, [])
    user_ids_to_attach_as_chat_followers = []
    for user_id in user_ids:
        flag = self.pool.get('res.users').has_group(self._cr, user_id, 'aa_sel_kempen.group_sel_manager')
        if flag is True:
            user_ids_to_attach_as_chat_followers.append(user_id)    
    # Write on the record
    self.write({
        'state': 'aanvraag',
        'message_follower_ids': user_ids_to_attach_as_chat_followers

body = _(u'My text') return self.message_post(body=body) 

This leaves me with two problems though:
1) I see that followers are added but they aren't users but relations (from the model 'res.partner')
2) When a correct follower (from 'res.partner') is added (manually by me) I still see no notification showing up under messaging. The new posted message on this record should also be seen from the inbox under Messaging.

So, what am I missing?
Yenthe

1
Samantha Cruz
On 1/26/16, 8:54 PM

First of all, if you want to add a followers you should get their partner_ids not user_ids. Your self.message_post is not complete.

If you want to send a notification, below is an example:


             self.message_post(
cr, uid, False,
subject="Message Subject",
body= ("Message body."),
partner_ids = followers,
type='notification',
subtype=False,
context=context)

Thanks this gave me a global idea about how to do this, upvoted it!

Yenthe
on 1/28/16, 3:38 AM
0

Yenthe

--Yenthe--
6676
| 7 7 8
Turnhout, Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 1/28/16, 3:41 AM


The final solution:

@api.multi def mdo_indienen(self): self.ensure_one() #Attach all people that are in the group user_ids = self.pool.get('res.users').search(self._cr, self._uid, []) user_ids_to_attach_as_chat_followers = []

for user_id in user_ids: flag = self.pool.get('res.users').has_group(self._cr, user_id, 'aa_sel_kempen.group_sel_manager') if flag is True: user_record = self.pool.get('res.users').browse(self._cr, self._uid, user_id) user_ids_to_attach_as_chat_followers.append(user_record.partner_id.id)

#Write on the record self.write({ 'state': 'aanvraag', 'message_follower_ids': user_ids_to_attach_as_chat_followers })

""" Write a notification message on the chatter of this MDO. This message should also be shown in the messages inbox for all admins. """ body = _(u'Er is een nieuwe MDO aanvraag ingediend voor de patiënt ' + self.mdo_patient_id.name + '.') return self.message_post(body=body, partner_ids = user_ids_to_attach_as_chat_followers


So I had to get the partner_ids in place of the user_ids (thank you Samantha!) and finally I had to add partner_ids = user_ids_to_attach_as_chat_followers so that the message is shown in the inbox for all added users (under Messaging).

Welcome :)

Samantha Cruz
on 1/28/16, 9:32 PM

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

1 follower(s)

Stats

Asked: 1/26/16, 5:39 AM
Seen: 902 times
Last updated: 1/28/16, 3:41 AM