view roundup/cgi/engine_jinja2.py @ 6823:fe0091279f50

Refactor session db logging and key generation for sessions/otks While I was working on the redis sessiondb stuff, I noticed that log_wanrning, get_logger ... was duplicated. Also there was code to generate a unique key for otks that was duplicated. Changes: creating new sessions_common.py and SessionsCommon class to provide methods: log_warning, log_info, log_debug, get_logger, getUniqueKey getUniqueKey method is closer to the method used to make session keys in client.py. sessions_common.py now report when random_.py chooses a weak random number generator. Removed same from rest.py. get_logger reconciles all logging under roundup.hyperdb.backends.<name of BasicDatabase class> some backends used to log to root logger. have BasicDatabase in other sessions_*.py modules inherit from SessionCommon. change logging to use log_* methods. In addition: remove unused imports reported by flake8 and other formatting changes modify actions.py, rest.py, templating.py to use getUniqueKey method. add tests for new methods test_redis_session.py swap out ModuleNotFoundError for ImportError to prevent crash in python2 when redis is not present. allow injection of username:password or just password into redis connection URL. set pytest_redis_pw envirnment variable to password or user:password when running test.
author John Rouillard <rouilj@ieee.org>
date Sun, 07 Aug 2022 01:51:11 -0400
parents ec1f725f5c91
children 07ce4e4110f5
line wrap: on
line source

"""
Experimental Jinja2 support for Roundup. It will become less
experimental when it is completely clear what information is
passed to template, and when the info is limited to the sane
minimal set (to avoid Roundup state changes from template).

[ ] fallback mechanizm to use multiple templating engines in
    parallel and aid in incremental translation from one
    engine to another

[ ] define a place for templates
    probably
      TRACKER_HOME/templates/jinja2
    with
      TRACKER_HOME/templates/INFO.txt
        describing how the dir was created, for example
          "This is a copy of 'classic' template from ..."
        also template fallback mechanizm for multi-engine
          configuration
    [ ] backward compatibility - if no engine is explicitly
          specified, use TRACKER_HOME/html directory
    [ ] copy TEMPLATES-INFO.txt to INFO.txt
      [ ] implement VERSION file in environment for auto
          upgrade

[ ] precompile() is a stub

[ ] add {{ debug() }} dumper to inspect available variables
    https://github.com/mitsuhiko/jinja2/issues/174
"""

from __future__ import print_function
import jinja2
import gettext
import mimetypes
import sys

# http://jinja.pocoo.org/docs/api/#loaders

from roundup.cgi.templating import context, LoaderBase, TemplateBase
from roundup.anypy.strings import s2u


class Jinja2Loader(LoaderBase):
    def __init__(self, dir):
        self._env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(dir),
            extensions=['jinja2.ext.i18n'],
            autoescape=True
        )

        # Adding a custom filter that can transform roundup's vars to unicode
        # This is necessary because jinja2 can only deal with unicode objects
        # and roundup uses utf-8 for the internal representation.
        # The automatic conversion will assume 'ascii' and fail sometime.
        # Analysed with roundup 1.5.0 and jinja 2.7.1. See issue2550811.
        self._env.filters["u"] = s2u

    def _find(self, tplname):
        for extension in ('', '.html', '.xml'):
            try:
                filename = tplname + extension
                return self._env.get_template(filename)
            except jinja2.TemplateNotFound:
                continue

        return None

    def check(self, tplname):
        return bool(self._find(tplname))

    def load(self, tplname):
        tpl = self._find(tplname)
        pt = Jinja2ProxyPageTemplate(tpl)
        pt.content_type = mimetypes.guess_type(tpl.filename)[0] or 'text/html'
        return pt

    def precompile(self):
        pass


class Jinja2ProxyPageTemplate(TemplateBase):
    def __init__(self, template):
        self._tpl = template

    def render(self, client, classname, request, **options):
        # [ ] limit the information passed to the minimal necessary set
        c = context(client, self, classname, request)

        c.update({'options': options,
                  'gettext': lambda s: s2u(client.gettext(s)),
                  'ngettext': lambda s, p, n: s2u(client.ngettext(s, p, n))})
        s = self._tpl.render(c)
        return s if sys.version_info[0] > 2 else \
            s.encode(client.STORAGE_CHARSET, )

    def __getitem__(self, name):
        # [ ] figure out what are these for
        raise NotImplementedError
        # return self._pt[name]

    def __getattr__(self, name):
        # [ ] figure out what are these for
        raise NotImplementedError
        # return getattr(self._pt, name)

Roundup Issue Tracker: http://roundup-tracker.org/