Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / mail / wizard / invite.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2012-Today OpenERP SA (<http://www.openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 from openerp import tools
23 from openerp.osv import osv
24 from openerp.osv import fields
25 from openerp.tools.translate import _
26
27
28 class invite_wizard(osv.osv_memory):
29     """ Wizard to invite partners and make them followers. """
30     _name = 'mail.wizard.invite'
31     _description = 'Invite wizard'
32
33     def default_get(self, cr, uid, fields, context=None):
34         result = super(invite_wizard, self).default_get(cr, uid, fields, context=context)
35         user_name = self.pool.get('res.users').name_get(cr, uid, [uid], context=context)[0][1]
36         model = result.get('res_model')
37         res_id = result.get('res_id')
38         if 'message' in fields and model and res_id:
39             ir_model = self.pool.get('ir.model')
40             model_ids = ir_model.search(cr, uid, [('model', '=', self.pool[model]._name)], context=context)
41             model_name = ir_model.name_get(cr, uid, model_ids, context=context)[0][1]
42
43             document_name = self.pool[model].name_get(cr, uid, [res_id], context=context)[0][1]
44             message = _('<div><p>Hello,</p><p>%s invited you to follow %s document: %s.<p></div>') % (user_name, model_name, document_name)
45             result['message'] = message
46         elif 'message' in fields:
47             result['message'] = _('<div><p>Hello,</p><p>%s invited you to follow a new document.</p></div>') % user_name
48         return result
49
50     _columns = {
51         'res_model': fields.char('Related Document Model',
52                         required=True, select=1,
53                         help='Model of the followed resource'),
54         'res_id': fields.integer('Related Document ID', select=1,
55                         help='Id of the followed resource'),
56         'partner_ids': fields.many2many('res.partner', string='Recipients',
57             help="List of partners that will be added as follower of the current document."),
58         'message': fields.html('Message'),
59         'send_mail': fields.boolean('Send Email',
60             help="If checked, the partners will receive an email warning they have been "
61                     "added in the document's followers."),
62     }
63
64     _defaults = {
65         'send_mail': True,
66     }
67
68     def add_followers(self, cr, uid, ids, context=None):
69         for wizard in self.browse(cr, uid, ids, context=context):
70             model_obj = self.pool[wizard.res_model]
71             document = model_obj.browse(cr, uid, wizard.res_id, context=context)
72
73             # filter partner_ids to get the new followers, to avoid sending email to already following partners
74             new_follower_ids = [p.id for p in wizard.partner_ids if p not in document.message_follower_ids]
75             model_obj.message_subscribe(cr, uid, [wizard.res_id], new_follower_ids, context=context)
76
77             ir_model = self.pool.get('ir.model')
78             model_ids = ir_model.search(cr, uid, [('model', '=', model_obj._name)], context=context)
79             model_name = ir_model.name_get(cr, uid, model_ids, context=context)[0][1]
80
81             # send an email if option checked and if a message exists (do not send void emails)
82             if wizard.send_mail and wizard.message and not wizard.message == '<br>':  # when deleting the message, cleditor keeps a <br>
83                 # add signature
84                 # FIXME 8.0: use notification_email_send, send a wall message and let mail handle email notification + message box
85                 signature_company = self.pool.get('mail.notification').get_signature_footer(cr, uid, user_id=uid, res_model=wizard.res_model, res_id=wizard.res_id, context=context)
86                 wizard.message = tools.append_content_to_html(wizard.message, signature_company, plaintext=False, container_tag='div')
87
88                 # send mail to new followers
89                 # the invite wizard should create a private message not related to any object -> no model, no res_id
90                 mail_mail = self.pool.get('mail.mail')
91                 mail_id = mail_mail.create(cr, uid, {
92                     'model': wizard.res_model,
93                     'res_id': wizard.res_id,
94                     'record_name': document.name_get()[0][1],
95                     'email_from': self.pool['mail.message']._get_default_from(cr, uid, context=context),
96                     'reply_to': self.pool['mail.message']._get_default_from(cr, uid, context=context),
97                     'subject': _('Invitation to follow %s: %s') % (model_name, document.name_get()[0][1]),
98                     'body_html': '%s' % wizard.message,
99                     'auto_delete': True,
100                     'message_id': self.pool['mail.message']._get_message_id(cr, uid, {'no_auto_thread': True}, context=context),
101                     'recipient_ids': [(4, id) for id in new_follower_ids]
102                 }, context=context)
103                 mail_mail.send(cr, uid, [mail_id], context=context)
104         return {'type': 'ir.actions.act_window_close'}