Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that jinja loader is not registered at each render #115

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 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 @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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='')),
Expand Down