[FIX] website page creation

bzr revid: al@openerp.com-20140128140017-r8yy8tzsvo4y0e92
This commit is contained in:
Antony Lesuisse 2014-01-28 15:00:17 +01:00
parent 0f2c0f9b6b
commit 7c90d3afa5
5 changed files with 38 additions and 90 deletions

View File

@ -22,11 +22,8 @@ from openerp.addons.website.models import website
from openerp.addons.web import http
from openerp.addons.web.http import request, LazyResponse
from ..utils import slugify
logger = logging.getLogger(__name__)
NOPE = object()
# Completely arbitrary limits
MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT = IMAGE_LIMITS = (1024, 768)
@ -51,20 +48,10 @@ class Website(openerp.addons.web.controllers.main.Home):
return response
@http.route('/pagenew/<path:path>', type='http', auth="user", website=True)
def pagenew(self, path, noredirect=NOPE):
web = request.registry['website']
try:
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)
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:
def pagenew(self, path, noredirect=False):
xml_id = request.registry['website'].new_page(request.cr, request.uid, path, context=request.context)
url = "/page/" + xml_id
if noredirect:
return werkzeug.wrappers.Response(url, mimetype='text/plain')
return werkzeug.utils.redirect(url)

View File

@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
import fnmatch
import inspect
import itertools
import logging
import math
import itertools
import re
import urllib
import urlparse
@ -11,13 +12,16 @@ import simplejson
import werkzeug
import werkzeug.exceptions
import werkzeug.wrappers
# optional python-slugify import (https://github.com/un33k/python-slugify)
try:
import slugify as slugify_lib
except ImportError:
slugify_lib = None
import openerp
from openerp.osv import orm, osv, fields
from openerp.tools.safe_eval import safe_eval
from openerp.addons.web.http import request, LazyResponse
from ..utils import slugify
logger = logging.getLogger(__name__)
@ -49,6 +53,13 @@ def url_for(path_or_uri, lang=None, keep_query=None):
return location
def slugify(s, max_length=None):
if slugify_lib:
return slugify_lib.slugify(s, max_length)
spaceless = re.sub(r'\s+', '-', s)
specialless = re.sub(r'[^-_A-Za-z0-9]', '', spaceless)
return specialless[:max_length]
def slug(value):
if isinstance(value, orm.browse_record):
# [(id, name)] = value.name_get()
@ -117,37 +128,36 @@ class website(osv.osv):
return super(website, self).write(cr, uid, ids, vals, context)
def new_page(self, cr, uid, name, template='website.default_page', ispage=True, context=None):
context=context or {}
# completely arbitrary max_length
idname = slugify(name, max_length=50)
context = context or {}
imd = self.pool.get('ir.model.data')
view = self.pool.get('ir.ui.view')
template_module, template_name = template.split('.')
module, tmp_page = template.split('.')
view_model, view_id = imd.get_object_reference(cr, uid, module, tmp_page)
# completely arbitrary max_length
page_name = slugify(name, max_length=50)
page_xmlid = "%s.%s" % (template_module, page_name)
cr.execute('SAVEPOINT new_page')
try:
newview_id = view.copy(cr, uid, view_id, context=context)
newview = view.browse(cr, uid, newview_id, context=context)
newview.write({
'arch': newview.arch.replace(template, "%s.%s" % (module, idname)),
'name': name,
# existing page
imd.get_object_reference(cr, uid, template_module, page_name)
except ValueError:
# new page
_, template_id = imd.get_object_reference(cr, uid, template_module, template_name)
page_id = view.copy(cr, uid, template_id, context=context)
page = view.browse(cr, uid, page_id, context=context)
page.write({
'arch': page.arch.replace(template, page_xmlid),
'name': page_name,
'page': ispage,
})
imd.create(cr, uid, {
'name': idname,
'module': module,
'name': page_name,
'module': template_module,
'model': 'ir.ui.view',
'res_id': newview_id,
'res_id': page_id,
'noupdate': True
}, context=context)
cr.execute('RELEASE SAVEPOINT new_page')
return "%s.%s" % (module, idname)
except:
cr.execute("ROLLBACK TO SAVEPOINT new_page")
raise
return page_xmlid
def page_for_name(self, cr, uid, ids, name, module='website', context=None):
# whatever

View File

@ -1072,7 +1072,7 @@
} else {
// Create the page, get the URL back
done = $.get(_.str.sprintf(
'/pagenew/%s?noredirect', encodeURI(data.id)))
'/pagenew/%s?noredirect=1', encodeURI(data.id)))
.then(function (response) {
self.make_link(response, false, data.id);
});

View File

@ -1,13 +0,0 @@
# -*- coding: utf-8 -*-
import re
__all__ = ['slugify']
try:
# use python-slugify (https://github.com/un33k/python-slugify) if available
from slugify import slugify
except ImportError:
def slugify(s, max_length=None):
spaceless = re.sub(r'\s+', '-', s)
specialless = re.sub(r'[^-_A-Za-z0-9]', '', spaceless)
return specialless[:max_length]

View File

@ -715,42 +715,6 @@ Sitemap: <t t-esc="url_root"/>sitemap.xml
</t>
</template>
<template id="creation_failed">
<t t-call="website.layout">
<div id="wrap">
<div class="oe_structure">
<div class="container">
<h1 class="text-center">
The page "<em><t t-esc="page"/></em>"
already exists
</h1>
<h3 class="text-center text-muted">We could not create it.</h3>
<ul>
<li>
You can <a t-href="#{path}">visit the existing page</a>
</li>
<li>Or you can <a href="#" class="create-new-page">
create an other page.</a></li>
</ul>
</div>
</div>
</div>
<script type="application/javascript">
$(document.body).on('click', 'a.create-new-page', function (e) {
e.preventDefault();
openerp.website.prompt({
window_title: "New Page",
input: "Page Title",
}).then(function (val) {
if (val) {
document.location = '/pagenew/' + encodeURI(val);
}
});
});
</script>
</t>
</template>
<template id="contact">
<address t-ignore="true" class="mb0">
<div t-att-class="'name' not in fields and 'css_non_editable_mode_hidden'"><span t-esc="name"/></div>