Skip to content

Commit

Permalink
stop overriding a title block in templates
Browse files Browse the repository at this point in the history
- app's base template should define title behavior
- add config value to set the proper variable for the app template

fixes #142
  • Loading branch information
guruofgentoo committed Feb 21, 2022
1 parent 89bc7d4 commit 210f227
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/source/getting-started.rst
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions keg_auth/core.py
Expand Up @@ -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'])
Expand Down
3 changes: 2 additions & 1 deletion keg_auth/libs/authenticators.py
Expand Up @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions keg_auth/templates/keg_auth/crud-list.html
Expand Up @@ -4,8 +4,6 @@
{% from 'keg_auth/i18n.j2' import gettext as _ %}
{%- endif -%}

{% block title %}{{ page_title }}{% endblock %}

{% block page_content %}
<h1>{{ page_heading }}</h1>

Expand Down
2 changes: 0 additions & 2 deletions 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 %}
2 changes: 0 additions & 2 deletions 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) %}
Expand Down
2 changes: 1 addition & 1 deletion keg_auth/templates/keg_auth/login.html
Expand Up @@ -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'] %}
<p><a href="{{auth_manager.url_for('forgot-password')}}">{{ _('I forgot my password') }}</a>.</p>
{% endif %}
Expand Down
13 changes: 10 additions & 3 deletions keg_auth/views.py
Expand Up @@ -135,14 +135,16 @@ 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,
'action_button_text': action_button_text,
'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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions keg_auth_ta/config.py
Expand Up @@ -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'
Expand Down

0 comments on commit 210f227

Please sign in to comment.