##############################################################################
import base64
+import logging
import netsvc
from osv import osv
from osv import fields
+import tools
from tools.translate import _
try:
_inherit = 'mail.message'
_name = "email.template"
_description = 'Email Templates'
+ _rec_name = 'name' # override mail.message's behavior
def render_template(self, cr, uid, template, model, res_id, context=None):
"""Render the given template text, replace mako expressions ``${expr}``
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
result = MakoTemplate(template).render_unicode(object=record,
user=user,
- context=context,
+ # context kw would clash with mako internals
+ ctx=context,
format_exceptions=True)
if result == u'False':
result = u''
context = {}
if not template_id:
return False
+ template = self.browse(cr, uid, template_id, context)
lang = self.render_template(cr, uid, template.lang, template.model, record_id, context)
if lang:
# Use translated template if necessary
"This should usually be a placeholder expression "
"that provides the appropriate language code, e.g. "
"${object.partner_id.lang.code}."),
- 'user_signature': fields.boolean('Signature',
+ 'user_signature': fields.boolean('Add Signature',
help="If checked, the user's signature will be appended to the text version "
"of the message"),
'report_name': fields.char('Report Filename', size=200, translate=True,
"of the related document model"),
'ref_ir_value':fields.many2one('ir.values', 'Sidebar button', readonly=True,
help="Sidebar button to open the sidebar action"),
- 'auto_delete': fields.boolean('Auto Delete', help="Permanently delete emails after sending"),
'track_campaign_item': fields.boolean('Resource Tracking',
help="Enable this is you wish to include a special tracking marker "
"in outgoing emails so you can identify replies and link "
'copyvalue': fields.char('Expression', size=256, help="Final placeholder expression, to be copy-pasted in the desired template field."),
}
+ _defaults = {
+ 'track_campaign_item': True
+ }
+
def create_action(self, cr, uid, ids, context=None):
vals = {}
action_obj = self.pool.get('ir.actions.act_window')
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Templates">
- <field name="name"/>
- <field name="smtp_server_id"/>
<field name="model_id"/>
- <field name="email_to"/>
- <field name="email_cc"/>
- <field name="email_bcc"/>
+ <field name="name"/>
<field name="subject"/>
- <field name="user_signature"/>
+ <field name="email_from"/>
+ <field name="email_to"/>
<field name="report_name"/>
<button name="%(wizard_email_template_preview)d" string="Preview Template"
type="action" target="new" icon="gtk-zoom-fit"/>
<field name="search_view_id" ref="view_email_template_search"/>
</record>
- <menuitem name="Email Templates" id="menu_email_template_all_tools"
+ <menuitem name="Templates" id="menu_email_template_all_tools"
parent="mail.menu_email_message_tools" action="action_email_template_tree_all" />
#
##############################################################################
+import base64
+
from osv import osv
from osv import fields
from tools.translate import _
-import base64
+import tools
+
+
+def _reopen(self,res_id):
+ return {'type': 'ir.actions.act_window',
+ 'view_mode': 'form',
+ 'view_type': 'form',
+ 'res_id': res_id,
+ 'res_model': self._name,
+ 'target': 'new'}
class mail_compose_message(osv.osv_memory):
_inherit = 'mail.compose.message'
if context is None:
context = {}
record_ids = []
- email_temp_pool = self.pool.get('email.template')
+ email_template= self.pool.get('email.template')
model = False
if context.get('message_id'):
mail_message = self.pool.get('mail.message')
elif context.get('active_model', False):
model = context.get('active_model')
if model:
- record_ids = email_temp_pool.search(cr, uid, [('model', '=', model)])
- return email_temp_pool.name_get(cr, uid, record_ids, context) + [(False,'')]
+ record_ids = email_template.search(cr, uid, [('model', '=', model)])
+ return email_template.name_get(cr, uid, record_ids, context) + [(False,'')]
return []
_columns = {
+ 'use_template': fields.boolean('Use Template'),
'template_id': fields.selection(_get_templates, 'Template'),
}
- def on_change_template(self, cr, uid, ids, template_id, context=None):
+ def on_change_template(self, cr, uid, ids, use_template, template_id, email_from=None, email_to=None, context=None):
if context is None:
context = {}
att_ids = []
for fname, fcontent in attachment.iteritems():
data_attach = {
'name': fname,
- 'datas': base64.b64_encode(fcontent),
+ 'datas': base64.b64encode(fcontent),
'datas_fname': fname,
'description': fname,
'res_model' : self._name,
}
att_ids.append(attachment_obj.create(cr, uid, data_attach))
values['attachment_ids'] = att_ids
+
+ # avoid overriding existing values
+ if email_from and 'email_from' in values:
+ del values['email_from']
+ if email_to and 'email_to' in values:
+ del values['email_to']
+ else:
+ # restore defaults
+ values = self.default_get(cr, uid, self.fields_get_keys(cr, uid), context)
+ values.update(use_template=use_template, template_id=template_id)
+
return {'value': values}
+
+ def template_toggle(self, cr, uid, ids, context=None):
+ for record in self.browse(cr, uid, ids, context=context):
+ had_template = record.use_template
+ record.write({'use_template': not(record.use_template)})
+ if had_template:
+ # equivalent to choosing an empty template
+ onchange_defaults = self.on_change_template(cr, uid, record.id, record.use_template,
+ False, email_from=record.email_from,
+ email_to=record.email_to, context=context)
+ record.write(onchange_defaults['value'])
+ return _reopen(self, record.id)
+
def save_as_template(self, cr, uid, ids, context=None):
- '''
- create a new template record
- '''
if context is None:
context = {}
- template_pool = self.pool.get('email.template')
+ email_template = self.pool.get('email.template')
model_pool = self.pool.get('ir.model')
for record in self.browse(cr, uid, ids, context=context):
model = context.get('active_model', record.model or False)
model = model_pool.search(cr, uid, [('model', '=', model)])[0]
model_name = model_pool.browse(cr, uid, model, context=context).name
+ template_name = "%s: %s" % (model_name, tools.ustr(record.subject))
values = {
- 'name': model_name,
+ 'name': template_name,
'email_from': record.email_from or False,
'subject': record.subject or False,
- 'body': record.body or False,
+ 'body_text': record.body_text or False,
'email_to': record.email_to or False,
'email_cc': record.email_cc or False,
'email_bcc': record.email_bcc or False,
'reply_to': record.reply_to or False,
- 'auto_delete': record.auto_delete,
'model_id': model or False,
- 'smtp_server_id': record.smtp_server_id.id or False,
'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])]
}
- template_pool.create(cr, uid, values, context=context)
- return {'type': 'ir.actions.act_window_close'}
+ template_id = email_template.create(cr, uid, values, context=context)
+ record.write({'template_id': template_id,
+ 'use_template': True})
+
+ # _reopen same wizard screen with new template preselected
+ return _reopen(self, record.id)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
- <data>
+ <data>
<record model="ir.ui.view" id="email_compose_message_wizard_inherit_form">
<field name="name">mail.compose.message.form</field>
<field name="type">form</field>
<field name="inherit_id" ref="mail.email_compose_message_wizard_form"/>
<field name="arch" type="xml">
- <data>
- <xpath expr="//field[@name='model']" position="after">
- <field name="template_id" colspan="4"
- on_change="on_change_template(template_id, context)"/>
- </xpath>
- <xpath expr="//button[@string='Cancel']" position="after">
- <button icon="gtk-new" type="object" name="save_as_template" string="Save As Template"/>
- </xpath>
- </data>
+ <data>
+ <xpath expr="//label[@name='placeholder']" position="before">
+ <group attrs="{'invisible':[('use_template','=',False)]}" colspan="4" col="4">
+ <field name="template_id" colspan="3"
+ on_change="on_change_template(use_template, template_id, email_from, email_to, context)"/>
+ <label string="" name="flexspace" colspan="1"/>
+ </group>
+ <group colspan="1" col="6">
+ <field name="use_template" invisible="1"/>
+ <button icon="gtk-paste" type="object" name="template_toggle"
+ string="" help="Use a message template" colspan="1"/>
+ <button icon="gtk-save" type="object" name="save_as_template"
+ string="" help="Save as a new template" colspan="1"/>
+ </group>
+ </xpath>
+ </data>
</field>
</record>
- </data>
+ </data>
</openerp>
template_id = context.get('template_id', False)
if not template_id:
return []
- template_pool = self.pool.get('email.template')
- template = template_pool.browse(cr, uid, int(template_id), context=context)
+ email_template = self.pool.get('email.template')
+ template = email_template.browse(cr, uid, int(template_id), context=context)
template_object = template.model_id
model = self.pool.get(template_object.model)
record_ids = model.search(cr, uid, [], 0, 10, 'id', context=context)
context = {}
result = super(email_template_preview, self).default_get(cr, uid, fields, context=context)
- template_pool = self.pool.get('email.template')
+ email_template = self.pool.get('email.template')
template_id = context.get('active_id', False)
if 'res_id' in fields:
records = self._get_records(cr, uid, context=context)
result['res_id'] = records and records[0][0] or False # select first record as a Default
if template_id and 'model_id' in fields:
- result['model_id'] = template_pool.read(cr, uid, int(template_id), ['model_id'], context).get('model_id', False)
+ result['model_id'] = email_template.read(cr, uid, int(template_id), ['model_id'], context).get('model_id', False)
return result
_columns = {
- 'res_id':fields.selection(_get_records, 'Referred Document'),
+ 'res_id':fields.selection(_get_records, 'Sample Document'),
}
def on_change_ref(self, cr, uid, ids, res_id, context=None):
vals['email_bcc'] = self.render_template(cr, uid, template.email_bcc, model, res_id, context)
vals['reply_to'] = self.render_template(cr, uid, template.reply_to, model, res_id, context)
vals['subject'] = self.render_template(cr, uid, template.subject, model, res_id, context)
- description = self.render_template(cr, uid, template.body, model, res_id, context) or ''
+ description = self.render_template(cr, uid, template.body_text, model, res_id, context) or ''
if template.user_signature:
signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
description += '\n' + signature
- vals['body'] = description
+ vals['body_text'] = description
vals['report_name'] = self.render_template(cr, uid, template.report_name, model, res_id, context)
return {'value': vals}
<separator string= "Body" colspan="2" />
<!--<separator string= "Body(Html)" colspan="2" />-->
<newline/>
- <field name="body" nolabel="1" colspan="2" readonly="1"/>
+ <field name="body_text" nolabel="1" colspan="2" readonly="1"/>
<!-- <field name="body_html" nolabel="1" colspan="2" readonly="1"/>-->
</group>
<field name="report_name" colspan="2" readonly="1"/>
</page>
</notebook>
<group col="4" colspan="4">
- <label string="" colspan="1"/>
+ <label string="" name="placeholder" colspan="1"/>
<button icon="gtk-close" special="cancel" string="Cancel"/>
<button icon="gtk-ok" name="send_mail" string="Send" type="object"/>
</group>