[FIX] base(res_partner),point_of_sale,product,sale_stock,stock: uniformization of...
[odoo/odoo.git] / addons / email_template / ir_actions.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (c) 2013 OpenERP S.A. <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 Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (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 Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23
24
25 class actions_server(osv.Model):
26     """ Add email option in server actions. """
27     _name = 'ir.actions.server'
28     _inherit = ['ir.actions.server']
29
30     def _get_states(self, cr, uid, context=None):
31         res = super(actions_server, self)._get_states(cr, uid, context=context)
32         res.insert(0, ('email', 'Send Email'))
33         return res
34
35     _columns = {
36         'email_from': fields.related(
37             'template_id', 'email_from', type='char',
38             readonly=True, string='From'
39         ),
40         'email_to': fields.related(
41             'template_id', 'email_to', type='char',
42             readonly=True, string='To (Emails)'
43         ),
44         'partner_to': fields.related(
45             'template_id', 'partner_to', type='char',
46             readonly=True, string='To (Partners)'
47         ),
48         'subject': fields.related(
49             'template_id', 'subject', type='char',
50             readonly=True, string='Subject'
51         ),
52         'body_html': fields.related(
53             'template_id', 'body_html', type='text',
54             readonly=True, string='Body'
55         ),
56         'template_id': fields.many2one(
57             'email.template', 'Email Template', ondelete='set null',
58             domain="[('model_id', '=', model_id)]",
59         ),
60     }
61
62     def on_change_template_id(self, cr, uid, ids, template_id, context=None):
63         """ Render the raw template in the server action fields. """
64         fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to']
65         if template_id:
66             template_values = self.pool.get('email.template').read(cr, uid, [template_id], fields, context)[0]
67             values = dict((field, template_values[field]) for field in fields if template_values.get(field))
68             if not values.get('email_from'):
69                 return {'warning': {'title': 'Incomplete template', 'message': 'Your template should define email_from'}, 'value': values}
70         else:
71             values = dict.fromkeys(fields, False)
72
73         return {'value': values}
74
75     def run_action_email(self, cr, uid, action, eval_context=None, context=None):
76         if not action.template_id or not context.get('active_id'):
77             return False
78         self.pool['email.template'].send_mail(cr, uid, action.template_id.id, context.get('active_id'),
79                                               force_send=False, raise_exception=False, context=context)
80         return False
81
82
83 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: