1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from lxml import etree
5from lxml.html import builder as html
6
7from odoo import _, api, fields, models
8from odoo.exceptions import UserError
9
10
11class Invite(models.TransientModel):
12    """ Wizard to invite partners (or channels) and make them followers. """
13    _name = 'mail.wizard.invite'
14    _description = 'Invite wizard'
15
16    @api.model
17    def default_get(self, fields):
18        result = super(Invite, self).default_get(fields)
19        if self._context.get('mail_invite_follower_channel_only'):
20            result['send_mail'] = False
21        if 'message' not in fields:
22            return result
23
24        user_name = self.env.user.display_name
25        model = result.get('res_model')
26        res_id = result.get('res_id')
27        if model and res_id:
28            document = self.env['ir.model']._get(model).display_name
29            title = self.env[model].browse(res_id).display_name
30            msg_fmt = _('%(user_name)s invited you to follow %(document)s document: %(title)s')
31        else:
32            msg_fmt = _('%(user_name)s invited you to follow a new document.')
33
34        text = msg_fmt % locals()
35        message = html.DIV(
36            html.P(_('Hello,')),
37            html.P(text)
38        )
39        result['message'] = etree.tostring(message)
40        return result
41
42    res_model = fields.Char('Related Document Model', required=True, index=True, help='Model of the followed resource')
43    res_id = fields.Integer('Related Document ID', index=True, help='Id of the followed resource')
44    partner_ids = fields.Many2many('res.partner', string='Recipients', help="List of partners that will be added as follower of the current document.",
45                                   domain=[('type', '!=', 'private')])
46    channel_ids = fields.Many2many('mail.channel', string='Channels', help='List of channels that will be added as listeners of the current document.',
47                                   domain=[('channel_type', '=', 'channel')])
48    message = fields.Html('Message')
49    send_mail = fields.Boolean('Send Email', default=True, help="If checked, the partners will receive an email warning they have been added in the document's followers.")
50
51    def add_followers(self):
52        if not self.env.user.email:
53            raise UserError(_("Unable to post message, please configure the sender's email address."))
54        email_from = self.env.user.email_formatted
55        for wizard in self:
56            Model = self.env[wizard.res_model]
57            document = Model.browse(wizard.res_id)
58
59            # filter partner_ids to get the new followers, to avoid sending email to already following partners
60            new_partners = wizard.partner_ids - document.sudo().message_partner_ids
61            new_channels = wizard.channel_ids - document.message_channel_ids
62            document.message_subscribe(new_partners.ids, new_channels.ids)
63
64            model_name = self.env['ir.model']._get(wizard.res_model).display_name
65            # send an email if option checked and if a message exists (do not send void emails)
66            if wizard.send_mail and wizard.message and not wizard.message == '<br>':  # when deleting the message, cleditor keeps a <br>
67                message = self.env['mail.message'].create({
68                    'subject': _('Invitation to follow %(document_model)s: %(document_name)s', document_model=model_name, document_name=document.display_name),
69                    'body': wizard.message,
70                    'record_name': document.display_name,
71                    'email_from': email_from,
72                    'reply_to': email_from,
73                    'model': wizard.res_model,
74                    'res_id': wizard.res_id,
75                    'no_auto_thread': True,
76                    'add_sign': True,
77                })
78                partners_data = []
79                recipient_data = self.env['mail.followers']._get_recipient_data(document, 'comment', False, pids=new_partners.ids)
80                for pid, cid, active, pshare, ctype, notif, groups in recipient_data:
81                    pdata = {'id': pid, 'share': pshare, 'active': active, 'notif': 'email', 'groups': groups or []}
82                    if not pshare and notif:  # has an user and is not shared, is therefore user
83                        partners_data.append(dict(pdata, type='user'))
84                    elif pshare and notif:  # has an user and is shared, is therefore portal
85                        partners_data.append(dict(pdata, type='portal'))
86                    else:  # has no user, is therefore customer
87                        partners_data.append(dict(pdata, type='customer'))
88
89                document._notify_record_by_email(message, {'partners': partners_data, 'channels': []}, send_after_commit=False)
90                # in case of failure, the web client must know the message was
91                # deleted to discard the related failure notification
92                self.env['bus.bus'].sendone(
93                    (self._cr.dbname, 'res.partner', self.env.user.partner_id.id),
94                    {'type': 'deletion', 'message_ids': message.ids}
95                )
96                message.unlink()
97        return {'type': 'ir.actions.act_window_close'}
98