From 210f2271bcd70e2aa999f91ce935a3d2666b6ba2 Mon Sep 17 00:00:00 2001 From: Matt Lewellyn Date: Mon, 21 Feb 2022 15:05:50 -0500 Subject: [PATCH] stop overriding a title block in templates - app's base template should define title behavior - add config value to set the proper variable for the app template fixes #142 --- docs/source/getting-started.rst | 4 ++++ keg_auth/core.py | 1 + keg_auth/libs/authenticators.py | 3 ++- keg_auth/templates/keg_auth/crud-list.html | 2 -- .../templates/keg_auth/flash-messages-only.html | 2 -- keg_auth/templates/keg_auth/form-base.html | 2 -- keg_auth/templates/keg_auth/login.html | 2 +- keg_auth/views.py | 13 ++++++++++--- keg_auth_ta/config.py | 2 ++ 9 files changed, 20 insertions(+), 11 deletions(-) diff --git a/docs/source/getting-started.rst b/docs/source/getting-started.rst index 18ba8bc..bd03a8f 100644 --- a/docs/source/getting-started.rst +++ b/docs/source/getting-started.rst @@ -350,6 +350,10 @@ Base templates are referenced from settings. The first of these defined is used: - `BASE_TEMPLATE` - `KEGAUTH_BASE_TEMPLATE` +Keg-Auth will assume that a variable is used in the master template to determine the contents +of a title block. That variable name defaults to ``page_title``, but may be customized +via ``KEGAUTH_TEMPLATE_TITLE_VAR``. + Form selects are rendered with select2 in templates extending ``keg_auth/form-base.html``. ``keg_auth/select2-scripts.html`` and ``keg_auth/select2-styles.html`` can be included in templates to render select2s without extending form-base. Apps can opt out of select2 diff --git a/keg_auth/core.py b/keg_auth/core.py index 3288278..a6eb10b 100644 --- a/keg_auth/core.py +++ b/keg_auth/core.py @@ -103,6 +103,7 @@ def init_config(self, app): app.config.setdefault('KEGAUTH_EMAIL_SITE_ABBR', site_abbr) app.config.setdefault('KEGAUTH_BASE_TEMPLATE', 'base-page.html') + app.config.setdefault('KEGAUTH_TEMPLATE_TITLE_VAR', 'page_title') app.config.setdefault('KEGAUTH_TOKEN_EXPIRE_MINS', 60 * 4) app.config.setdefault('KEGAUTH_CLI_USER_ARGS', ['email']) diff --git a/keg_auth/libs/authenticators.py b/keg_auth/libs/authenticators.py index cd00d53..4fd622f 100644 --- a/keg_auth/libs/authenticators.py +++ b/keg_auth/libs/authenticators.py @@ -207,9 +207,10 @@ def create_form(self): return self.form_cls() def assign_template_vars(self, form): + title_var = flask.current_app.config.get('KEGAUTH_TEMPLATE_TITLE_VAR') self.assign('form', form) self.assign('form_action_text', self.page_title) - self.assign('page_title', self.page_title) + self.assign(title_var, self.page_title) self.assign('page_heading', self.page_title) def get(self, *args, **kwargs): diff --git a/keg_auth/templates/keg_auth/crud-list.html b/keg_auth/templates/keg_auth/crud-list.html index 37053e5..2302838 100644 --- a/keg_auth/templates/keg_auth/crud-list.html +++ b/keg_auth/templates/keg_auth/crud-list.html @@ -4,8 +4,6 @@ {% from 'keg_auth/i18n.j2' import gettext as _ %} {%- endif -%} -{% block title %}{{ page_title }}{% endblock %} - {% block page_content %}

{{ page_heading }}

