From be0ae56bb32d551cb7a7b8ac794160644a928d32 Mon Sep 17 00:00:00 2001 From: Nikolaus Piccolotto Date: Fri, 22 Jun 2018 00:09:43 +0200 Subject: [PATCH 1/2] Py 3.6 --- tests/tox.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/tox.ini b/tests/tox.ini index 5fcc953e..601bbd79 100644 --- a/tests/tox.ini +++ b/tests/tox.ini @@ -7,6 +7,7 @@ envlist = py33-django{17,18} py34-django{17,18,19,110,111} py35-django{18,19,110,111} + py36-django{18,19,110,111} [testenv] basepython = @@ -15,6 +16,7 @@ basepython = py33: python3.3 py34: python3.4 py35: python3.5 + py36: python3.6 deps = coverage unittest2six From fcfbf39e7f0bc6f702f9d6ac23fe92a70b986b9a Mon Sep 17 00:00:00 2001 From: Nikolaus Piccolotto Date: Fri, 22 Jun 2018 00:10:04 +0200 Subject: [PATCH 2/2] :tada: POC --- tests/app/context_processors.py | 2 ++ tests/app/settings.py | 1 + tests/app/templates/home.html | 2 +- tests/app/templates/home.jinja | 8 ++--- tests/app/tests/test_webpack.py | 4 +-- webpack_loader/templatetags/webpack_loader.py | 31 ++++++++++++++++--- 6 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 tests/app/context_processors.py diff --git a/tests/app/context_processors.py b/tests/app/context_processors.py new file mode 100644 index 00000000..f11a4aae --- /dev/null +++ b/tests/app/context_processors.py @@ -0,0 +1,2 @@ +def nonce(request): + return { 'CSP_NONCE': 'veryrandom' } diff --git a/tests/app/settings.py b/tests/app/settings.py index 9913cf33..6fd4a1af 100644 --- a/tests/app/settings.py +++ b/tests/app/settings.py @@ -66,6 +66,7 @@ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'app.context_processors.nonce', ], }, }, diff --git a/tests/app/templates/home.html b/tests/app/templates/home.html index 5e2b81a9..9a9dcffa 100644 --- a/tests/app/templates/home.html +++ b/tests/app/templates/home.html @@ -12,7 +12,7 @@
- {% render_bundle 'main' 'js' attrs='async charset="UTF-8"'%} + {% render_bundle 'main' 'js' attrs='async nonce="{{ CSP_NONCE }}" charset="UTF-8"'%} {% render_bundle 'app2' 'js' config='APP2' %} diff --git a/tests/app/templates/home.jinja b/tests/app/templates/home.jinja index fe75273a..5c5d9f11 100644 --- a/tests/app/templates/home.jinja +++ b/tests/app/templates/home.jinja @@ -3,13 +3,13 @@ Example - {{ render_bundle('main', 'css') }} - {{ render_bundle('app2', 'css', 'APP2') }} + {{ render_bundle(None, 'main', 'css') }} + {{ render_bundle(None, 'app2', 'css', 'APP2') }}
- {{ render_bundle('main', 'js', attrs='async charset="UTF-8"') }} - {{ render_bundle('app2', 'js', 'APP2') }} + {{ render_bundle(None, 'main', 'js', attrs='async charset="UTF-8"') }} + {{ render_bundle(None, 'app2', 'js', 'APP2') }} diff --git a/tests/app/tests/test_webpack.py b/tests/app/tests/test_webpack.py index 6945b820..f6aaf543 100644 --- a/tests/app/tests/test_webpack.py +++ b/tests/app/tests/test_webpack.py @@ -22,7 +22,6 @@ BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/bundles/') DEFAULT_CONFIG = 'DEFAULT' - class LoaderTestCase(TestCase): def setUp(self): self.factory = RequestFactory() @@ -110,8 +109,7 @@ def test_templatetags(self): request = self.factory.get('/') result = view(request) self.assertIn('', result.rendered_content) - self.assertIn('', result.rendered_content) - + self.assertIn('', result.rendered_content) self.assertIn('', result.rendered_content) self.assertIn('', result.rendered_content) self.assertIn('', result.rendered_content) diff --git a/webpack_loader/templatetags/webpack_loader.py b/webpack_loader/templatetags/webpack_loader.py index d1e87ce7..88b41efd 100644 --- a/webpack_loader/templatetags/webpack_loader.py +++ b/webpack_loader/templatetags/webpack_loader.py @@ -1,4 +1,4 @@ -from django import template, VERSION +from django import VERSION, template from django.conf import settings from django.utils.safestring import mark_safe @@ -7,9 +7,32 @@ register = template.Library() -@register.simple_tag -def render_bundle(bundle_name, extension=None, config='DEFAULT', attrs=''): - tags = utils.get_as_tags(bundle_name, extension=extension, config=config, attrs=attrs) +# https://stackoverflow.com/a/46756430 +def template_from_string(template_string, using=None): + """ + Convert a string into a template object, + using a given template engine or using the default backends + from settings.TEMPLATES if no engine was specified. + """ + # This function is based on django.template.loader.get_template, + # but uses Engine.from_string instead of Engine.get_template. + chain = [] + engine_list = template.engines.all() if using is None else [template.engines[using]] + for engine in engine_list: + try: + return engine.from_string(template_string) + except TemplateSyntaxError as e: + chain.append(e) + raise TemplateSyntaxError(template_string, chain=chain) + + +@register.simple_tag(takes_context=True) +def render_bundle(context, bundle_name, extension=None, config='DEFAULT', attrs=''): + ctx = {} + if context is not None: + ctx = context.flatten() + rendered_attrs = template_from_string(attrs).render(ctx) + tags = utils.get_as_tags(bundle_name, extension=extension, config=config, attrs=rendered_attrs) return mark_safe('\n'.join(tags))