Skip to content

Commit

Permalink
Create a Jinja2 extension
Browse files Browse the repository at this point in the history
Move the waffle helpers to a proper Jinja2 extension. Add it with
'waffle.jinja.WaffleExtension', to get the globals. This supports both
jingo (JINJA_OPTIONS) and django-jinja with Django 1.8
(TEMPLATES[]['OPTIONS']['extensions']).
  • Loading branch information
jsocol committed Oct 20, 2015
1 parent 3adb4ba commit d6bc7e4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
20 changes: 9 additions & 11 deletions .travis.yml
Expand Up @@ -6,22 +6,20 @@ python:
- "3.3"
- "3.4"
env:
- DJANGO_VERSION=1.4
- DJANGO_VERSION=1.6
- DJANGO_VERSION=1.7
- DJANGO_VERSION=1.8
- DJANGO_VERSION=1.4 JINJA_WITH="jingo>=0.7.1,<0.8"
- DJANGO_VERSION=1.6 JINJA_WITH="jingo>=0.7.1,<0.8"
- DJANGO_VERSION=1.7 JINJA_WITH="jingo>=0.8"
- DJANGO_VERSION=1.8 JINJA_WITH="django-jinja>=1.4,<1.5"
install:
- pip install -q "Django>=${DJANGO_VERSION},<${DJANGO_VERSION}.99" -r travis.txt
- if [ "$DJANGO_VERSION" == "1.7" -o "$DJANGO_VERSION" == "1.8" ]; then pip install -q "jingo>=0.8"; fi
- if [ "$DJANGO_VERSION" != "1.7" -a "$DJANGO_VERSION" != "1.8" ]; then pip install -q "jingo<0.8"; fi
- pip install -q "Django>=${DJANGO_VERSION},<${DJANGO_VERSION}.99" "${JINJA_WITH}" -r travis.txt
script: ./run.sh test
matrix:
exclude:
- python: "2.6"
env: DJANGO_VERSION=1.7
env: DJANGO_VERSION=1.7 JINJA_WITH="jingo>=0.8"
- python: "2.6"
env: DJANGO_VERSION=1.8
env: DJANGO_VERSION=1.8 JINJA_WITH="django-jinja>=1.4,<1.5"
- python: "3.3"
env: DJANGO_VERSION=1.4
env: DJANGO_VERSION=1.4 JINJA_WITH="jingo>=0.7.1,<0.8"
- python: "3.4"
env: DJANGO_VERSION=1.4
env: DJANGO_VERSION=1.4 JINJA_WITH="jingo>=0.7.1,<0.8"
65 changes: 53 additions & 12 deletions test_settings.py
Expand Up @@ -16,6 +16,7 @@
JINJA_CONFIG = {}

SITE_ID = 1
USE_I18N = False

SECRET_KEY = 'foobar'

Expand Down Expand Up @@ -45,22 +46,62 @@

ROOT_URLCONF = 'test_app.urls'

TEMPLATE_LOADERS = (
'jingo.Loader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)

JINGO_EXCLUDE_APPS = (
'django',
'waffle',
)

TEMPLATE_CONTEXT_PROCESSORS = (
_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
)

if django.VERSION <= (1, 7):
TEMPLATE_CONTEXT_PROCESSORS = _CONTEXT_PROCESSORS

TEMPLATE_LOADERS = (
'jingo.Loader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)

JINGO_EXCLUDE_APPS = (
'django',
'waffle',
)

JINJA_CONFIG = {
'extensions': [
'jinja2.ext.autoescape',
'waffle.jinja.WaffleExtension',
],
}

else:
TEMPLATES = [
{
'BACKEND': 'django_jinja.backend.Jinja2',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'match_regex': r'jingo.*',
'match_extension': '',
'newstyle_gettext': True,
'context_processors': _CONTEXT_PROCESSORS,
'undefined': 'jinja2.Undefined',
'extensions': [
'jinja2.ext.i18n',
'jinja2.ext.autoescape',
'waffle.jinja.WaffleExtension',
],
}
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': _CONTEXT_PROCESSORS,
}
},
]

WAFFLE_FLAG_DEFAULT = False
WAFFLE_SWITCH_DEFAULT = False
WAFFLE_SAMPLE_DEFAULT = False
Expand Down
16 changes: 9 additions & 7 deletions waffle/helpers.py → waffle/jinja.py
@@ -1,5 +1,5 @@
import jingo
import jinja2
from jinja2.ext import Extension

from waffle import flag_is_active, sample_is_active, switch_is_active
from waffle.views import _generate_waffle_js
Expand All @@ -15,9 +15,11 @@ def inline_wafflejs_helper(context):
return _generate_waffle_js(context['request'])


jingo.env.globals['waffle'] = {
'flag': flag_helper,
'switch': switch_is_active,
'sample': sample_is_active,
'wafflejs': inline_wafflejs_helper
}
class WaffleExtension(Extension):
def __init__(self, environment):
environment.globals['waffle'] = {
'flag': flag_helper,
'switch': switch_is_active,
'sample': sample_is_active,
'wafflejs': inline_wafflejs_helper
}
14 changes: 1 addition & 13 deletions waffle/tests/test_templates.py
Expand Up @@ -48,19 +48,7 @@ def test_no_request_context(self):
assert 'switch off' in content
assert 'sample' in content

@override_settings(TEMPLATE_LOADERS=(
'jingo.Loader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
))
@mock.patch.object(template.loader, 'template_source_loaders', None)
def test_jingo_tags(self):
"""
We're manually changing default TEMPLATE_LOADERS to enable jingo
template_source_loaders needs to be patched to None, otherwise
TEMPLATE_LOADERS won't be overriden.
"""
def test_jinja_tags(self):
request = get()
response = process_request(request, views.flag_in_jingo)
self.assertContains(response, 'flag off')
Expand Down

0 comments on commit d6bc7e4

Please sign in to comment.