Launchpad automatic translations update.
[odoo/odoo.git] / addons / email_template / wizard / mail_compose_message.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010-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 import base64
23
24 from osv import osv
25 from osv import fields
26 from tools.translate import _
27 import tools
28
29
30 def _reopen(self,res_id,model):
31     return {'type': 'ir.actions.act_window',
32             'view_mode': 'form',
33             'view_type': 'form',
34             'res_id': res_id,
35             'res_model': self._name,
36             'target': 'new',
37
38             # save original model in context, otherwise
39             # it will be lost on the action's context switch
40             'context': {'mail.compose.target.model': model}
41     }
42
43 class mail_compose_message(osv.osv_memory):
44     _inherit = 'mail.compose.message'
45
46     def _get_templates(self, cr, uid, context=None):
47         """
48         Return Email Template of particular  Model.
49         """
50         if context is None:
51             context = {}
52         record_ids = []
53         email_template= self.pool.get('email.template')
54         model = False
55         if context.get('message_id'):
56             mail_message = self.pool.get('mail.message')
57             message_data = mail_message.browse(cr, uid, int(context.get('message_id')), context)
58             model = message_data.model
59         elif context.get('mail.compose.target.model') or context.get('active_model'):
60             model = context.get('mail.compose.target.model', context.get('active_model'))
61         if model:
62             record_ids = email_template.search(cr, uid, [('model', '=', model)])
63             return email_template.name_get(cr, uid, record_ids, context) + [(False,'')]
64         return []
65
66     _columns = {
67         'use_template': fields.boolean('Use Template'),
68         'template_id': fields.selection(_get_templates, 'Template',
69                                         size=-1 # means we want an int db column
70                                         ),
71     }
72     
73     _defaults = {
74         'template_id' : lambda self, cr, uid, context={} : context.get('mail.compose.template_id', False)          
75     }
76
77     def on_change_template(self, cr, uid, ids, use_template, template_id, email_from=None, email_to=None, context=None):
78         if context is None:
79             context = {}
80         values = {}
81         if template_id:
82             res_id = context.get('active_id', False)
83             if context.get('mail.compose.message.mode') == 'mass_mail':
84                 # use the original template values - to be rendered when actually sent
85                 # by super.send_mail()
86                 values = self.pool.get('email.template').read(cr, uid, template_id, self.fields_get_keys(cr, uid), context)
87             else:
88                 # render the mail as one-shot
89                 values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
90                 # retrofit generated attachments in the expected field format
91                 if values['attachments']:
92                     attachment = values.pop('attachments')
93                     attachment_obj = self.pool.get('ir.attachment')
94                     att_ids = []
95                     for fname, fcontent in attachment.iteritems():
96                         data_attach = {
97                             'name': fname,
98                             'datas': base64.b64encode(fcontent),
99                             'datas_fname': fname,
100                             'description': fname,
101                             'res_model' : self._name,
102                             'res_id' : ids[0] if ids else False
103                         }
104                         att_ids.append(attachment_obj.create(cr, uid, data_attach))
105                     values['attachment_ids'] = att_ids
106         else:
107             # restore defaults
108             values = self.default_get(cr, uid, self.fields_get_keys(cr, uid), context)
109             values.update(use_template=use_template, template_id=template_id)
110
111         return {'value': values}
112
113
114     def template_toggle(self, cr, uid, ids, context=None):
115         for record in self.browse(cr, uid, ids, context=context):
116             had_template = record.use_template
117             record.write({'use_template': not(had_template)})
118             if had_template:
119                 # equivalent to choosing an empty template
120                 onchange_defaults = self.on_change_template(cr, uid, record.id, not(had_template),
121                                                             False, email_from=record.email_from,
122                                                             email_to=record.email_to, context=context)
123                 record.write(onchange_defaults['value'])
124             return _reopen(self, record.id, record.model)
125
126     def save_as_template(self, cr, uid, ids, context=None):
127         if context is None:
128             context = {}
129         email_template = self.pool.get('email.template')
130         model_pool = self.pool.get('ir.model')
131         for record in self.browse(cr, uid, ids, context=context):
132             model = record.model or context.get('active_model')
133             model_ids = model_pool.search(cr, uid, [('model', '=', model)])
134             model_id = model_ids and model_ids[0] or False
135             model_name = ''
136             if model_id:
137                 model_name = model_pool.browse(cr, uid, model_id, context=context).name
138             template_name = "%s: %s" % (model_name, tools.ustr(record.subject))
139             values = {
140                 'name': template_name,
141                 'email_from': record.email_from or False,
142                 'subject': record.subject or False,
143                 'body_text': record.body_text or False,
144                 'email_to': record.email_to or False,
145                 'email_cc': record.email_cc or False,
146                 'email_bcc': record.email_bcc or False,
147                 'reply_to': record.reply_to or False,
148                 'model_id': model_id or False,
149                 'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])]
150             }
151             template_id = email_template.create(cr, uid, values, context=context)
152             record.write({'template_id': template_id,
153                           'use_template': True})
154
155         # _reopen same wizard screen with new template preselected
156         return _reopen(self, record.id, model)
157
158     # override the basic implementation 
159     def render_template(self, cr, uid, template, model, res_id, context=None):
160         return self.pool.get('email.template').render_template(cr, uid, template, model, res_id, context=context)
161
162 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: