[IMP] website_mail: make the template / mail editor work. We now have a limited
authorThibault Delavallée <tde@openerp.com>
Wed, 16 Apr 2014 10:59:01 +0000 (12:59 +0200)
committerThibault Delavallée <tde@openerp.com>
Wed, 16 Apr 2014 10:59:01 +0000 (12:59 +0200)
set of fields that we try to edit (body_html and body for body, email_from and email for
email-from, name and subject for subject), because t-field is not dynamic. If the model
that should be edited does not hold those fields, the mail editor won't work.

Also fixed editor not being actiated when going into the body edition.

bzr revid: tde@openerp.com-20140416105901-vavkh9erjsq4mof9

addons/mass_mailing/models/mass_mailing.py
addons/website/static/src/js/website.editor.js
addons/website_mail/controllers/email_designer.py
addons/website_mail/models/email_template.py
addons/website_mail/static/src/js/website_email_designer.js
addons/website_mail/views/website_email_designer.xml

index cb94001..bd314f0 100644 (file)
@@ -516,7 +516,7 @@ class MassMailing(osv.Model):
         if not len(ids) == 1:
             raise ValueError('One and only one ID allowed for this action')
         mail = self.browse(cr, uid, ids[0], context=context)
-        url = '/website_mail/email_designer?model=mail.mass_mailing&res_id=%d&field_body=body_html&field_from=email_form&field_subject=name&template_model=%s' % (ids[0], mail.mailing_model)
+        url = '/website_mail/email_designer?model=mail.mass_mailing&res_id=%d&template_model=%s&enable_editor=1' % (ids[0], mail.mailing_model)
         return {
             'name': _('Open with Visual Editor'),
             'type': 'ir.actions.act_url',
index 365b8cf..2119d05 100644 (file)
                 editor.edit();
             }
         });
+        website.editor_bar = editor;
     };
 
     /* ----- TOP EDITOR BAR FOR ADMIN ---- */
index 8b34391..447474c 100644 (file)
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
-# from openerp import SUPERUSER_ID
+from urllib import urlencode
+
 from openerp.addons.web import http
 from openerp.addons.web.http import request
 
@@ -8,33 +9,63 @@ from openerp.addons.web.http import request
 class WebsiteEmailDesigner(http.Controller):
 
     @http.route('/website_mail/email_designer', type='http', auth="user", website=True, multilang=True)
-    def index(self, model, res_id, template_model, field_body='body', field_from='email_from', field_subject='name', **kw):
+    def index(self, model, res_id, template_model=None, **kw):
         if not model or not model in request.registry or not res_id:
             return request.redirect('/')
-#        if not 'body' in request.registry[model]._all_columns and not 'body_html' in request.registry[model]._all_columns:
-#            return request.redirect('/')
+        model_cols = request.registry[model]._all_columns
+        if 'body' not in model_cols and 'body_html' not in model_cols or \
+           'email' not in model_cols and 'email_from' not in model_cols or \
+           'name' not in model_cols and 'subject' not in model_cols:
+            return request.redirect('/')
         obj_ids = request.registry[model].exists(request.cr, request.uid, [res_id], context=request.context)
         if not obj_ids:
             return request.redirect('/')
+        # try to find fields to display / edit -> as t-field is static, we have to limit
+        # the available fields to a given subset
+        email_from_field = 'email'
+        if 'email_from' in model_cols:
+            email_from_field = 'email_from'
+        subject_field = 'name'
+        if 'subject' in model_cols:
+            subject_field = 'subject'
+        body_field = 'body'
+        if 'body_html' in model_cols:
+            body_field = 'body_html'
+
         cr, uid, context = request.cr, request.uid, request.context
-        tmpl_obj = request.registry['email.template']
         res_id = int(res_id)
+        record = request.registry[model].browse(cr, uid, res_id, context=context)
 
-        tids = tmpl_obj.search(cr, uid, [('model','=',template_model)], context=context)
-        templates = tmpl_obj.browse(cr, uid, tids, context=context)
-
-        print templates
         values = {
-            'object': request.registry[model].browse(cr, uid, res_id, context=context),
-            'templates': templates,
+            'record': record,
+            'templates': None,
             'model': model,
             'res_id': res_id,
-            'field_body': field_body,
-            'field_from': field_from,
-            'field_subject': field_subject,
+            'email_from_field': email_from_field,
+            'subject_field': subject_field,
+            'body_field': body_field,
         }
-        print '*', values
-        return request.website.render("website_mail.designer_index", values)
+
+        if getattr(record, body_field):
+            values['mode'] = 'email_designer'
+        else:
+            if kw.get('enable_editor'):
+                kw.pop('enable_editor')
+                fragments = dict(model=model, res_id=res_id, **kw)
+                if template_model:
+                    fragments['template_model'] = template_model
+                return request.redirect('/website_mail/email_designer?%s' % urlencode(fragments))
+            values['mode'] = 'email_template'
+
+        tmpl_obj = request.registry['email.template']
+        if template_model:
+            tids = tmpl_obj.search(cr, uid, [('model', '=', template_model)], context=context)
+        else:
+            tids = tmpl_obj.search(cr, uid, [], context=context)
+        templates = tmpl_obj.browse(cr, uid, tids, context=context)
+        values['templates'] = templates
+
+        return request.website.render("website_mail.email_designer", values)
 
     @http.route(['/website_mail/snippets'], type='json', auth="user", website=True)
     def snippets(self):
index 8f8ba3c..aaf8f32 100644 (file)
@@ -1,35 +1,16 @@
 # -*- coding: utf-8 -*-
-##############################################################################
-#
-#    OpenERP, Open Source Management Solution
-#    Copyright (C) 2014-Today OpenERP SA (<http://www.openerp.com>).
-#
-#    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU Affero General Public License as
-#    published by the Free Software Foundation, either version 3 of the
-#    License, or (at your option) any later version.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU Affero General Public License for more details.
-#
-#    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#
-##############################################################################
 
-from openerp.osv import osv, fields
+from openerp.osv import osv
 from openerp.tools.translate import _
 
 
 class EmailTemplate(osv.Model):
     _inherit = 'email.template'
 
-    def action_edit_html(self, cr, uid, ids, name, args, context=None):
-        # tde fixme: avoid asserts
-        assert len(ids)==1, "One and only one ID allowed for this action"
-        url = '/website_mail/email_designer?model=email.template&res_id=%d&field_body=body_html&field_from=email_form&field_subject=name' % (ids[0],)
+    def action_edit_html(self, cr, uid, ids, context=None):
+        if not len(ids) == 1:
+            raise ValueError('One and only one ID allowed for this action')
+        url = '/website_mail/email_designer?model=email.template&res_id=%d&enable_editor=1' % (ids[0],)
         return {
             'name': _('Edit Template'),
             'type': 'ir.actions.act_url',
index b023f08..bd6516b 100644 (file)
@@ -3,9 +3,6 @@
     var website = openerp.website;
 
     website.snippet.BuildingBlock.include({
-        // init: function (parent) {
-        //     this._super.apply(this, arguments);
-        // },
         _get_snippet_url: function () {
             return '/website_mail/snippets';
         }
@@ -18,7 +15,7 @@
             $('#email_template').hide();
             $(".js_content", $(this).parent()).children().clone().appendTo('#email_body');
 
-            // Todo: switch to edit mode
+            openerp.website.editor_bar.edit();
             event.preventDefault();
         });
     });
index c478315..5dee573 100644 (file)
@@ -1,15 +1,15 @@
 <?xml version="1.0" encoding="utf-8"?>
 <openerp>
-<data>
+    <data>
 
-<!-- Email Designer main page -->
-<template id="designer_index" name="Email Designer">
+<!-- Template Choice page -->
+<template id="email_designer" name="Email Designer">
     <t t-call="website.layout">
         <t t-set="head">
             <script type="text/javascript" src="/website_mail/static/src/js/website_email_designer.js"></script>
         </t>
         <div id="wrap" class="container" t-ignore="True">
-            <div id="email_template" t-att-style="(object.body_html and len(templates)&gt;0) and 'display: none'" class="mb32">
+            <div id="email_template" class="mb32" t-att-style="mode != 'email_template' and 'display: none' or ''">
                 <a class="mt16 btn btn-default pull-right" 
                   t-attf-href="/web#return_label=Website&amp;model=#{model}&amp;id=#{res_id}&amp;view_type=form">
                     Back
@@ -34,7 +34,7 @@
                     </div>
                 </div>
             </div>
-            <div id="email_designer" t-att-style="not (object.body_html and len(templates)&gt; 0) and 'display: none'" class="mb32">
+            <div id="email_designer" class="mb32" t-att-style="mode != 'email_designer' and 'display: none' or ''">
                 <a class="mt16 btn btn-primary pull-right" 
                   t-attf-href="/web#return_label=Website&amp;model=#{model}&amp;id=#{res_id}&amp;view_type=form">
                     Save and Continue
                     Design Your Email
                 </h1>
                 <div class="form-horizontal">
-                    <div class="form-group">
+                    <!-- email_from fields-->
+                    <div class="form-group" t-if="email_from_field == 'email_from'">
                         <label class="col-sm-2 control-label">From:</label>
-                        <div class="col-sm-7">
-                            <span t-field="object.email_from" class="form-control"/>
-                        </div>
+                        <div class="col-sm-7"><span t-field="record.email_from" class="form-control"/></div>
+                    </div>
+                    <div class="form-group" t-if="email_from_field == 'email'">
+                        <label class="col-sm-2 control-label">From:</label>
+                        <div class="col-sm-7"><span t-field="record.email" class="form-control"/></div>
                     </div>
-                    <div class="form-group">
+                    <!-- email_from fields-->
+                    <div class="form-group" t-if="subject_field == 'subject'">
                         <label class="col-sm-2 control-label">Subject:</label>
-                        <div class="col-sm-7">
-                            <span t-field="object.name" class="form-control"/>
-                        </div>
+                        <div class="col-sm-7"><span t-field="record.subject" class="form-control"/></div>
+                    </div>
+                    <div class="form-group" t-if="subject_field == 'name'">
+                        <label class="col-sm-2 control-label">Subject:</label>
+                        <div class="col-sm-7"><span t-field="record.name" class="form-control"/></div>
                     </div>
                 </div>
                 <hr/>
-                <div t-field="object.body_html" id="email_body"/>
+                <!-- body fields -->
+                <div t-if="body_field == 'body_html'">
+                    <div t-field="record.body_html" id="email_body_html"/>
+                </div>
+                <div t-if="body_field == 'body'">
+                    <div t-field="record.body" id="email_body"/>
+                </div>
             </div>
         </div>
     </t>
     <t t-set="website.footer"></t>
 </template>
 
-</data>
+    </data>
 </openerp>