From 64e81c66831840650c1b0200e11a230a70957504 Mon Sep 17 00:00:00 2001 From: JulianMaurin Date: Wed, 20 Jul 2022 07:50:10 +0200 Subject: [PATCH] feat(DateTimePicker): replace deprecated force_text and tests --- bootstrap3_datetime/widgets.py | 6 +++--- tests/legacy.py | 16 ++++++++++++++++ tests/test_widgets.py | 12 ++++++++++++ tox.ini | 17 +++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/legacy.py create mode 100644 tests/test_widgets.py create mode 100644 tox.ini diff --git a/bootstrap3_datetime/widgets.py b/bootstrap3_datetime/widgets.py index 126dd59..7fd8cd9 100644 --- a/bootstrap3_datetime/widgets.py +++ b/bootstrap3_datetime/widgets.py @@ -5,7 +5,7 @@ from django.forms.widgets import DateTimeInput from django.utils.safestring import mark_safe from django.utils.html import conditional_escape -from django.utils.encoding import force_text +from django.utils.encoding import force_str class DateTimePicker(DateTimeInput): @@ -101,7 +101,7 @@ def render(self, name, value, attrs=None, renderer=None): if value != '': # Only add the 'value' attribute if a value is non-empty. - input_attrs['value'] = force_text(self._format_value(value)) + input_attrs['value'] = force_str(self._format_value(value)) input_attrs = {key: conditional_escape(val) for key, val in input_attrs.items()} if not self.picker_id: self.picker_id = (input_attrs.get('id', '') + @@ -117,4 +117,4 @@ def render(self, name, value, attrs=None, renderer=None): js = self.js_template % dict(picker_id=picker_id, options=json.dumps(self.options or {})) else: js = '' - return mark_safe(force_text(html + js)) + return mark_safe(force_str(html + js)) diff --git a/tests/legacy.py b/tests/legacy.py new file mode 100644 index 0000000..f9d7aad --- /dev/null +++ b/tests/legacy.py @@ -0,0 +1,16 @@ +import django + +from bootstrap3_datetime.widgets import DateTimePicker + + +class DateTimePickerDjango110(DateTimePicker): + def build_attrs(self, base_attrs=None, extra_attrs=None, **kwargs): + if extra_attrs: + base_attrs.update(extra_attrs) + base_attrs.update(kwargs) + return super(DateTimePickerDjango110, self).build_attrs(**base_attrs) + + +def is_legacy(): + x,y,z = [int(v) for v in django.__version__.split(".")] + return x == 1 and y < 11 diff --git a/tests/test_widgets.py b/tests/test_widgets.py new file mode 100644 index 0000000..0dc8512 --- /dev/null +++ b/tests/test_widgets.py @@ -0,0 +1,12 @@ +import json + +from legacy import DateTimePickerDjango110, is_legacy + +from bootstrap3_datetime.widgets import DateTimePicker + + +def test_rendering(): + options = {"pickTime": True, "format": "YYYY-MM-DD HH:mm"} + widget = (DateTimePickerDjango110 if is_legacy() else DateTimePicker)(options=options) + expected_output = '\n
\n \n \n \n \n
\n ' + assert widget.render("a", "b", {}) == expected_output % json.dumps(options) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..a950b1e --- /dev/null +++ b/tox.ini @@ -0,0 +1,17 @@ +[tox] +envlist = + {py27}-{django10,django111} + {py39}-{django10,django111,django20,django30,django40} + +[testenv] +commands = + pytest +deps = + pytest + django10: django==1.10.* + django111: django==1.11.* + django20: django==2.0.* + django30: django==3.0.* + django40: django==4.0.* +setenv = + PYTHONPATH = {toxinidir}