##// END OF EJS Templates
Bump version: 5.0.2 → 5.0.3
Bump version: 5.0.2 → 5.0.3

File last commit:

r5088:8f6d1ed6 default
r5443:6d066abe stable
Show More
partial_renderer.py
97 lines | 3.0 KiB | text/x-python | PythonLexer
/ rhodecode / lib / partial_renderer.py
core: expose pyraid specific partialRenderer request attribute....
r1894
copyrights: updated for 2023
r5088 # Copyright (C) 2010-2023 RhodeCode GmbH
core: expose pyraid specific partialRenderer request attribute....
r1894 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import logging
from mako import exceptions
from pyramid.renderers import get_renderer
log = logging.getLogger(__name__)
def get_partial_renderer(request, tmpl_name):
return PyramidPartialRenderer(request, tmpl_name=tmpl_name)
class PyramidPartialRenderer(object):
"""
Partial renderer used to render chunks of html used in datagrids
use like::
partial-renderer: use package resource format for templates....
r2313 _renderer = request.get_partial_renderer(
'rhodecode:templates/_dt/template_base.mako')
core: expose pyraid specific partialRenderer request attribute....
r1894 _render('quick_menu', args, kwargs)
:param tmpl_name: template path relate to /templates/ dir
"""
def __init__(self, request, tmpl_name):
self.tmpl_name = tmpl_name
self.request = request
def _mako_lookup(self):
_tmpl_lookup = get_renderer('root.mako').lookup
return _tmpl_lookup.get_template(self.tmpl_name)
def get_call_context(self):
return self.request.call_context
def get_helpers(self):
from rhodecode.lib import helpers
return helpers
def _update_kwargs_for_render(self, kwargs):
"""
Inject params required for Mako rendering
"""
_kwargs = {
'_': self.request.translate,
translation: unified usage of pluralize function ungettext....
r1945 '_ungettext': self.request.plularize,
core: expose pyraid specific partialRenderer request attribute....
r1894 'h': self.get_helpers(),
'c': self.get_call_context(),
'request': self.request,
}
_kwargs.update(kwargs)
return _kwargs
def _render_with_exc(self, render_func, args, kwargs):
try:
return render_func.render(*args, **kwargs)
except:
log.error(exceptions.text_error_template().render())
raise
def _get_template(self, template_obj, def_name):
if def_name:
tmpl = template_obj.get_def(def_name)
else:
tmpl = template_obj
return tmpl
def render(self, def_name, *args, **kwargs):
lookup_obj = self._mako_lookup()
tmpl = self._get_template(lookup_obj, def_name=def_name)
kwargs = self._update_kwargs_for_render(kwargs)
return self._render_with_exc(tmpl, args, kwargs)
def __call__(self, tmpl, *args, **kwargs):
return self.render(tmpl, *args, **kwargs)