[ADD] creation of new page from link dialog

* Replace select by input with dropdown

* Add random crap to /pagenew so it does not blow up when a butterfly
  flaps its wings, also so it's possible to call it through xhr and
  get something out of it

* Fix duplicated save() call in link dialog widget thing

bzr revid: xmo@openerp.com-20130903143237-6pwsbqzc02bv3mri
This commit is contained in:
Xavier Morel 2013-09-03 16:32:37 +02:00
parent 2dfe9f2bed
commit c89d31ca95
4 changed files with 82 additions and 32 deletions

View File

@ -1,19 +1,21 @@
# -*- coding: utf-8 -*-
import base64
import cStringIO
import hashlib
import json
import logging
import cStringIO
import os
import psycopg2
import werkzeug
import werkzeug.exceptions
import werkzeug.utils
import werkzeug.wrappers
from PIL import Image
import openerp
from openerp.addons.web import http
from openerp.addons.web.http import request
import werkzeug
import werkzeug.exceptions
import werkzeug.wrappers
import hashlib
import os
logger = logging.getLogger(__name__)
@ -26,7 +28,7 @@ def auth_method_public():
request.uid = request.session.uid
http.auth_methods['public'] = auth_method_public
NOPE = object()
# PIL images have a type flag, but no MIME. Reverse type flag to MIME.
PIL_MIME_MAPPING = {'PNG': 'image/png', 'JPEG': 'image/jpeg', 'GIF': 'image/gif', }
# Completely arbitrary limits
@ -41,30 +43,42 @@ class Website(openerp.addons.web.controllers.main.Home):
return super(Website, self).index(*args, **kw)
@http.route('/pagenew/<path:path>', type='http', auth="admin")
def pagenew(self, path):
def pagenew(self, path, noredirect=NOPE):
if '.' in path:
module, idname = path.split('.', 1)
else:
module = 'website'
idname = path
path = "%s.%s" % (module, idname)
request.cr.execute('SAVEPOINT pagenew')
imd = request.registry['ir.model.data']
view = request.registry['ir.ui.view']
view_model, view_id = imd.get_object_reference(request.cr, request.uid, 'website', 'default_page')
newview_id = view.copy(request.cr, request.uid, view_id)
newview = view.browse(request.cr, request.uid, newview_id, context={})
newview = view.browse(request.cr, request.uid, newview_id)
newview.write({
'arch': newview.arch.replace("website.default_page", path),
'name': "page/%s" % path,
'page': True,
})
if '.' in path:
module, idname = path.split('.')
# Fuck it, we're doing it live
try:
imd.create(request.cr, request.uid, {
'name': idname,
'module': module,
'model': 'ir.ui.view',
'res_id': newview_id,
'noupdate': True
})
except psycopg2.IntegrityError:
request.cr.execute('ROLLBACK TO SAVEPOINT pagenew')
else:
module = False
idname = path
imd.create(request.cr, request.uid, {
'name': idname,
'module': module,
'model': 'ir.ui.view',
'res_id': newview_id,
'noupdate': True
})
return werkzeug.utils.redirect("/page/%s" % path)
request.cr.execute('RELEASE SAVEPOINT pagenew')
url = "/page/%s" % path
if noredirect is not NOPE:
return werkzeug.wrappers.Response(url, mimetype='text/plain')
return werkzeug.utils.redirect(url)
@http.route('/website/theme_change', type='http', auth="admin")
def theme_change(self, theme_id=False, **kwargs):

View File

@ -330,12 +330,15 @@
website.editor.LinkDialog = website.editor.Dialog.extend({
template: 'website.editor.dialog.link',
events: _.extend({}, website.editor.Dialog.prototype.events, {
'click button.btn-primary': 'save',
'change .url-source': function (e) { this.changed($(e.target)); },
'click div.existing a': 'select_page',
}),
init: function (editor) {
this._super(editor);
// url -> name mapping for existing pages
this.pages = Object.create(null);
// name -> url mapping for the same
this.pages_by_name = Object.create(null);
},
start: function () {
var element;
@ -393,17 +396,30 @@
}
},
save: function () {
var self = this, _super = this._super.bind(this);
var $e = this.$('.url-source').filter(function () { return !!this.value; });
var val = $e.val();
var val = $e.val(), done = $.when();
if ($e.hasClass('email-address')) {
this.make_link('mailto:' + val, false, val);
} else if ($e.hasClass('pages')) {
this.make_link(val, false, $e.find('option:selected').text());
// ``val`` is the *name* of the page
var url = this.pages_by_name[val];
if (!url) {
// Create the page, get the URL back
done = $.get(_.str.sprintf(
'/pagenew/%s?noredirect', encodeURIComponent(val)))
.then(function (response) {
url = response;
});
}
done.then(function () {
self.make_link(url, false, val);
});
} else {
this.make_link(val, this.$('input.window-new').prop('checked'));
}
this._super();
done.then(_super);
},
bind_data: function () {
var href = this.element && (this.element.data( 'cke-saved-href')
@ -414,7 +430,7 @@
if (match = /(mailto):(.+)/.exec(href)) {
$control = this.$('input.email-address').val(match[2]);
} else if(href in this.pages) {
$control = this.$('select.pages').val(href);
$control = this.$('input.pages').val(this.pages[href]);
}
if (!$control) {
$control = this.$('input.url').val(href);
@ -430,6 +446,18 @@
.siblings().removeClass('active');
this.$('.url-source').not($e).val('');
},
/**
* Selected an existing page in dropdown
*/
select_page: function (e) {
e.preventDefault();
e.stopPropagation();
var $target = $(e.target);
this.$('input.pages').val($target.text()).change();
// No #dropdown('close'), and using #dropdown('toggle') sur
// #closest('.dropdown') makes the dropdown not work correctly
$target.closest('.open').removeClass('open')
},
/**
* CKEDITOR.plugins.link.getSelectedLink ignores the editor's root,
* if the editor is set directly on a link it will thus not work.
@ -457,11 +485,12 @@
},
fill_pages: function (results) {
var self = this;
var $select = this.$('select');
$select.append(new Option());
var $pages = this.$('div.existing ul').empty();
_(results).each(function (result) {
self.pages[result.url] = true;
$select.append(new Option(result.name, result.url));
self.pages[result.url] = result.name;
self.pages_by_name[result.name] = result.url;
var $link = $('<a>').attr('href', result.url).text(result.name);
$('<li>').append($link).appendTo($pages);
});
},
});

View File

@ -38,7 +38,14 @@
<ul class="list-group form-group">
<li class="list-group-item active">
<h3 class="list-group-item-heading">Page on your website</h3>
<select class="form-control pages url-source"/>
<div class="input-group">
<input type="text" class="form-control pages url-source"
placeholder="Create new page"/>
<div class="input-group-btn existing dropdown">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Existing page <span class="caret"></span></button>
<ul class="dropdown-menu pull-right"/>
</div>
</div>
</li>
<li class="list-group-item">
<div class="pull-right">

View File

@ -133,7 +133,7 @@ class website(osv.osv):
# check if xmlid of the template exists
try:
model, xmlid = template.split('.')
model, xmlid = template.split('.', 1)
model, id = IMD.get_object_reference(request.cr, request.uid, model, xmlid)
except ValueError:
logger.error("Website Rendering Error.\n\n%s" % traceback.format_exc())