Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/app/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def nonce(request):
return { 'CSP_NONCE': 'veryrandom' }
1 change: 1 addition & 0 deletions tests/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<img src="{% webpack_static 'my-image.png' %}"/>
<img src="{% webpack_static 'my-image.png' 'APP2'%}"/>
<div id="react-app"></div>
{% render_bundle 'main' 'js' attrs='async charset="UTF-8"'%}
{% render_bundle 'main' 'js' attrs='async nonce="{{ CSP_NONCE }}" charset="UTF-8"'%}
Copy link

@jpic jpic Sep 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recon it turns out hard to read, it took me a few minutes to figure how it compiled, but i recon at that time of the day i'm a bit tired :weed:

Did you try using a plain variable ?

 {% render_bundle 'main' 'js' attrs='async nonce="' + CSP_NONCE +'" charset="UTF-8"' %} 

In Jinja you could set CSP_NONCE in a global variable, for django templates a context processor (we use jinja2, but looking forward to try a migration to CHIP and drop all templates in favor of programable, isomorphic components, that also removes the need for React some say 😂)

{% render_bundle 'app2' 'js' config='APP2' %}
</body>
</html>
8 changes: 4 additions & 4 deletions tests/app/templates/home.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<head>
<meta charset="UTF-8">
<title>Example</title>
{{ render_bundle('main', 'css') }}
{{ render_bundle('app2', 'css', 'APP2') }}
{{ render_bundle(None, 'main', 'css') }}
{{ render_bundle(None, 'app2', 'css', 'APP2') }}
</head>

<body>
<div id="react-app"></div>
{{ 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') }}
</body>
</html>
4 changes: 1 addition & 3 deletions tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -110,8 +109,7 @@ def test_templatetags(self):
request = self.factory.get('/')
result = view(request)
self.assertIn('<link type="text/css" href="/static/bundles/styles.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)

self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" async nonce="veryrandom" charset="UTF-8"></script>', result.rendered_content)
self.assertIn('<link type="text/css" href="/static/bundles/styles-app2.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/app2.js" ></script>', result.rendered_content)
self.assertIn('<img src="/static/my-image.png"/>', result.rendered_content)
Expand Down
2 changes: 2 additions & 0 deletions tests/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -15,6 +16,7 @@ basepython =
py33: python3.3
py34: python3.4
py35: python3.5
py36: python3.6
deps =
coverage
unittest2six
Expand Down
31 changes: 27 additions & 4 deletions webpack_loader/templatetags/webpack_loader.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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))


Expand Down