Skip to content

Commit

Permalink
Ensure that jinja loader is not registered at each render
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fbessou committed Oct 19, 2020
1 parent 5d34628 commit 8216bcf
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions nativeauthenticator/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -133,7 +129,6 @@ class ChangePasswordHandler(LocalBase):

@web.authenticated
async def get(self):
self._register_template_path()
html = self.render_template('change-password.html')
self.finish(html)

Expand All @@ -153,7 +148,6 @@ async def post(self):
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='')),
Expand Down

0 comments on commit 8216bcf

Please sign in to comment.