[IMP] account_followup: use the new signal_xxx methods instead of trg_validate.
[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         if 'message' in fields and result.get('res_model') and result.get('res_id'):
36             document_name = self.pool.get(result.get('res_model')).name_get(cr, uid, [result.get('res_id')], context=context)[0][1]
37             message = _('<div>You have been invited to follow %s.</div>' % document_name)
38             result['message'] = message
39         elif 'message' in fields:
40             result['message'] = _('<div>You have been invited to follow a new document.</div>')
41         return result
42
43     _columns = {
44         'res_model': fields.char('Related Document Model', size=128,
45                         required=True, select=1,
46                         help='Model of the followed resource'),
47         'res_id': fields.integer('Related Document ID', select=1,
48                         help='Id of the followed resource'),
49         'partner_ids': fields.many2many('res.partner', string='Partners'),
50         'message': fields.html('Message'),
51     }
52
53     def add_followers(self, cr, uid, ids, context=None):
54         for wizard in self.browse(cr, uid, ids, context=context):
55             model_obj = self.pool.get(wizard.res_model)
56             document = model_obj.browse(cr, uid, wizard.res_id, context=context)
57
58             # filter partner_ids to get the new followers, to avoid sending email to already following partners
59             new_follower_ids = [p.id for p in wizard.partner_ids if p.id not in document.message_follower_ids]
60             model_obj.message_subscribe(cr, uid, [wizard.res_id], new_follower_ids, context=context)
61
62             # send an email
63             if wizard.message:
64                 # add signature
65                 user_id = self.pool.get("res.users").read(cr, uid, [uid], fields=["signature"], context=context)[0]
66                 signature = user_id and user_id["signature"] or ''
67                 if signature:
68                     wizard.message = tools.append_content_to_html(wizard.message, signature, plaintext=True, container_tag='div')
69                 # send mail to new followers
70                 for follower_id in new_follower_ids:
71                     mail_mail = self.pool.get('mail.mail')
72                     # the invite wizard should create a private message not related to any object -> no model, no res_id
73                     mail_id = mail_mail.create(cr, uid, {
74                         'model': wizard.res_model,
75                         'res_id': wizard.res_id,
76                         'subject': 'Invitation to follow %s' % document.name_get()[0][1],
77                         'body_html': '%s' % wizard.message,
78                         'auto_delete': True,
79                         }, context=context)
80                     mail_mail.send(cr, uid, [mail_id], recipient_ids=[follower_id], context=context)
81         return {'type': 'ir.actions.act_window_close'}