[IMP] rename list_pages to enumerate_pages, add a search method and a basic non-contiguous substring match

bzr revid: xmo@openerp.com-20131113101824-uqatvqokkzhvi0ei
This commit is contained in:
Xavier Morel 2013-11-13 11:18:24 +01:00
parent 9ca2b8e8e0
commit 122ddb8e31
3 changed files with 40 additions and 4 deletions

View File

@ -273,12 +273,12 @@ class Website(openerp.addons.web.controllers.main.Home):
@website.route('/sitemap', type='http', auth='public', multilang=True)
def sitemap(self):
return request.website.render('website.sitemap', {'pages': request.website.list_pages()})
return request.website.render('website.sitemap', {'pages': request.website.enumerate_pages()})
@website.route('/sitemap.xml', type='http', auth="public")
def sitemap_xml(self):
body = request.website.render('website.sitemap_xml', {
'pages': request.website.list_pages()
'pages': request.website.enumerate_pages()
})
return request.make_response(body, [

View File

@ -323,7 +323,7 @@ class website(osv.osv):
(arg == 'self' or arg in rule._converters)
for arg in args)
def list_pages(self, cr, uid, ids, context=None):
def enumerate_pages(self, cr, uid, ids, context=None):
""" Available pages in the website/CMS. This is mostly used for links
generation and can be overridden by modules setting up new HTML
controllers for dynamic pages (e.g. blog).
@ -363,6 +363,41 @@ class website(osv.osv):
domain_part, url = rule.build(values, append_unknown=False)
yield {'name': url, 'url': url }
def search_pages(self, cr, uid, ids, needle=None, limit=None, context=None):
pages = self.enumerate_pages(cr, uid, ids, context=context)
if needle:
pages = itertools.ifilter(lambda v: self.page_matches(
cr, uid, v, needle, context=None), pages)
return list(itertools.islice(pages, limit))
def page_matches(self, cr, uid, page, needle, context=None):
""" Checks that a "page" matches a user-provide search string.
The default implementation attempts to perform a non-contiguous
substring match of the page's name.
:param page: {'name': str, 'url': str}
:param needle: str
:rtype: bool
"""
haystack = page['name'].lower()
needle = iter(needle.lower())
n = next(needle)
end = object()
for char in haystack:
if char != n: continue
n = next(needle, end)
# found all characters of needle in haystack in order
if n is end:
return True
return False
def kanban(self, cr, uid, ids, model, domain, column, template, step=None, scope=None, orderby=None, context=None):
step = step and int(step) or 10
scope = scope and int(scope) or 5

View File

@ -930,9 +930,10 @@
fetch_pages: function () {
return openerp.jsonRpc('/web/dataset/call_kw', 'call', {
model: 'website',
method: 'list_pages',
method: 'search_pages',
args: [null],
kwargs: {
limit: 10,
context: website.get_context()
},
});