-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwizard_poweremail_preview.py
More file actions
191 lines (151 loc) · 7.08 KB
/
wizard_poweremail_preview.py
File metadata and controls
191 lines (151 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from __future__ import absolute_import
import sys
import traceback
from mako.exceptions import html_error_template
from osv import osv, fields
from ..poweremail_template import get_value
from tools.translate import _
import tools
from premailer import transform
class poweremail_preview(osv.osv_memory):
_name = "poweremail.preview"
_description = "Power Email Template Preview"
def _ref_models(self, cursor, uid, context=None):
template_obj = self.pool.get('poweremail.templates')
if not context:
context = {}
template_ids = context.get('active_ids', [])
if not isinstance(template_ids, (list, tuple)):
template_ids = [template_ids]
res = []
for template in template_obj.browse(cursor, uid, template_ids, context=context):
template_model = template.object_name.model
template_name = template.object_name.name
res.append((template_model, template_name))
return res
def get_save_to_draft(self, cursor, uid, context=None):
template_obj = self.pool.get('poweremail.templates')
if not context:
context = {}
template_ids = context.get('active_ids', [])
res = template_obj.read(cursor, uid, template_ids, ['save_to_drafts'])[0]['save_to_drafts']
return res
def _get_enforce_from_account_from_templat(self, cr, uid, context=None):
if context is None:
context = {}
res = ''
template_ids = context.get('active_ids', [])
if template_ids:
template_obj = self.pool.get('poweremail.templates')
templates_brw = template_obj.simple_browse(cr, uid, template_ids, context=context)
res = templates_brw[0].enforce_from_account.id
return res
_columns = {
'model_ref': fields.reference(
"Template reference", selection=_ref_models,
size=64, required=True
),
'to': fields.char('To', size=250, readonly=True),
'cc': fields.char('CC', size=250, readonly=True),
'bcc': fields.char('BCC', size=250, readonly=True),
'subject': fields.char('Subject', size=200, readonly=True),
'lang': fields.char('Language', size=6, readonly=True),
'body_text': fields.text('Body', readonly=True),
'body_html': fields.text('Body', readonly=True),
'report': fields.char('Report Name', size=100, readonly=True),
'env': fields.text('Extra scope variables'),
'state': fields.selection([('init', 'Init'), ('end', 'End'), ('error', 'Error')], 'State'),
'save_to_drafts_prev': fields.boolean('Save to Drafts',
help="When automatically sending emails generated from"
" this template, save them into the Drafts folder rather"
" than sending them immediately."),
'enforce_from_account': fields.many2one('poweremail.core_accounts',
"Email account",
help="Email will be sent from this account."),
}
_defaults = {
'state': lambda *a: 'init',
'enforce_from_account': lambda self, cr, uid, context:
self._get_enforce_from_account_from_templat(cr, uid, context),
'save_to_drafts_prev': get_save_to_draft
}
def action_generate_static_mail(self, cr, uid, ids, context=None):
wizard_values = self.read(cr, uid, ids, ['model_ref', 'env'], context=context)
if context is None:
context = {}
if not wizard_values:
return {}
wizard_values = wizard_values[0]
model_name, record_id = wizard_values['model_ref'].split(',')
record_id = int(record_id)
template = self.pool.get('poweremail.templates').browse(cr, uid, context['active_id'], context=context)
# Search translated template
lang = get_value(cr, uid, record_id, template.lang, template, context)
ctx = context.copy()
if lang:
ctx.update({'lang': lang})
template = self.pool.get('poweremail.templates').browse(cr, uid, context['active_id'], ctx)
vals = {'lang': str(lang)}
mail_fields = ['to', 'cc', 'bcc', 'subject', 'body_text', 'body_html', 'report']
ctx['raise_exception'] = True
if wizard_values['env']:
ctx.update(eval(wizard_values['env']))
for field in mail_fields:
try:
if field == 'report':
field_value = template.file_name
else:
field_value = getattr(template, "def_{}".format(field))
vals[field] = get_value(
cr, uid, record_id, field_value, template, ctx
)
if field == 'body_text' and template.inline:
vals[field] = transform(vals[field])
except Exception as e:
if field == 'body_text':
vals[field] = html_error_template().render()
else:
tb = traceback.format_tb(sys.exc_info()[2])
vals[field] = '{}\n{}'.format(e.message, ''.join(tb))
vals['state'] = 'error'
self.write(cr, uid, ids, vals, context=context)
return True
def action_send_static_mail(self, cursor, uid, ids, context=None):
if context is None:
context = {}
template_obj = self.pool.get('poweremail.templates')
mailbox_obj = self.pool.get('poweremail.mailbox')
wizard = self.browse(cursor, uid, ids[0], context=context)
template_ids = context['active_ids']
if not wizard.model_ref:
raise osv.except_osv(
_('Error'),
_('No model reference defined')
)
model_name, model_id = wizard.model_ref.split(',')
model_id = int(model_id)
if not isinstance(template_ids, (list, tuple)):
template_ids = [template_ids]
mailbox_ids = []
for template_id in template_ids:
template = template_obj.browse(cursor, uid, template_id,
context=context)
if not template:
raise Exception("The requested template could not be loaded")
ctx = context.copy()
ctx['src_rec_id'] = model_id
ctx['src_model'] = template.object_name.model
mailbox_id = template_obj.generate_mail_sync(cursor, uid, template_id, model_id, context=ctx)
if wizard.save_to_drafts_prev:
mailbox_obj.write(cursor, uid, mailbox_id, {'folder': 'drafts'}, context=context)
mailbox_ids.append(mailbox_id)
wizard.write({'state': 'end'}, context=context)
return {
'domain': "[('id','in', %s)]" % str(mailbox_ids),
'name': _('Generated Email'),
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'poweremail.mailbox',
'type': 'ir.actions.act_window'
}
poweremail_preview()