doc/book/devweb/views/urlpublish.rst
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 05 Apr 2019 17:58:19 +0200
changeset 12567 26744ad37953
parent 10491 c67bcee93248
permissions -rw-r--r--
Drop python2 support This mostly consists in removing the dependency on "six" and updating the code to use only Python3 idioms. Notice that we previously used TemporaryDirectory from cubicweb.devtools.testlib for compatibility with Python2. We now directly import it from tempfile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
     2
5301
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     3
URL publishing
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     4
--------------
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     5
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     6
(:mod:`cubicweb.web.views.urlpublishing`)
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     7
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     8
.. automodule:: cubicweb.web.views.urlpublishing
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     9
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    10
.. autoclass:: cubicweb.web.views.urlpublishing.URLPublisherComponent
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    11
   :members:
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    12
8039
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    13
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    14
You can write your own *URLPathEvaluator* class to handle custom paths.
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    15
For instance, if you want */my-card-id* to redirect to the corresponding
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    16
card's primary view, you would write:
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    17
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    18
.. sourcecode:: python
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    19
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    20
    class CardWikiidEvaluator(URLPathEvaluator):
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    21
        priority = 3 # make it be evaluated *before* RestPathEvaluator
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    22
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    23
        def evaluate_path(self, req, segments):
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    24
            if len(segments) != 1:
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    25
                raise PathDontMatch()
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    26
            rset = req.execute('Any C WHERE C wikiid %(w)s',
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    27
                               {'w': segments[0]})
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    28
            if len(rset) == 0:
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    29
                # Raise NotFound if no card is found
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    30
                raise PathDontMatch()
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    31
            return None, rset
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    32
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    33
On the other hand, you can also deactivate some of the standard
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    34
evaluators in your final application. The only thing you have to
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    35
do is to unregister them, for instance in a *registration_callback*
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    36
in your cube:
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    37
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    38
.. sourcecode:: python
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    39
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    40
    def registration_callback(vreg):
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    41
        vreg.unregister(RestPathEvaluator)
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    42
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    43
You can even replace the :class:`cubicweb.web.views.urlpublishing.URLPublisherComponent`
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    44
class if you want to customize the whole toolchain process or if you want
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    45
to plug into an early enough extension point to control your request
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    46
parameters:
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    47
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    48
.. sourcecode:: python
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    49
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    50
    class SanitizerPublisherComponent(URLPublisherComponent):
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    51
        """override default publisher component to explicitly ignore
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    52
        unauthorized request parameters in anonymous mode.
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    53
        """
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    54
        unauthorized_form_params = ('rql', 'vid', '__login', '__password')
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    55
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    56
        def process(self, req, path):
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    57
            if req.session.anonymous_session:
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    58
                self._remove_unauthorized_params(req)
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    59
            return super(SanitizerPublisherComponent, self).process(req, path)
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    60
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    61
        def _remove_unauthorized_params(self, req):
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    62
            for param in req.form.keys():
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    63
                if param in self.unauthorized_form_params:
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    64
                     req.form.pop(param)
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    65
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    66
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    67
    def registration_callback(vreg):
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    68
        vreg.register_and_replace(SanitizerPublisherComponent, URLPublisherComponent)
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    69
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    70
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    71
.. autoclass:: cubicweb.web.views.urlpublishing.RawPathEvaluator
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    72
.. autoclass:: cubicweb.web.views.urlpublishing.EidPathEvaluator
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    73
.. autoclass:: cubicweb.web.views.urlpublishing.URLRewriteEvaluator
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    74
.. autoclass:: cubicweb.web.views.urlpublishing.RestPathEvaluator
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    75
.. autoclass:: cubicweb.web.views.urlpublishing.ActionPathEvaluator
1fe90d4ec307 [doc] add some documentation on url path evaulators
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8032
diff changeset
    76
5301
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    77
URL rewriting
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    78
-------------
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
5301
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    80
(:mod:`cubicweb.web.views.urlrewrite`)
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    81
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    82
.. autoclass:: cubicweb.web.views.urlrewrite.URLRewriter
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    83
   :members:
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    84
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    85
.. autoclass:: cubicweb.web.views.urlrewrite.SimpleReqRewriter
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    86
   :members:
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    87
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    88
.. autoclass:: cubicweb.web.views.urlrewrite.SchemaBasedRewriter
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    89
   :members:
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    90
f4219a6e62e3 [doc/book] stuff a bit the publishing process (including various docstrings)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    91
5313
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    92
``SimpleReqRewriter`` is enough for a certain number of simple cases. If it is not sufficient, ``SchemaBasedRewriter`` allows to do more elaborate things.
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    93
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    94
Here is an example of ``SimpleReqRewriter`` usage with plain string:
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    95
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    96
.. sourcecode:: python
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    97
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    98
   from cubicweb.web.views.urlrewrite import SimpleReqRewriter
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
    99
   class TrackerSimpleReqRewriter(SimpleReqRewriter):
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   100
       rules = [
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   101
        ('/versions', dict(vid='versionsinfo')),
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   102
        ]
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   103
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   104
When the url is `<base_url>/versions`, the view with the __regid__ `versionsinfo` is displayed.
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   105
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   106
Here is an example of ``SimpleReqRewriter`` usage with regular expressions:
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   107
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   108
.. sourcecode:: python
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   109
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   110
    from cubicweb.web.views.urlrewrite import (
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   111
        SimpleReqRewriter, rgx)
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   112
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   113
    class BlogReqRewriter(SimpleReqRewriter):
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   114
        rules = [
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   115
            (rgx('/blogentry/([a-z_]+)\.rss'),
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   116
             dict(rql=('Any X ORDERBY CD DESC LIMIT 20 WHERE X is BlogEntry,'
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   117
                       'X creation_date CD, X created_by U, '
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   118
                       'U login "%(user)s"'
8032
bcb87336c7d2 [doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5394
diff changeset
   119
                       % {'user': r'\1'}), vid='rss'))
5313
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   120
            ]
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   121
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   122
When a url matches the regular expression, the view with the __regid__
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   123
`rss` which match the result set is displayed.
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   124
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   125
Here is an example of ``SchemaBasedRewriter`` usage:
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   126
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   127
.. sourcecode:: python
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   128
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   129
    from cubicweb.web.views.urlrewrite import (
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   130
        SchemaBasedRewriter, rgx, build_rset)
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   131
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   132
    class TrackerURLRewriter(SchemaBasedRewriter):
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   133
        rules = [
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   134
            (rgx('/project/([^/]+)/([^/]+)/tests'),
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   135
             build_rset(rql='Version X WHERE X version_of P, P name %(project)s, X num %(num)s',
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   136
                        rgxgroups=[('project', 1), ('num', 2)], vid='versiontests')),
c4fe397379c7 [doc/book] complete the urlrewrite sections with examples
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 5301
diff changeset
   137
            ]