[PATCH 2 of 8 sherpa] Extend jinja_static_view to handle path registration

Sylvain Thenault sylvain.thenault at logilab.fr
Tue Feb 28 17:08:16 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1488277529 -3600
#      Tue Feb 28 11:25:29 2017 +0100
# Node ID 2b4c0dca2466360202398abb4e6b3445f196219a
# Parent  c2b5337dbc99121eaa376ff1f4ba8114cb5b36af
Extend jinja_static_view to handle path registration

Also add `ignore_baseclass_rules = True` to our rewriter, else we ends up
checking the base views again and again (one for the cubicweb class, another for
its subclass in the seda cube, another for its subclass in this cube, etc...).

diff --git a/cubicweb_sherpa/views/__init__.py b/cubicweb_sherpa/views/__init__.py
--- a/cubicweb_sherpa/views/__init__.py
+++ b/cubicweb_sherpa/views/__init__.py
@@ -18,10 +18,11 @@
 from jinja2 import Environment, PackageLoader, select_autoescape
 
 from cubicweb.view import View
 from cubicweb.web.views import urlrewrite, startup
 
+_REWRITE_RULES = []
 _JINJA_ENV = Environment(loader=PackageLoader('cubicweb_sherpa.views'),
                          autoescape=select_autoescape(enabled_extensions=('html',)))
 
 
 def jinja_render(template_name, **ctx):
@@ -42,37 +43,44 @@ class JinjaStaticView(View):
         self.w(jinja_render(self.template_name,
                             title=self._cw._(self.title),
                             data_url=self._cw.datadir_url))
 
 
-def jinja_static_view(template_name, title=None, regid=None):
+def jinja_static_view(template_name, title=None, regid=None, path=None):
     """Generate a sub-class of JinjaStaticView parametrized with its `template_name` and `title`.
 
-    `__regid__` is built by prepending 'sherpa.' to template_name.
+    `__regid__` is built by prepending 'sherpa.' to template_name or may be explicitly specified
+    using `regid`.
+
+    A path to access to view is automatically generated and will match `template_name` unless
+    explicitly specified using `path` argument.
     """
     class_name = template_name.capitalize() + 'View'
     if regid is None:
         regid = 'sherpa.' + template_name
 
+    if path is None:
+        path = '/' + template_name
+    _REWRITE_RULES.append((path, {'vid': regid}))
+
     return type(class_name, (JinjaStaticView,), {'__regid__': regid,
                                                  'template_name': template_name,
                                                  'title': title})
 
 
 ProjectView = jinja_static_view('project', u'Sherpa un générateur de profils')
 UtilisationView = jinja_static_view('utilisation', u'Pour commencer')
 SedaView = jinja_static_view('seda', u'Le SEDA')
-IndexView = jinja_static_view('index', regid='index')
+IndexView = jinja_static_view('index', regid='index', path='/')
 
 
+# add our rewrite rules, has to be done once the list if filled because of metaclass magic
 class SherpaReqRewriter(urlrewrite.SimpleReqRewriter):
-    rules = [
-        ('/', dict(vid='sherpa.index')),
-        ('/project', dict(vid='sherpa.project')),
-        ('/utilisation', dict(vid='sherpa.utilisation')),
-        ('/seda', dict(vid='sherpa.seda')),
-    ]
+    ignore_baseclass_rules = True
+    global _REWRITE_RULES
+    rules = tuple(_REWRITE_RULES)
+    del _REWRITE_RULES
 
 
 def registration_callback(vreg):
     vreg.register_all(globals().values(), __name__, (IndexView,))
     vreg.register_and_replace(IndexView, startup.IndexView)


More information about the saem-devel mailing list