cubicweb logo

Table Of Contents

Previous topic

XML and RSS views

Next topic

Breadcrumbs

This Page

URL publishing

(cubicweb.web.views.urlpublishing)

Associate url’s path to view identifier / rql queries.

It currently handles url path with the forms:

  • <publishing_method>
  • minimal REST publishing:
    • <eid>
    • <etype>[/<attribute name>/<attribute value>]*
  • folder navigation

You can actually control URL (more exactly path) resolution using an URL path evaluator.

Note

Actionpath and Folderpath execute a query whose results is lost because of redirecting instead of direct traversal.

class cubicweb.web.views.urlpublishing.URLPublisherComponent(vreg, default_method='view')

Associate url path to view identifier / rql queries, by applying a chain of urlpathevaluator components.

An evaluator is a URLPathEvaluator subclass with an .evaluate_path method taking the request object and the path to publish as argument. It will either return a publishing method identifier and an rql query on success or raise a PathDontMatch exception on failure. URL evaluators are called according to their priority attribute, with 0 as the greatest priority and greater values as lower priority. The first evaluator returning a result or raising something else than PathDontMatch will stop the handlers chain.

process(req, path)

Given an url (essentialy caracterized by a path on the server, but additional information may be found in the request object), return a publishing method identifier (e.g. controller) and an optional result set.

Parameters:
  • req (cubicweb.web.request.CubicWebRequestBase) – the request object
  • path (str) – the path of the resource to publish
Return type:

tuple(str, cubicweb.rset.ResultSet or None)

Returns:

the publishing method identifier and an optional result set

Raises NotFound:
 

if no handler is able to decode the given path

URL rewriting

(cubicweb.web.views.urlrewrite)

class cubicweb.web.views.urlrewrite.URLRewriter(req, **extra)

Base class for URL rewriters.

Url rewriters should have a rules dict that maps an input URI to something that should be used for rewriting.

The actual logic that defines how the rules dict is used is implemented in the rewrite method.

A priority attribute might be used to indicate which rewriter should be tried first. The higher the priority is, the earlier the rewriter will be tried.

class cubicweb.web.views.urlrewrite.SimpleReqRewriter(req, **extra)

The SimpleReqRewriters uses a rules dict that maps input URI (regexp or plain string) to a dictionary to update the request’s form.

If the input uri is a regexp, group substitution is allowed.

rewrite(req, uri)
for each input, output `in rules, if `uri matches input, req’s form is updated with output
class cubicweb.web.views.urlrewrite.SchemaBasedRewriter(req, **extra)
Here, the rules dict maps regexps or plain strings to callbacks that will be called with inputurl, uri, req, schema as parameters.

SimpleReqRewriter is enough for a certain number of simple cases. If it is not sufficient, SchemaBasedRewriter allows to do more elaborate things.

Here is an example of SimpleReqRewriter usage with plain string:

from cubicweb.web.views.urlrewrite import SimpleReqRewriter
class TrackerSimpleReqRewriter(SimpleReqRewriter):
    rules = [
     ('/versions', dict(vid='versionsinfo')),
     ]

When the url is <base_url>/versions, the view with the __regid__ versionsinfo is displayed.

Here is an example of SimpleReqRewriter usage with regular expressions:

from cubicweb.web.views.urlrewrite import (
    SimpleReqRewriter, rgx)

class BlogReqRewriter(SimpleReqRewriter):
    rules = [
        (rgx('/blogentry/([a-z_]+)\.rss'),
         dict(rql=('Any X ORDERBY CD DESC LIMIT 20 WHERE X is BlogEntry,'
                   'X creation_date CD, X created_by U, '
                   'U login "%(user)s"'
                   % {'user': r'\1'}, vid='rss'))),
        ]

When a url matches the regular expression, the view with the __regid__ rss which match the result set is displayed.

Here is an example of SchemaBasedRewriter usage:

from cubicweb.web.views.urlrewrite import (
    SchemaBasedRewriter, rgx, build_rset)

class TrackerURLRewriter(SchemaBasedRewriter):
    rules = [
        (rgx('/project/([^/]+)/([^/]+)/tests'),
         build_rset(rql='Version X WHERE X version_of P, P name %(project)s, X num %(num)s',
                    rgxgroups=[('project', 1), ('num', 2)], vid='versiontests')),
        ]