[IMP] email.template: typos and usability review

bzr revid: odo@openerp.com-20110908173752-j6z5kd1in80jk8re
This commit is contained in:
Olivier Dony 2011-09-08 19:37:52 +02:00
parent 32a892cbbb
commit 4281cbd0de
7 changed files with 94 additions and 46 deletions

View File

@ -21,10 +21,12 @@
############################################################################## ##############################################################################
import base64 import base64
import logging
import netsvc import netsvc
from osv import osv from osv import osv
from osv import fields from osv import fields
import tools
from tools.translate import _ from tools.translate import _
try: try:
@ -37,6 +39,7 @@ class email_template(osv.osv):
_inherit = 'mail.message' _inherit = 'mail.message'
_name = "email.template" _name = "email.template"
_description = 'Email Templates' _description = 'Email Templates'
_rec_name = 'name' # override mail.message's behavior
def render_template(self, cr, uid, template, model, res_id, context=None): def render_template(self, cr, uid, template, model, res_id, context=None):
"""Render the given template text, replace mako expressions ``${expr}`` """Render the given template text, replace mako expressions ``${expr}``
@ -59,7 +62,8 @@ class email_template(osv.osv):
user = self.pool.get('res.users').browse(cr, uid, uid, context=context) user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
result = MakoTemplate(template).render_unicode(object=record, result = MakoTemplate(template).render_unicode(object=record,
user=user, user=user,
context=context, # context kw would clash with mako internals
ctx=context,
format_exceptions=True) format_exceptions=True)
if result == u'False': if result == u'False':
result = u'' result = u''
@ -73,6 +77,7 @@ class email_template(osv.osv):
context = {} context = {}
if not template_id: if not template_id:
return False return False
template = self.browse(cr, uid, template_id, context)
lang = self.render_template(cr, uid, template.lang, template.model, record_id, context) lang = self.render_template(cr, uid, template.lang, template.model, record_id, context)
if lang: if lang:
# Use translated template if necessary # Use translated template if necessary
@ -98,7 +103,7 @@ class email_template(osv.osv):
"This should usually be a placeholder expression " "This should usually be a placeholder expression "
"that provides the appropriate language code, e.g. " "that provides the appropriate language code, e.g. "
"${object.partner_id.lang.code}."), "${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 " help="If checked, the user's signature will be appended to the text version "
"of the message"), "of the message"),
'report_name': fields.char('Report Filename', size=200, translate=True, 'report_name': fields.char('Report Filename', size=200, translate=True,
@ -110,7 +115,6 @@ class email_template(osv.osv):
"of the related document model"), "of the related document model"),
'ref_ir_value':fields.many2one('ir.values', 'Sidebar button', readonly=True, 'ref_ir_value':fields.many2one('ir.values', 'Sidebar button', readonly=True,
help="Sidebar button to open the sidebar action"), 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', 'track_campaign_item': fields.boolean('Resource Tracking',
help="Enable this is you wish to include a special tracking marker " help="Enable this is you wish to include a special tracking marker "
"in outgoing emails so you can identify replies and link " "in outgoing emails so you can identify replies and link "
@ -157,6 +161,10 @@ class email_template(osv.osv):
'copyvalue': fields.char('Expression', size=256, help="Final placeholder expression, to be copy-pasted in the desired template field."), '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): def create_action(self, cr, uid, ids, context=None):
vals = {} vals = {}
action_obj = self.pool.get('ir.actions.act_window') action_obj = self.pool.get('ir.actions.act_window')

View File

@ -109,14 +109,11 @@
<field name="type">tree</field> <field name="type">tree</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Templates"> <tree string="Templates">
<field name="name"/>
<field name="smtp_server_id"/>
<field name="model_id"/> <field name="model_id"/>
<field name="email_to"/> <field name="name"/>
<field name="email_cc"/>
<field name="email_bcc"/>
<field name="subject"/> <field name="subject"/>
<field name="user_signature"/> <field name="email_from"/>
<field name="email_to"/>
<field name="report_name"/> <field name="report_name"/>
<button name="%(wizard_email_template_preview)d" string="Preview Template" <button name="%(wizard_email_template_preview)d" string="Preview Template"
type="action" target="new" icon="gtk-zoom-fit"/> type="action" target="new" icon="gtk-zoom-fit"/>
@ -158,7 +155,7 @@
<field name="search_view_id" ref="view_email_template_search"/> <field name="search_view_id" ref="view_email_template_search"/>
</record> </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" /> parent="mail.menu_email_message_tools" action="action_email_template_tree_all" />

View File

@ -19,10 +19,21 @@
# #
############################################################################## ##############################################################################
import base64
from osv import osv from osv import osv
from osv import fields from osv import fields
from tools.translate import _ 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): class mail_compose_message(osv.osv_memory):
_inherit = 'mail.compose.message' _inherit = 'mail.compose.message'
@ -34,7 +45,7 @@ class mail_compose_message(osv.osv_memory):
if context is None: if context is None:
context = {} context = {}
record_ids = [] record_ids = []
email_temp_pool = self.pool.get('email.template') email_template= self.pool.get('email.template')
model = False model = False
if context.get('message_id'): if context.get('message_id'):
mail_message = self.pool.get('mail.message') mail_message = self.pool.get('mail.message')
@ -43,15 +54,16 @@ class mail_compose_message(osv.osv_memory):
elif context.get('active_model', False): elif context.get('active_model', False):
model = context.get('active_model') model = context.get('active_model')
if model: if model:
record_ids = email_temp_pool.search(cr, uid, [('model', '=', model)]) record_ids = email_template.search(cr, uid, [('model', '=', model)])
return email_temp_pool.name_get(cr, uid, record_ids, context) + [(False,'')] return email_template.name_get(cr, uid, record_ids, context) + [(False,'')]
return [] return []
_columns = { _columns = {
'use_template': fields.boolean('Use Template'),
'template_id': fields.selection(_get_templates, '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: if context is None:
context = {} context = {}
att_ids = [] att_ids = []
@ -65,7 +77,7 @@ class mail_compose_message(osv.osv_memory):
for fname, fcontent in attachment.iteritems(): for fname, fcontent in attachment.iteritems():
data_attach = { data_attach = {
'name': fname, 'name': fname,
'datas': base64.b64_encode(fcontent), 'datas': base64.b64encode(fcontent),
'datas_fname': fname, 'datas_fname': fname,
'description': fname, 'description': fname,
'res_model' : self._name, 'res_model' : self._name,
@ -73,36 +85,60 @@ class mail_compose_message(osv.osv_memory):
} }
att_ids.append(attachment_obj.create(cr, uid, data_attach)) att_ids.append(attachment_obj.create(cr, uid, data_attach))
values['attachment_ids'] = att_ids 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} 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): def save_as_template(self, cr, uid, ids, context=None):
'''
create a new template record
'''
if context is None: if context is None:
context = {} context = {}
template_pool = self.pool.get('email.template') email_template = self.pool.get('email.template')
model_pool = self.pool.get('ir.model') model_pool = self.pool.get('ir.model')
for record in self.browse(cr, uid, ids, context=context): for record in self.browse(cr, uid, ids, context=context):
model = context.get('active_model', record.model or False) model = context.get('active_model', record.model or False)
model = model_pool.search(cr, uid, [('model', '=', model)])[0] model = model_pool.search(cr, uid, [('model', '=', model)])[0]
model_name = model_pool.browse(cr, uid, model, context=context).name model_name = model_pool.browse(cr, uid, model, context=context).name
template_name = "%s: %s" % (model_name, tools.ustr(record.subject))
values = { values = {
'name': model_name, 'name': template_name,
'email_from': record.email_from or False, 'email_from': record.email_from or False,
'subject': record.subject 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_to': record.email_to or False,
'email_cc': record.email_cc or False, 'email_cc': record.email_cc or False,
'email_bcc': record.email_bcc or False, 'email_bcc': record.email_bcc or False,
'reply_to': record.reply_to or False, 'reply_to': record.reply_to or False,
'auto_delete': record.auto_delete,
'model_id': model or False, '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])] 'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])]
} }
template_pool.create(cr, uid, values, context=context) template_id = email_template.create(cr, uid, values, context=context)
return {'type': 'ir.actions.act_window_close'} 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: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<openerp> <openerp>
<data> <data>
<record model="ir.ui.view" id="email_compose_message_wizard_inherit_form"> <record model="ir.ui.view" id="email_compose_message_wizard_inherit_form">
<field name="name">mail.compose.message.form</field> <field name="name">mail.compose.message.form</field>
@ -8,16 +8,23 @@
<field name="type">form</field> <field name="type">form</field>
<field name="inherit_id" ref="mail.email_compose_message_wizard_form"/> <field name="inherit_id" ref="mail.email_compose_message_wizard_form"/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<data> <data>
<xpath expr="//field[@name='model']" position="after"> <xpath expr="//label[@name='placeholder']" position="before">
<field name="template_id" colspan="4" <group attrs="{'invisible':[('use_template','=',False)]}" colspan="4" col="4">
on_change="on_change_template(template_id, context)"/> <field name="template_id" colspan="3"
</xpath> on_change="on_change_template(use_template, template_id, email_from, email_to, context)"/>
<xpath expr="//button[@string='Cancel']" position="after"> <label string="" name="flexspace" colspan="1"/>
<button icon="gtk-new" type="object" name="save_as_template" string="Save As Template"/> </group>
</xpath> <group colspan="1" col="6">
</data> <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> </field>
</record> </record>
</data> </data>
</openerp> </openerp>

