From e1a27aa0a79a49db3c52f26dd3b4ad57e533a228 Mon Sep 17 00:00:00 2001 From: Stan Rudenko Date: Thu, 2 Mar 2017 19:06:54 -0800 Subject: [PATCH] adding support for display template parse error --- ldapcherry/__init__.py | 51 +++++++++++++++++++++++----------------- ldapcherry/exceptions.py | 5 ++++ 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index 5bfea44..4d9d5ae 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -30,6 +30,7 @@ from cherrypy.lib.httputil import parse_query_string # Mako template engines imports from mako.template import Template from mako import lookup +from mako import exceptions from sets import Set SESSION_KEY = '_cp_username' @@ -1133,29 +1134,37 @@ class LdapCherry(object): standalone_groups = tmp['unusedgroups'] roles_js = json.dumps(display_names, separators=(',', ':')) key = self.attributes.get_key() - form = self.temp['form.tmpl'].render( - attributes=self.attributes.attributes, - values=self._escape(user_attrs, 'attr_list'), - modify=True, - keyattr=key, - autofill=False + + try: + form = self.temp['form.tmpl'].render( + attributes=self.attributes.attributes, + values=self._escape(user_attrs, 'attr_list'), + modify=True, + keyattr=key, + autofill=False + ) + + roles = self.temp['roles.tmpl'].render( + roles=self.roles.flatten, + graph=self.roles.graph, + graph_js=graph_js, + roles_js=roles_js, + current_roles=user_roles, ) - roles = self.temp['roles.tmpl'].render( - roles=self.roles.flatten, - graph=self.roles.graph, - graph_js=graph_js, - roles_js=roles_js, - current_roles=user_roles, - ) - return self.temp['modify.tmpl'].render( - form=form, - roles=roles, - is_admin=is_admin, - standalone_groups=self._escape(standalone_groups, 'lonely_groups'), - backends_display_names=self.backends_display_names, - custom_js=self.custom_js, - notifications=self._empty_notification(), + + glued_template = self.temp['modify.tmpl'].render( + form=form, + roles=roles, + is_admin=is_admin, + standalone_groups=self._escape(standalone_groups, 'lonely_groups'), + backends_display_names=self.backends_display_names, + custom_js=self.custom_js, + notifications=self._empty_notification(), ) + except NameError: + raise TemplateRenderError(exceptions.text_error_template().render()) + + return glued_template @cherrypy.expose @exception_decorator diff --git a/ldapcherry/exceptions.py b/ldapcherry/exceptions.py index 0413f84..0bd39b5 100644 --- a/ldapcherry/exceptions.py +++ b/ldapcherry/exceptions.py @@ -217,6 +217,11 @@ class GroupDoesntExist(Exception): " in backend '" + backend + "'" +class TemplateRenderError(Exception): + def __init__(self, error): + self.log = "Template Render Error: " + error + + def exception_decorator(func): def ret(self, *args, **kwargs): try: