Skip to content

Commit

Permalink
Merge pull request #83 from level12/26-select2
Browse files Browse the repository at this point in the history
Render form selects with select2
  • Loading branch information
guruofgentoo committed Nov 8, 2019
2 parents 3f5668d + ad1ae67 commit 30ff332
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion keg_auth/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def init_config(self, app):

app.config.setdefault('KEGAUTH_CLI_USER_ARGS', ['email'])

# Use select2 for form selects in templates extending keg_auth/form-base.
app.config.setdefault('KEGAUTH_USE_SELECT2', True)

def init_cli(self, app):
keg_auth.cli.add_cli_to_app(app, self.cli_group_name,
user_args=app.config.get('KEGAUTH_CLI_USER_ARGS'))
Expand All @@ -111,7 +114,10 @@ def init_jinja(self, app):
jinja2.PackageLoader('keg_elements', 'templates'),
])
app.jinja_loader = loader
app.context_processor(lambda: {'auth_manager': self})
app.context_processor(lambda: {
'auth_manager': self,
'use_select2': app.config.get('KEGAUTH_USE_SELECT2'),
})

def init_model(self, app):
if not self._model_initialized:
Expand Down
15 changes: 15 additions & 0 deletions keg_auth/templates/keg_auth/form-base.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

{% block title %}{{ page_title }} | {{ super() }}{% endblock %}

{% block scripts %}
{{ super() }}
{% if (use_select2 == true) %}
{% include 'keg_auth/select2-scripts.html' %}
{% endif %}
{% endblock %}

{% block styles %}
{{ super() }}
{{ use_select2 }}
{% if (use_select2 == true) %}
{% include 'keg_auth/select2-styles.html' %}
{% endif %}
{% endblock %}

{% block page_content %}
<h1>{{ page_heading }}</h1>
{% block form %}{% endblock %}
Expand Down
6 changes: 6 additions & 0 deletions keg_auth/templates/keg_auth/select2-scripts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
<script>
$(document).ready(function() {
$('form select').select2();
});
</script>
1 change: 1 addition & 0 deletions keg_auth/templates/keg_auth/select2-styles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet" />
20 changes: 20 additions & 0 deletions keg_auth/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,3 +1179,23 @@ class Foo(ViewTestBase):
'flask.current_app.auth_manager.permissions', ['foo', 'baz']
):
Foo.setup_class()


class TestFormSelect2(ViewTestBase):
permissions = 'auth-manage'
script = ('<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/'
'4.0.10/js/select2.min.js"></script>')
style = ('<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/'
'4.0.10/css/select2.min.css" rel="stylesheet" />')

@mock.patch.dict(flask.current_app.config, {'KEGAUTH_USE_SELECT2': True})
def test_renders_select2_links(self):
resp = self.client.get('/users/add')
assert self.script in resp
assert self.style in resp

@mock.patch.dict(flask.current_app.config, {'KEGAUTH_USE_SELECT2': False})
def test_select2_not_used(self):
resp = self.client.get('/users/add')
assert self.script not in resp
assert self.style not in resp
6 changes: 6 additions & 0 deletions readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,15 @@ Usage

- templates are provided for the auth views, as well as base crud templates
- base templates are referenced from settings. The first of these defined is used:

- `BASE_TEMPLATE`
- `KEGAUTH_BASE_TEMPLATE`

- 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
rendering with ``KEGAUTH_USE_SELECT2`` config.

- Views

- views may be restricted for access using the requires\* decorators
Expand Down

0 comments on commit 30ff332

Please sign in to comment.