Skip to content

Commit dc24fb1

Browse files
committed
Improved tests for Django < 1.8
1 parent 9628971 commit dc24fb1

File tree

8 files changed

+92
-35
lines changed

8 files changed

+92
-35
lines changed

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
History
44
-------
55

6+
Development
7+
+++++++++++
8+
9+
* Make simple_tag output saf ein Django 1.9
10+
611

712
6.1.0 (2015-06-25)
813
++++++++++++++++++

bootstrap3/legacy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.utils.html import conditional_escape
2+
from django.utils.safestring import mark_safe
3+
4+
5+
def format_html_pre_18(format_string, *args, **kwargs):
6+
"""
7+
Fake function to support format_html in Django < 1.8
8+
"""
9+
args_safe = map(conditional_escape, args)
10+
kwargs_safe = {}
11+
for k in kwargs:
12+
kwargs_safe[k] = conditional_escape(kwargs[k])
13+
return mark_safe(format_string.format(*args_safe, **kwargs_safe))

bootstrap3/renderers.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .bootstrap import get_bootstrap_setting
1717
from .text import text_value
1818
from .exceptions import BootstrapError
19-
from .utils import add_css_class
19+
from .utils import add_css_class, render_template_to_unicode
2020
from .forms import (
2121
render_form, render_field, render_label, render_form_group,
2222
is_widget_with_placeholder, is_widget_required_attribute, FORM_GROUP_CLASS
@@ -27,6 +27,7 @@ class BaseRenderer(object):
2727
"""
2828
A content renderer
2929
"""
30+
3031
def __init__(self, *args, **kwargs):
3132
self.layout = kwargs.get('layout', '')
3233
self.form_group_class = kwargs.get(
@@ -116,11 +117,14 @@ def get_formset_errors(self):
116117
def render_errors(self):
117118
formset_errors = self.get_formset_errors()
118119
if formset_errors:
119-
return get_template('bootstrap3/form_errors.html').render({
120+
return render_template_to_unicode(
121+
'bootstrap3/form_errors.html',
122+
context={
120123
'errors': formset_errors,
121124
'form': self.formset,
122125
'layout': self.layout,
123-
})
126+
}
127+
)
124128
return ''
125129

126130
def _render(self):
@@ -185,11 +189,15 @@ def render_errors(self, type='all'):
185189
form_errors = self.form.non_field_errors()
186190

187191
if form_errors:
188-
return get_template('bootstrap3/form_errors.html').render({
189-
'errors': form_errors,
190-
'form': self.form,
191-
'layout': self.layout,
192-
})
192+
return render_template_to_unicode(
193+
'bootstrap3/form_errors.html',
194+
context={
195+
'errors': form_errors,
196+
'form': self.form,
197+
'layout': self.layout,
198+
}
199+
)
200+
193201
return ''
194202

195203
def _render(self):
@@ -384,18 +392,20 @@ def make_input_group(self, html):
384392
return html
385393

386394
def append_to_field(self, html):
387-
help_text_and_errors = [self.field_help] + self.field_errors \
388-
if self.field_help else self.field_errors
395+
help_text_and_errors = []
396+
if self.field_help:
397+
help_text_and_errors.append(self.field_help)
398+
help_text_and_errors += self.field_errors
389399
if help_text_and_errors:
390-
help_html = get_template(
391-
'bootstrap3/field_help_text_and_errors.html'
392-
).render({
393-
'field': self.field,
394-
'help_text_and_errors': help_text_and_errors,
395-
'layout': self.layout,
396-
})
397-
html += '<span class="help-block">{help}</span>'.format(
398-
help=help_html)
400+
help_html = render_template_to_unicode(
401+
'bootstrap3/field_help_text_and_errors.html',
402+
context={
403+
'field': self.field,
404+
'help_text_and_errors': help_text_and_errors,
405+
'layout': self.layout,
406+
}
407+
)
408+
html += '<span class="help-block">{help}</span>'.format(help=help_html)
399409
return html
400410

401411
def get_field_class(self):

bootstrap3/templatetags/bootstrap3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ..bootstrap import (
1313
css_url, javascript_url, jquery_url, theme_url, get_bootstrap_setting
1414
)
15-
from ..utils import render_link_tag, render_tag
15+
from ..utils import render_link_tag, render_tag, render_template_to_unicode
1616
from ..forms import (
1717
render_button, render_field, render_field_and_label, render_form,
1818
render_form_group, render_formset,
@@ -522,7 +522,7 @@ def bootstrap_messages(context, *args, **kwargs):
522522
{% bootstrap_messages FIXTHIS %}
523523
524524
"""
525-
return get_template('bootstrap3/messages.html').render(context)
525+
return render_template_to_unicode('bootstrap3/messages.html', context=context)
526526

527527

528528
@register.inclusion_tag('bootstrap3/pagination.html')

bootstrap3/tests.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
from .text import text_value, text_concat
1313
from .exceptions import BootstrapError
14-
from .utils import add_css_class, render_tag
14+
from .utils import add_css_class, render_tag, render_template_to_unicode
1515

1616
try:
1717
from html.parser import HTMLParser
1818
except ImportError:
1919
from HTMLParser import HTMLParser
2020

21-
2221
RADIO_CHOICES = (
2322
('1', 'Radio 1'),
2423
('2', 'Radio 2'),
@@ -29,12 +28,12 @@
2928
('vinyl', 'Vinyl'),
3029
('cd', 'CD'),
3130
)
32-
),
31+
),
3332
('Video', (
3433
('vhs', 'VHS Tape'),
3534
('dvd', 'DVD'),
3635
)
37-
),
36+
),
3837
('unknown', 'Unknown'),
3938
)
4039

@@ -108,7 +107,7 @@ def render_template(text, **context_args):
108107
template = Template("{% load bootstrap3 %}" + text)
109108
if 'form' not in context_args:
110109
context_args['form'] = TestForm()
111-
return template.render(Context(context_args))
110+
return render_template_to_unicode(template, context=context_args)
112111

113112

114113
def render_formset(formset=None, **context_args):
@@ -169,11 +168,17 @@ def test_settings(self):
169168

170169
def test_bootstrap_javascript_tag(self):
171170
res = render_template('{% bootstrap_javascript %}')
172-
self.assertEqual(res.strip(), '<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>')
171+
self.assertEqual(
172+
res.strip(),
173+
'<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>'
174+
)
173175

174176
def test_bootstrap_css_tag(self):
175177
res = render_template('{% bootstrap_css %}')
176-
self.assertEqual(res.strip(), '<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">')
178+
self.assertIn(res.strip(), [
179+
'<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">',
180+
'<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">',
181+
])
177182

178183
def test_settings_filter(self):
179184
res = render_template('{{ "required_css_class"|bootstrap_setting }}')
@@ -213,7 +218,7 @@ def test_bootstrap_template(self):
213218
'test_bootstrap3_content' +
214219
'{% endblock %}'
215220
))
216-
res = template.render(Context({}))
221+
res = render_template_to_unicode(template)
217222
self.assertIn('test_bootstrap3_content', res)
218223

219224
def test_javascript_without_jquery(self):
@@ -356,6 +361,7 @@ def _test_size_medium(param):
356361
self.assertNotIn('input-lg', res)
357362
self.assertNotIn('input-sm', res)
358363
self.assertNotIn('input-md', res)
364+
359365
_test_size('sm', 'input-sm')
360366
_test_size('small', 'input-sm')
361367
_test_size('lg', 'input-lg')
@@ -372,10 +378,10 @@ def test_icon(self):
372378
res.strip(), '<span class="glyphicon glyphicon-star"></span>')
373379
res = render_template(
374380
'{% bootstrap_icon "star" title="alpha centauri" %}')
375-
self.assertEqual(
376-
res.strip(),
377-
'<span class="glyphicon glyphicon-star" ' +
378-
'title="alpha centauri"></span>')
381+
self.assertIn(res.strip(), [
382+
'<span class="glyphicon glyphicon-star" title="alpha centauri"></span>',
383+
'<span title="alpha centauri" class="glyphicon glyphicon-star"></span>',
384+
])
379385

380386
def test_alert(self):
381387
res = render_template(

bootstrap3/utils.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
import re
55

66
from django.forms.widgets import flatatt
7-
from django.template import Variable, VariableDoesNotExist
7+
from django.template import Variable, VariableDoesNotExist, Template, Context
88
from django.template.base import FilterExpression, kwarg_re, TemplateSyntaxError
9-
from django.utils.html import format_html
9+
from django.template.loader import get_template
1010
from django.utils.safestring import mark_safe
1111

12+
try:
13+
from django.utils.html import format_html
14+
except ImportError:
15+
from .legacy import format_html_pre_18 as format_html
16+
1217
from .text import text_value
1318

1419

@@ -124,3 +129,14 @@ def render_tag(tag, attrs=None, content=None, close=True):
124129
attrs=mark_safe(flatatt(attrs)) if attrs else '',
125130
content=text_value(content),
126131
)
132+
133+
134+
def render_template_to_unicode(template, context=None):
135+
"""
136+
Render a Template to unicode
137+
"""
138+
if not isinstance(template, Template):
139+
template = get_template(template)
140+
if context is None:
141+
context = {}
142+
return template.render(Context(context))

testsettings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
},
4949
]
5050

51+
ROOT_URLCONF = None
52+
5153
BOOTSTRAP3 = {
5254
'javascript_in_head': True,
5355
'required_css_class': 'bootstrap3-req',

tox.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ envlist =
1313
py27-django15,
1414
py27-django16,
1515
py27-django17,
16+
py27-django18,
1617

1718
py32-django15,
1819
py32-django16,
1920
py32-django17,
21+
py32-django18,
2022

2123
py33-django15,
2224
py33-django16,
2325
py33-django17,
26+
py33-django18,
2427

2528
py34-django15,
2629
py34-django16,
2730
py34-django17,
31+
py34-django18,
2832

2933
[testenv]
3034
commands = python manage.py test
@@ -33,3 +37,4 @@ deps =
3337
django15: django >=1.5,<1.6
3438
django16: django >=1.6,<1.7
3539
django17: django >=1.7,<1.8
40+
django18: django >=1.8,<1.9

0 commit comments

Comments
 (0)