[IMP] sale order line invisible type
[odoo/odoo.git] / addons / email_template / wizard / email_template_preview.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2009 Sharoon Thomas
6 #    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
20 #
21 ##############################################################################
22
23 from osv import osv, fields
24
25 class email_template_preview(osv.osv_memory):
26     _inherit = "email.template"
27     _name = "email_template.preview"
28     _description = "Email Template Preview"
29     _rec_name = "subject"
30
31     def _get_records(self, cr, uid, context=None):
32         """
33         Return Records of particular Email Template's Model
34         """
35         if context is None:
36             context = {}
37
38         template_id = context.get('template_id', False)
39         if not template_id:
40             return []
41         email_template = self.pool.get('email.template')
42         template = email_template.browse(cr, uid, int(template_id), context=context)
43         template_object = template.model_id
44         model =  self.pool.get(template_object.model)
45         record_ids = model.search(cr, uid, [], 0, 10, 'id', context=context)
46         default_id = context.get('default_res_id')
47
48         if default_id and default_id not in record_ids:
49             record_ids.insert(0, default_id)
50
51         return model.name_get(cr, uid, record_ids, context)
52
53
54     def default_get(self, cr, uid, fields, context=None):
55         if context is None:
56             context = {}
57         result = super(email_template_preview, self).default_get(cr, uid, fields, context=context)
58
59         email_template = self.pool.get('email.template')
60         template_id = context.get('template_id')
61         if 'res_id' in fields and not result.get('res_id'):
62             records = self._get_records(cr, uid, context=context)
63             result['res_id'] = records and records[0][0] or False # select first record as a Default
64         if template_id and 'model_id' in fields and not result.get('model_id'):
65             result['model_id'] = email_template.read(cr, uid, int(template_id), ['model_id'], context).get('model_id', False)
66         return result
67
68     _columns = {
69         'res_id':fields.selection(_get_records, 'Sample Document'),
70     }
71
72     def on_change_res_id(self, cr, uid, ids, res_id, context=None):
73         if not res_id:
74             return {}
75         vals = {}
76         email_template = self.pool.get('email.template')
77         template_id = context and context.get('template_id')
78         template = email_template.get_email_template(cr, uid, template_id=template_id, record_id=res_id, context=context)
79         model = template.model
80         vals['email_to'] = self.render_template(cr, uid, template.email_to, model, res_id, context)
81         vals['email_cc'] = self.render_template(cr, uid, template.email_cc, model, res_id, context)
82         vals['email_bcc'] = self.render_template(cr, uid, template.email_bcc, model, res_id, context)
83         vals['reply_to'] = self.render_template(cr, uid, template.reply_to, model, res_id, context)
84         vals['subject'] = self.render_template(cr, uid, template.subject, model, res_id, context)
85         description = self.render_template(cr, uid, template.body_text, model, res_id, context) or ''
86         if template.user_signature:
87             signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
88             description += '\n' + signature
89         vals['body_text'] = description
90         if template.body_html:
91             vals['body_html'] = self.render_template(cr, uid, template.body_html, model, res_id, context) or ''
92         vals['report_name'] = self.render_template(cr, uid, template.report_name, model, res_id, context)
93         return {'value': vals}
94
95 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: