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