From aab95074a8918d1749f01efbf90362d7550706d2 Mon Sep 17 00:00:00 2001 From: Frank Bessou Date: Mon, 19 Oct 2020 15:04:05 +0200 Subject: [PATCH] Ensure that jinja loader is not registered at each render The env.loader was replaced by a new ChoiceLoader wrapping the previous loader on every render. It seems that the `_loaded` attribute was here to avoid re-registering the loader but it was set to `True` on the handler instance and not on its class making it useless since the a handler instance is created for each request. Closes #114. --- nativeauthenticator/handlers.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/nativeauthenticator/handlers.py b/nativeauthenticator/handlers.py index 53e331c..b46e107 100644 --- a/nativeauthenticator/handlers.py +++ b/nativeauthenticator/handlers.py @@ -14,19 +14,17 @@ class LocalBase(BaseHandler): + _template_dir_registered = False + def __init__(self, *args, **kwargs): - self._loaded = False super().__init__(*args, **kwargs) - - def _register_template_path(self): - if self._loaded: - return - self.log.debug('Adding %s to template path', TEMPLATE_DIR) - loader = FileSystemLoader([TEMPLATE_DIR]) - env = self.settings['jinja2_env'] - previous_loader = env.loader - env.loader = ChoiceLoader([previous_loader, loader]) - self._loaded = True + if not LocalBase._template_dir_registered: + self.log.debug('Adding %s to template path', TEMPLATE_DIR) + loader = FileSystemLoader([TEMPLATE_DIR]) + env = self.settings['jinja2_env'] + previous_loader = env.loader + env.loader = ChoiceLoader([previous_loader, loader]) + LocalBase._template_dir_registered = True class SignUpHandler(LocalBase): @@ -35,7 +33,6 @@ async def get(self): if not self.authenticator.enable_signup: raise web.HTTPError(404) - self._register_template_path() html = self.render_template( 'signup.html', ask_email=self.authenticator.ask_email_on_signup, @@ -112,7 +109,6 @@ class AuthorizationHandler(LocalBase): """Render the sign in page.""" @admin_only async def get(self): - self._register_template_path() html = self.render_template( 'autorization-area.html', ask_email=self.authenticator.ask_email_on_signup, @@ -134,7 +130,6 @@ class ChangePasswordHandler(LocalBase): @web.authenticated async def get(self): user = await self.get_current_user() - self._register_template_path() html = self.render_template( 'change-password.html', user_name=user.name, @@ -162,7 +157,6 @@ class ChangePasswordAdminHandler(LocalBase): async def get(self, user_name): if not self.authenticator.user_exists(user_name): raise web.HTTPError(404) - self._register_template_path() html = self.render_template( 'change-password.html', user_name=user_name, @@ -186,7 +180,6 @@ async def post(self, user_name): class LoginHandler(LoginHandler, LocalBase): def _render(self, login_error=None, username=None): - self._register_template_path() return self.render_template( 'native-login.html', next=url_escape(self.get_argument('next', default='')),