View File

@ -38,8 +38,8 @@ class email_template_preview(osv.osv_memory):
template_id = context.get('template_id', False) template_id = context.get('template_id', False)
if not template_id: if not template_id:
return [] return []
template_pool = self.pool.get('email.template') email_template = self.pool.get('email.template')
template = template_pool.browse(cr, uid, int(template_id), context=context) template = email_template.browse(cr, uid, int(template_id), context=context)
template_object = template.model_id template_object = template.model_id
model = self.pool.get(template_object.model) model = self.pool.get(template_object.model)
record_ids = model.search(cr, uid, [], 0, 10, 'id', context=context) record_ids = model.search(cr, uid, [], 0, 10, 'id', context=context)
@ -56,17 +56,17 @@ class email_template_preview(osv.osv_memory):
context = {} context = {}
result = super(email_template_preview, self).default_get(cr, uid, fields, 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) template_id = context.get('active_id', False)
if 'res_id' in fields: if 'res_id' in fields:
records = self._get_records(cr, uid, context=context) records = self._get_records(cr, uid, context=context)
result['res_id'] = records and records[0][0] or False # select first record as a Default result['res_id'] = records and records[0][0] or False # select first record as a Default
if template_id and 'model_id' in fields: 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 return result
_columns = { _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): def on_change_ref(self, cr, uid, ids, res_id, context=None):
@ -87,11 +87,11 @@ class email_template_preview(osv.osv_memory):
vals['email_bcc'] = self.render_template(cr, uid, template.email_bcc, model, res_id, context) 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['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) 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: if template.user_signature:
signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
description += '\n' + 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) vals['report_name'] = self.render_template(cr, uid, template.report_name, model, res_id, context)
return {'value': vals} return {'value': vals}

View File

@ -23,7 +23,7 @@
<separator string= "Body" colspan="2" /> <separator string= "Body" colspan="2" />
<!--<separator string= "Body(Html)" colspan="2" />--> <!--<separator string= "Body(Html)" colspan="2" />-->
<newline/> <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"/>--> <!-- <field name="body_html" nolabel="1" colspan="2" readonly="1"/>-->
</group> </group>
<field name="report_name" colspan="2" readonly="1"/> <field name="report_name" colspan="2" readonly="1"/>

View File

@ -28,7 +28,7 @@
</page> </page>
</notebook> </notebook>
<group col="4" colspan="4"> <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-close" special="cancel" string="Cancel"/>
<button icon="gtk-ok" name="send_mail" string="Send" type="object"/> <button icon="gtk-ok" name="send_mail" string="Send" type="object"/>
</group> </group>