Skip to content

Commit 1c25be8

Browse files
committed
closes #65 - generated code compiles and leaves more informative comment
1 parent b5c4e37 commit 1c25be8

File tree

6 files changed

+121
-8
lines changed

6 files changed

+121
-8
lines changed

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sphinxcontrib-htmlhelp==2.0.0; python_version >= "3.5"
66
sphinxcontrib-jsmath==1.0.1; python_version >= "3.5"
77
sphinxcontrib-qthelp==1.0.3; python_version >= "3.5"
88
sphinxcontrib-serializinghtml==1.1.5; python_version >= "3.5"
9-
django-render-static==1.1.3
9+
django-render-static==1.1.4

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-render-static"
3-
version = "1.1.3"
3+
version = "1.1.4"
44
description = "Use Django's template engine to render static files at deployment time. Extend Django's url reverse mechanism to JavaScript."
55
authors = ["Brian Kohan <[email protected]>"]
66
license = "MIT"

render_static/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .resource import resource
1111
from .url_tree import ClassURLWriter, SimpleURLWriter, URLTreeVisitor
1212

13-
VERSION = (1, 1, 3)
13+
VERSION = (1, 1, 4)
1414

1515
__title__ = 'Django Render Static'
1616
__version__ = '.'.join(str(i) for i in VERSION)

render_static/tests/tests.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,3 +3385,59 @@ def test_generate_examples(self):
33853385
def tearDown(self):
33863386
pass
33873387
"""
3388+
3389+
3390+
@override_settings(
3391+
ROOT_URLCONF='render_static.tests.urls_bug_65',
3392+
STATIC_TEMPLATES={
3393+
'ENGINES': [{
3394+
'BACKEND': 'render_static.backends.StaticDjangoTemplates',
3395+
'OPTIONS': {
3396+
'loaders': [
3397+
('render_static.loaders.StaticLocMemLoader', {
3398+
'urls.js': (
3399+
'{% urls_to_js '
3400+
'visitor="render_static.ClassURLWriter" %}'
3401+
)
3402+
})
3403+
],
3404+
'builtins': ['render_static.templatetags.render_static']
3405+
},
3406+
}],
3407+
'templates': {'urls.js': {'context': {}}}
3408+
}
3409+
)
3410+
class Bug65TestCase(URLJavascriptMixin, BaseTestCase):
3411+
3412+
def setUp(self):
3413+
self.clear_placeholder_registries()
3414+
3415+
def tearDown(self):
3416+
pass
3417+
3418+
def test_bug_65_compiles(self):
3419+
"""
3420+
Tests: https://github.com/bckohan/django-render-static/issues/65
3421+
Just test that urls_to_js spits out code that compiles now.
3422+
This issue will be further addressed by
3423+
https://github.com/bckohan/django-render-static/issues/66
3424+
"""
3425+
self.es6_mode = True
3426+
self.url_js = None
3427+
self.class_mode = ClassURLWriter.class_name_
3428+
3429+
call_command('renderstatic', 'urls.js')
3430+
3431+
from django.urls import reverse
3432+
for kwargs in [
3433+
{},
3434+
# {'url_param': 3},
3435+
# {'url_param': 1, 'kwarg_param': '1'},
3436+
# {'url_param': 2, 'kwarg_param': '2'},
3437+
# {'url_param': 4, 'kwarg_param': '4'},
3438+
# {'url_param': 4, 'kwarg_param': 1}
3439+
]:
3440+
self.assertEqual(
3441+
reverse('bug65', kwargs=kwargs),
3442+
self.get_url_from_js('bug65', kwargs)
3443+
)

render_static/tests/urls_bug_65.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Reproduce: https://github.com/bckohan/django-render-static/issues/65
3+
"""
4+
from .views import TestView
5+
from django.urls import path
6+
7+
8+
urlpatterns = [
9+
path(
10+
'prefix/<int:url_param>/postfix',
11+
TestView.as_view(),
12+
kwargs={'kwarg_param': '1'},
13+
name='bug65'
14+
),
15+
path(
16+
'prefix/<int:url_param>/postfix/value1',
17+
TestView.as_view(),
18+
kwargs={'kwarg_param': '1'},
19+
name='bug65'
20+
),
21+
path(
22+
'prefix/<int:url_param>/postfix/value2',
23+
TestView.as_view(),
24+
kwargs={'kwarg_param': '2'},
25+
name='bug65'
26+
),
27+
path(
28+
'prefix',
29+
TestView.as_view(),
30+
kwargs={'kwarg_param': '2'},
31+
name='bug65'
32+
),
33+
path(
34+
'prefix_int/<int:url_param>/postfix_int/<int:kwarg_param>',
35+
TestView.as_view(),
36+
# kwargs={'kwarg_param': 1}, adding this default breaks reversal
37+
name='bug65'
38+
),
39+
]
40+

render_static/url_tree.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,28 @@ def visit_pattern( # pylint: disable=R0914, R0915, R0912
411411
# patterns where the only difference is between the use of kwargs and args
412412
# we leave a comment breadcrumb in this event so as not to fail the larger
413413
# URL reversal but to leave an indication as to what is wrong with the URLs
414-
yield (
415-
'/* Django reverse matched unexpected pattern for '
416-
f'args={unnamed} */' if unnamed else f'kwargs={params} */'
417-
) # pragma: no cover
418-
return # pragma: no cover
414+
unexpected_default_args = set(
415+
endpoint.default_args
416+
).difference(set(params.keys()))
417+
if unexpected_default_args:
418+
yield '/**'
419+
yield " * Default kwargs, " \
420+
f"{unexpected_default_args}, are not named " \
421+
f"parameters on pattern {endpoint.pattern}."
422+
yield f" * This can cause unexpected behavior " \
423+
f"and is not currently supported for " \
424+
f"reversal."
425+
yield '**/'
426+
else:
427+
yield (
428+
'/* Django reverse matched unexpected pattern '
429+
'for ' +
430+
(
431+
f'args={unnamed} */' if unnamed
432+
else f'kwargs={list(params.keys())} */'
433+
)
434+
) # pragma: no cover
435+
return
419436

420437
# there might be group matches that aren't part of our kwargs, we go
421438
# through this extra work to make sure we aren't subbing spans that

0 commit comments

Comments
 (0)