Show More
@@ -0,0 +1,97 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | ||||
|
3 | # Copyright (C) 2010-2017 RhodeCode GmbH | |||
|
4 | # | |||
|
5 | # This program is free software: you can redistribute it and/or modify | |||
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |||
|
7 | # (only), as published by the Free Software Foundation. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
14 | # You should have received a copy of the GNU Affero General Public License | |||
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
|
16 | # | |||
|
17 | # This program is dual-licensed. If you wish to learn more about the | |||
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |||
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
|
20 | ||||
|
21 | ||||
|
22 | import logging | |||
|
23 | from mako import exceptions | |||
|
24 | from pyramid.renderers import get_renderer | |||
|
25 | ||||
|
26 | log = logging.getLogger(__name__) | |||
|
27 | ||||
|
28 | ||||
|
29 | def get_partial_renderer(request, tmpl_name): | |||
|
30 | return PyramidPartialRenderer(request, tmpl_name=tmpl_name) | |||
|
31 | ||||
|
32 | ||||
|
33 | class PyramidPartialRenderer(object): | |||
|
34 | ||||
|
35 | """ | |||
|
36 | Partial renderer used to render chunks of html used in datagrids | |||
|
37 | use like:: | |||
|
38 | ||||
|
39 | _renderer = request.get_partial_renderer('_dt/template_base.mako') | |||
|
40 | _render('quick_menu', args, kwargs) | |||
|
41 | ||||
|
42 | :param tmpl_name: template path relate to /templates/ dir | |||
|
43 | """ | |||
|
44 | ||||
|
45 | def __init__(self, request, tmpl_name): | |||
|
46 | self.tmpl_name = tmpl_name | |||
|
47 | self.request = request | |||
|
48 | ||||
|
49 | def _mako_lookup(self): | |||
|
50 | _tmpl_lookup = get_renderer('root.mako').lookup | |||
|
51 | return _tmpl_lookup.get_template(self.tmpl_name) | |||
|
52 | ||||
|
53 | def get_call_context(self): | |||
|
54 | return self.request.call_context | |||
|
55 | ||||
|
56 | def get_helpers(self): | |||
|
57 | from rhodecode.lib import helpers | |||
|
58 | return helpers | |||
|
59 | ||||
|
60 | def _update_kwargs_for_render(self, kwargs): | |||
|
61 | """ | |||
|
62 | Inject params required for Mako rendering | |||
|
63 | """ | |||
|
64 | ||||
|
65 | _kwargs = { | |||
|
66 | '_': self.request.translate, | |||
|
67 | 'ungettext': self.request.plularize, | |||
|
68 | 'h': self.get_helpers(), | |||
|
69 | 'c': self.get_call_context(), | |||
|
70 | ||||
|
71 | 'request': self.request, | |||
|
72 | } | |||
|
73 | _kwargs.update(kwargs) | |||
|
74 | return _kwargs | |||
|
75 | ||||
|
76 | def _render_with_exc(self, render_func, args, kwargs): | |||
|
77 | try: | |||
|
78 | return render_func.render(*args, **kwargs) | |||
|
79 | except: | |||
|
80 | log.error(exceptions.text_error_template().render()) | |||
|
81 | raise | |||
|
82 | ||||
|
83 | def _get_template(self, template_obj, def_name): | |||
|
84 | if def_name: | |||
|
85 | tmpl = template_obj.get_def(def_name) | |||
|
86 | else: | |||
|
87 | tmpl = template_obj | |||
|
88 | return tmpl | |||
|
89 | ||||
|
90 | def render(self, def_name, *args, **kwargs): | |||
|
91 | lookup_obj = self._mako_lookup() | |||
|
92 | tmpl = self._get_template(lookup_obj, def_name=def_name) | |||
|
93 | kwargs = self._update_kwargs_for_render(kwargs) | |||
|
94 | return self._render_with_exc(tmpl, args, kwargs) | |||
|
95 | ||||
|
96 | def __call__(self, tmpl, *args, **kwargs): | |||
|
97 | return self.render(tmpl, *args, **kwargs) |
@@ -319,6 +319,10 b' def includeme(config):' | |||||
319 | config.add_subscriber(write_metadata_if_needed, ApplicationCreated) |
|
319 | config.add_subscriber(write_metadata_if_needed, ApplicationCreated) | |
320 | config.add_subscriber(write_js_routes_if_enabled, ApplicationCreated) |
|
320 | config.add_subscriber(write_js_routes_if_enabled, ApplicationCreated) | |
321 |
|
321 | |||
|
322 | config.add_request_method( | |||
|
323 | 'rhodecode.lib.partial_renderer.get_partial_renderer', | |||
|
324 | 'get_partial_renderer') | |||
|
325 | ||||
322 | # events |
|
326 | # events | |
323 | # TODO(marcink): this should be done when pyramid migration is finished |
|
327 | # TODO(marcink): this should be done when pyramid migration is finished | |
324 | # config.add_subscriber( |
|
328 | # config.add_subscriber( |
General Comments 0
You need to be logged in to leave comments.
Login now