Search code examples
javascriptpythoncrmodooodoo-10

Odoo opportunity new message uncheck recipient


In Odoo when I open an opportunity from "My pipeline" and on that opportunity I press button "New message" then customer from that opportunity appears (along with followers of that oppportunity) as a receiver of that message:

there is a checked checkbox near the name of that customer in the list of recipients for that message.

This is because file addons/mail/static/src/xml/chatter.xml has this:

 <!-- List of followers -->
                <div class="o_composer_suggested_partners">
                    <t t-foreach='widget.suggested_partners' t-as='recipient'>
                        <div t-attf-title="Add as recipient and follower (reason: #{recipient.reason})">
                            <input type="checkbox"
                                   t-att-checked="recipient.checked ? 'checked' : undefined"
                                   t-att-data-fullname="recipient.full_name"/>
                            <t t-esc="recipient.name"/>
                            <t t-if="recipient.email_address">(<t t-esc="recipient.email_address"/>)</t>
                        </div>
                    </t>
                </div>

And hovering on the one of the unwanted recipients shows following message "Adds as recipient and follower (reason: Customer)".

There is relevant source code in addons/crm/models/crm_lead.py

@api.multi
def _message_add_suggested_recipient(self, result, partner=None, email=None, reason=''):
    """ Called by message_get_suggested_recipients, to add a suggested
        recipient in the result dictionary. The form is :
            partner_id, partner_name<partner_email> or partner_name, reason """
    self.ensure_one()
    if email and not partner:
        # get partner info from email
        partner_info = self.message_partner_info_from_emails([email])[0]
        if partner_info.get('partner_id'):
            partner = self.env['res.partner'].sudo().browse([partner_info['partner_id']])[0]
    if email and email in [val[1] for val in result[self.ids[0]]]:  # already existing email -> skip
        return result
    if partner and partner in self.message_partner_ids:  # recipient already in the followers -> skip
        return result
    if partner and partner.id in [val[0] for val in result[self.ids[0]]]:  # already existing partner ID -> skip
        return result
    if partner and partner.email:  # complete profile: id, name <email>
        result[self.ids[0]].append((partner.id, '%s<%s>' % (partner.name, partner.email), reason))
    elif partner:  # incomplete profile: id, name
        result[self.ids[0]].append((partner.id, '%s' % (partner.name), reason))
    else:  # unknown partner, we are probably managing an email address
        result[self.ids[0]].append((False, email, reason))
    return result

@api.multi
def message_get_suggested_recipients(self):
    """ Returns suggested recipients for ids. Those are a list of
    tuple (partner_id, partner_name, reason), to be managed by Chatter. """
    result = dict((res_id, []) for res_id in self.ids)
    if 'user_id' in self._fields:
        for obj in self.sudo():  # SUPERUSER because of a read on res.users that would crash otherwise
            if not obj.user_id or not obj.user_id.partner_id:
                continue
            obj._message_add_suggested_recipient(result, partner=obj.user_id.partner_id, reason=self._fields['user_id'].string)
    return result

and in addons/mail/static/src/js/chatter.js

message_get_suggested_recipients: function () {
    var self = this;
    var email_addresses = _.pluck(this.suggested_partners, 'email_address');
    return this.thread_dataset
        .call('message_get_suggested_recipients', [[this.context.default_res_id], this.context])
        .done(function (suggested_recipients) {
            var thread_recipients = suggested_recipients[self.context.default_res_id];
            _.each(thread_recipients, function (recipient) {
                var parsed_email = utils.parse_email(recipient[1]);
                if (_.indexOf(email_addresses, parsed_email[1]) === -1) {
                    self.suggested_partners.push({
                        checked: true,
                        partner_id: recipient[0],
                        full_name: recipient[1],
                        name: parsed_email[0],
                        email_address: parsed_email[1],
                        reason: recipient[2],
                    });
                }
            });
        });
},

Is there a way to make by default the checkbox not checked for the customer here?

Is that correct?

  1. modify _message_add_suggested_recipient (in crm_lead.py) to contain "checked" = False for the partner.

  2. modify message_get_suggested_recipients (in chatter.js) to take checked value from parameter recipient instead of making true by default (checked: true).


Solution

  • Create a file in your module at /static/src/xml/chatter.xml with following content:

    <templates>
        <t t-name="mail.chatter.ChatComposer" t-extend="mail.chatter.ChatComposer">
            <t
                t-jquery="input[type='checkbox'][t-att-data-fullname='recipient.full_name']"
                t-operation="replace">
                <input type="checkbox" t-att-data-fullname="recipient.full_name" />
            </t>
        </t>
    </templates>
    

    And then call this file in your manifest:

    {
        # name, author, ...
        'qweb': [
            'static/src/xml/chatter.xml'
        ],
    }
    

    Attention: That will change the behaviour in all chatters in Odoo. It was tested for v10 only.