From 67f8000683bb01dda2fd236b1ee989f3be4c9a88 Mon Sep 17 00:00:00 2001 From: Marcin Kuzminski Date: Mon, 24 Jul 2017 14:28:19 +0200 Subject: [PATCH] core: use a custom filter for rendering all mako templates. - brings old pylons function that converted None into a empty markup --- rhodecode/config/middleware.py | 5 +++++ rhodecode/lib/base.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/rhodecode/config/middleware.py b/rhodecode/config/middleware.py index 64dd3fc5..243b7cec 100644 --- a/rhodecode/config/middleware.py +++ b/rhodecode/config/middleware.py @@ -273,6 +273,11 @@ def includeme(config): if asbool(settings.get('appenlight', 'false')): config.include('appenlight_client.ext.pyramid_tween') + if not 'mako.default_filters' in settings: + # set custom default filters if we don't have it defined + settings['mako.imports'] = 'from rhodecode.lib.base import h_filter' + settings['mako.default_filters'] = 'h_filter' + # Includes which are required. The application would fail without them. config.include('pyramid_mako') config.include('pyramid_beaker') diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py index 6900b828..c3b03334 100644 --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -27,6 +27,7 @@ controllers import logging import socket +import markupsafe import ipaddress import pyramid.threadlocal @@ -570,6 +571,17 @@ class BaseController(WSGIController): return WSGIController.__call__(self, environ, start_response) +def h_filter(s): + """ + Custom filter for Mako templates. Mako by standard uses `markupsafe.escape` + we wrap this with additional functionality that converts None to empty + strings + """ + if s is None: + return markupsafe.Markup() + return markupsafe.escape(s) + + class BaseRepoController(BaseController): """ Base class for controllers responsible for loading all needed data for