[IMP] email.template: typos and usability review
authorOlivier Dony <odo@openerp.com>
Thu, 8 Sep 2011 17:37:52 +0000 (19:37 +0200)
committerOlivier Dony <odo@openerp.com>
Thu, 8 Sep 2011 17:37:52 +0000 (19:37 +0200)
bzr revid: odo@openerp.com-20110908173752-j6z5kd1in80jk8re

addons/email_template/email_template.py
addons/email_template/email_template_view.xml
addons/email_template/wizard/email_compose_message.py
addons/email_template/wizard/email_compose_message_view.xml
addons/email_template/wizard/email_template_preview.py
addons/email_template/wizard/email_template_preview_view.xml
addons/mail/wizard/mail_compose_message_view.xml

index 3120d22..b11821a 100644 (file)
 ##############################################################################
 
 import base64
+import logging
 
 import netsvc
 from osv import osv
 from osv import fields
+import tools
 from tools.translate import _
 
 try:
@@ -37,6 +39,7 @@ class email_template(osv.osv):
     _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}``
@@ -59,7 +62,8 @@ class email_template(osv.osv):
             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''
@@ -73,6 +77,7 @@ class email_template(osv.osv):
             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
@@ -98,7 +103,7 @@ class email_template(osv.osv):
                                  "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,
@@ -110,7 +115,6 @@ class email_template(osv.osv):
                                                  "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 "
@@ -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."),
     }
 
+    _defaults = {
+        'track_campaign_item': True
+    }
+
     def create_action(self, cr, uid, ids, context=None):
         vals = {}
         action_obj = self.pool.get('ir.actions.act_window')
index f8002c1..61d2dfc 100644 (file)
             <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" />
 
 
index 34ce59f..6330fe4 100644 (file)
 #
 ##############################################################################
 
+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'
@@ -34,7 +45,7 @@ class mail_compose_message(osv.osv_memory):
         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')
@@ -43,15 +54,16 @@ class mail_compose_message(osv.osv_memory):
         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 = []
@@ -65,7 +77,7 @@ class mail_compose_message(osv.osv_memory):
                 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,
@@ -73,36 +85,60 @@ class mail_compose_message(osv.osv_memory):
                     }
                     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:
index e230871..cf233b2 100644 (file)
@@ -1,6 +1,6 @@
 <?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>
@@ -8,16 +8,23 @@
             <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>
index 4e1382b..7d316da 100644 (file)
@@ -38,8 +38,8 @@ class email_template_preview(osv.osv_memory):
         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)
@@ -56,17 +56,17 @@ class email_template_preview(osv.osv_memory):
             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):
@@ -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['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}
 
index d9ba1a1..80d3e55 100644 (file)
@@ -23,7 +23,7 @@
                         <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"/>
index 491c4f1..68538a6 100644 (file)
@@ -28,7 +28,7 @@
                         </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>