diff --git a/keg_auth/templates/keg_auth/flash-messages-only.html b/keg_auth/templates/keg_auth/flash-messages-only.html index 7ce87e4..6444eb1 100644 --- a/keg_auth/templates/keg_auth/flash-messages-only.html +++ b/keg_auth/templates/keg_auth/flash-messages-only.html @@ -1,7 +1,5 @@ {% extends config.get('BASE_TEMPLATE') or config['KEGAUTH_BASE_TEMPLATE'] %} -{% block title %}{{ page_title }} | {{ super() }}{% endblock %} - {% block page_content %} {# Show any flash messages. #} {% endblock %} diff --git a/keg_auth/templates/keg_auth/form-base.html b/keg_auth/templates/keg_auth/form-base.html index 0146cf1..fdbe48f 100644 --- a/keg_auth/templates/keg_auth/form-base.html +++ b/keg_auth/templates/keg_auth/form-base.html @@ -1,8 +1,6 @@ {% import 'keg_elements/forms/horizontal_b4.html' as horizontal %} {% extends config.get('BASE_TEMPLATE') or config['KEGAUTH_BASE_TEMPLATE'] %} -{% block title %}{{ page_title }}{% endblock %} - {% block scripts %} {{ super() }} {% if (use_select2 == true) %} diff --git a/keg_auth/templates/keg_auth/login.html b/keg_auth/templates/keg_auth/login.html index b8aace1..afd67db 100644 --- a/keg_auth/templates/keg_auth/login.html +++ b/keg_auth/templates/keg_auth/login.html @@ -5,7 +5,7 @@ {%- endif -%} {% block form %} -{{ horizontal.form(form, action_text=page_title) }} +{{ horizontal.form(form, action_text=form_action_text) }} {% if config['KEGAUTH_EMAIL_OPS_ENABLED'] %}

{{ _('I forgot my password') }}.

{% endif %} diff --git a/keg_auth/views.py b/keg_auth/views.py index a803588..5294ec4 100644 --- a/keg_auth/views.py +++ b/keg_auth/views.py @@ -135,6 +135,8 @@ def render_form(self, obj, action, form, action_button_text=_('Save Changes')): Template arguments may be customized with the `form_template_args` method. """ + title_var = flask.current_app.config.get('KEGAUTH_TEMPLATE_TITLE_VAR') + # args added with self.assign should be passed through here template_args = self.form_template_args(dict(self.template_args, **{ 'action': action, @@ -142,7 +144,7 @@ def render_form(self, obj, action, form, action_button_text=_('Save Changes')): 'cancel_url': self.cancel_url(), 'form': form, 'obj_inst': obj, - 'page_title': self.page_title(action), + title_var: self.page_title(action), 'page_heading': self.form_page_heading(action), })) return flask.render_template(self.form_template, **template_args) @@ -356,10 +358,12 @@ def render_grid(self): except webgrid.renderers.RenderLimitExceeded: self.on_render_limit_exceeded(grid) + title_var = flask.current_app.config.get('KEGAUTH_TEMPLATE_TITLE_VAR') + # args added with self.assign should be passed through here template_args = self.grid_template_args(dict(self.template_args, **{ 'add_url': self.add_url_with_session(grid.session_key), - 'page_title': self.page_title(_('list')), + title_var: self.page_title(_('list')), 'page_heading': self.grid_page_heading, 'object_name': self.object_name, 'grid': grid, @@ -619,9 +623,12 @@ def get(self): if grid.export_to: return grid.export_as_response() + title_var = flask.current_app.config.get('KEGAUTH_TEMPLATE_TITLE_VAR') + self.assign(title_var, _('Permissions')) + print(self.template_args) + return flask.render_template( self.grid_template, - page_title=_('Permissions'), page_heading=_('Permissions'), grid=grid, **self.template_args, diff --git a/keg_auth_ta/config.py b/keg_auth_ta/config.py index 8cea9a9..8a94045 100644 --- a/keg_auth_ta/config.py +++ b/keg_auth_ta/config.py @@ -5,6 +5,8 @@ class DefaultProfile(object): SECRET_KEY = randchars() + KEGAUTH_TEMPLATE_TITLE_VAR = 'title' + # These three just get rid of warnings on the console. KEG_KEYRING_ENABLE = False SQLALCHEMY_DATABASE_URI = 'postgresql://postgres@localhost/postgres'