diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index c5628765ed8..a214e6cd334 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -53,7 +53,12 @@ class Website(openerp.addons.web.controllers.main.Home): path = web.new_page(request.cr, request.uid, path, context=request.context) except psycopg2.IntegrityError: logger.exception('Unable to create ir_model_data for page %s', path) - return werkzeug.exceptions.InternalServerError() + response = request.website.render('website.creation_failed', { + 'page': path, + 'path': '/page/' + request.website.page_for_name(name=path) + }) + response.status_code = 409 + return response url = "/page/" + path if noredirect is not NOPE: return werkzeug.wrappers.Response(url, mimetype='text/plain') diff --git a/addons/website/models/website.py b/addons/website/models/website.py index 43b0be03831..62844305403 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -143,6 +143,20 @@ class website(osv.osv): cr.execute("ROLLBACK TO SAVEPOINT new_page") raise + def page_for_name(self, cr, uid, ids, name, module='website', context=None): + # whatever + return '%s.%s' % (module, slugify(name, max_length=50)) + + def page_exists(self, cr, uid, ids, name, module='website', context=None): + page = self.page_for_name(cr, uid, ids, name, module=module, context=context) + + try: + return self.get_template( + cr, uid, ids, template=page, context=context + ).exists() + except: + return False + def get_public_user(self, cr, uid, context=None): if not self.public_user: uid = openerp.SUPERUSER_ID @@ -185,14 +199,14 @@ class website(osv.osv): }) def get_template(self, cr, uid, ids, template, context=None): - IMD = self.pool.get("ir.model.data") + IMD = self.pool["ir.model.data"] try: module, xmlid = template.split('.', 1) - view_ref = IMD.get_object_reference(cr, uid, module, xmlid) + model, id = IMD.get_object_reference(cr, uid, module, xmlid) except ValueError: # catches both unpack errors and gor errors module, xmlid = 'website', template - view_ref = IMD.get_object_reference(cr, uid, module, xmlid) - return self.pool.get("ir.ui.view").browse(cr, uid, view_ref[1]) + model, id = IMD.get_object_reference(cr, uid, module, xmlid) + return self.pool["ir.ui.view"].browse(cr, uid, id, context=context) def _render(self, cr, uid, ids, template, values=None, context=None): user = self.pool.get("res.users") diff --git a/addons/website/static/src/js/website.editor.js b/addons/website/static/src/js/website.editor.js index 52b3f19e1ba..c50ad392887 100644 --- a/addons/website/static/src/js/website.editor.js +++ b/addons/website/static/src/js/website.editor.js @@ -888,7 +888,8 @@ this._super(editor); this.text = null; // Store last-performed request to be able to cancel/abort it. - this.req = null; + this.page_exists_req = null; + this.search_pages_req = null; }, start: function () { var self = this; @@ -896,15 +897,20 @@ minimumInputLength: 1, placeholder: _t("New or existing page"), query: function (q) { - self.fetch_pages(q.term).then(function (results) { + $.when( + self.page_exists(q.term), + self.fetch_pages(q.term) + ).then(function (exists, results) { var rs = _.map(results, function (r) { return { id: r.url, text: r.name, }; }); - rs.push({ - create: true, - id: q.term, - text: _.str.sprintf(_t("Create page '%s'"), q.term), - }); + if (!exists) { + rs.push({ + create: true, + id: q.term, + text: _.str.sprintf(_t("Create page '%s'"), q.term), + }); + } q.callback({ more: false, results: rs @@ -983,20 +989,30 @@ .siblings().removeClass('active') .addBack().removeClass('has-error'); }, - fetch_pages: function (term) { + call: function (method, args, kwargs) { var self = this; - if (this.req) { this.req.abort(); } - return this.req = openerp.jsonRpc('/web/dataset/call_kw', 'call', { + var req = method + '_req'; + + if (this[req]) { this[req].abort(); } + + return this[req] = openerp.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website', - method: 'search_pages', - args: [null, term], - kwargs: { - limit: 9, - context: website.get_context() - }, + method: method, + args: args, + kwargs: kwargs, }).always(function () { - // request completed -> unstore it - self.req = null; + self[req] = null; + }); + }, + page_exists: function (term) { + return this.call('page_exists', [null, term], { + context: website.get_context(), + }); + }, + fetch_pages: function (term) { + return this.call('search_pages', [null, term], { + limit: 9, + context: website.get_context(), }); }, }); diff --git a/addons/website/static/src/xml/website.xml b/addons/website/static/src/xml/website.xml index b35dd8a9cfc..d5368ec7b94 100644 --- a/addons/website/static/src/xml/website.xml +++ b/addons/website/static/src/xml/website.xml @@ -72,16 +72,21 @@
- diff --git a/addons/website/views/website_templates.xml b/addons/website/views/website_templates.xml index 375cac64dfb..da9f80375f4 100644 --- a/addons/website/views/website_templates.xml +++ b/addons/website/views/website_templates.xml @@ -629,5 +629,41 @@ Sitemap: sitemap.xml + +