1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Business Applications
5 # Copyright (c) 2013 OpenERP S.A. <http://www.openerp.com>
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import fields, osv
25 class actions_server(osv.Model):
26 """ Add email option in server actions. """
27 _name = 'ir.actions.server'
28 _inherit = ['ir.actions.server']
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'))
36 'email_from': fields.related(
37 'template_id', 'email_from', type='char',
38 readonly=True, string='From'
40 'email_to': fields.related(
41 'template_id', 'email_to', type='char',
42 readonly=True, string='To (Emails)'
44 'partner_to': fields.related(
45 'template_id', 'partner_to', type='char',
46 readonly=True, string='To (Partners)'
48 'subject': fields.related(
49 'template_id', 'subject', type='char',
50 readonly=True, string='Subject'
52 'body_html': fields.related(
53 'template_id', 'body_html', type='text',
54 readonly=True, string='Body'
56 'template_id': fields.many2one(
57 'email.template', 'Email Template', ondelete='set null',
58 domain="[('model_id', '=', model_id)]",
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']
66 template_values = self.pool.get('email.template').read(cr, uid, template_id, fields, context)
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}
71 values = dict.fromkeys(fields, False)
73 return {'value': values}
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'):
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)
83 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: