[views] Refactor jinja handling
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 03 Feb 2017 14:20:48 +0100
changeset 61 43b191bd84aa
parent 60 a6fa6749d820
child 62 eec896494026
[views] Refactor jinja handling * turn JinjaViewMixin class into a function, * introduce JinjaStaticView abstract base class and a simple jinja_static_view to build concret sub-classes, * use this to build ProjectView, UtilisationView and SedaView, dropping the static.py module along the way.
views/__init__.py
views/static.py
views/templates.py
--- a/views/__init__.py	Thu Feb 02 15:51:23 2017 +0100
+++ b/views/__init__.py	Fri Feb 03 14:20:48 2017 +0100
@@ -16,12 +16,43 @@
 
 from jinja2 import Environment, PackageLoader
 
-env = Environment(loader=PackageLoader('cubes.sherpa.views'))
+from cubicweb import _
+from cubicweb.view import View
+
+_JINJA_ENV = Environment(loader=PackageLoader('cubes.sherpa.views'))
+
+
+def jinja_render(template_name, **ctx):
+    """Return a string containing result of rendering of Jinja2's `template_name` with
+    `ctx` as context.
+    """
+    template = _JINJA_ENV.get_template(template_name + '.jinja2')
+    return template.render(**ctx)
 
 
-class JinjaViewMixin(object):
+class JinjaStaticView(View):
+    """Abstract base class to render static pages from a jinja template."""
+    __abstract__ = True
     template_name = None
+    title = None
+
+    def call(self, **kw):
+        self.w(jinja_render(self.template_name,
+                            title=self._cw._(self.title),
+                            data_url=self._cw.datadir_url))
+
 
-    def call_template(self, **ctx):
-        t = env.get_template(self.template_name)
-        self.w(t.render(**ctx))
+def jinja_static_view(template_name, title):
+    """Generate a sub-class of JinjaStaticView parametrized with its `template_name` and `title`.
+
+    `__regid__` is built by prepending 'sherpa.' to template_name.
+    """
+    class_name = template_name.capitalize() + 'View'
+    return type(class_name, (JinjaStaticView,), {'__regid__': 'sherpa.' + template_name,
+                                                 'template_name': template_name,
+                                                 'title': title})
+
+
+ProjectView = jinja_static_view('project', _('Le projet Sherpa'))
+UtilisationView = jinja_static_view('utilisation', _("Mode d'emploi"))
+SedaView = jinja_static_view('seda', _('Le SEDA'))
--- a/views/static.py	Thu Feb 02 15:51:23 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-from cubicweb.view import View
-
-from cubes.sherpa.views import JinjaViewMixin
-
-class ProjectView(View, JinjaViewMixin):
-    __regid__ = 'sherpa-project'
-    template_name = 'project.jinja2'
-
-
-    def call(self, **kw):
-        req = self._cw
-        _ = req._
-
-        self.call_template(
-            title=_('Le projet Sherpa'),
-            data_url=self._cw.datadir_url,
-        )
-
-
-class UtilisationView(View, JinjaViewMixin):
-    __regid__ = 'sherpa-utilisation'
-    template_name = 'utilisation.jinja2'
-
-
-    def call(self, **kw):
-        req = self._cw
-        _ = req._
-
-        self.call_template(
-            title=_("Mode d'emploi"),
-            data_url=self._cw.datadir_url,
-        )
-
-class SedaView(View, JinjaViewMixin):
-    __regid__ = 'sherpa-seda'
-    template_name = 'seda.jinja2'
-
-
-    def call(self, **kw):
-        req = self._cw
-        _ = req._
-
-        self.call_template(
-            title=_("Le SEDA"),
-            data_url=self._cw.datadir_url,
-        )
--- a/views/templates.py	Thu Feb 02 15:51:23 2017 +0100
+++ b/views/templates.py	Fri Feb 03 14:20:48 2017 +0100
@@ -21,7 +21,8 @@
 from logilab.common.decorators import monkeypatch
 from cubicweb.utils import json_dumps, HTMLHead, UStringIO
 from cubicweb.web.views import basetemplates
-from . import JinjaViewMixin
+
+from . import jinja_render
 
 
 # Bootstrap configuration.
@@ -36,9 +37,7 @@
   %s
 });""" % jscode)
 
-class SherpaMainTemplate(JinjaViewMixin, basetemplates.TheMainTemplate):
-
-    template_name = 'maintemplate.jinja2'
+class SherpaMainTemplate(basetemplates.TheMainTemplate):
 
     def _handle_added_resources(self, tmpl_context):
         """fetch all resources added with add_{js,css}, etc.
@@ -73,7 +72,7 @@
         context.update(getattr(view, 'template_context', lambda: {})())
         self._handle_added_resources(context)
         context['page_content'] = page_content
-        self.call_template(**context)
+        self.w(jinja_render('maintemplate', **context))
 
 
     def template_context(self, view):