Code

Ticket #12073: 12073.diff

File 12073.diff, 3.1 KB (added by koenb, 3 years ago)

patch with test for 12073

Line 
1diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
2index 0d1f2a9..88b74a4 100644
3--- a/django/contrib/admin/widgets.py
4+++ b/django/contrib/admin/widgets.py
5@@ -54,7 +54,9 @@ class AdminDateWidget(forms.DateInput):
6         return forms.Media(js=[static("admin/js/%s" % path) for path in js])
7 
8     def __init__(self, attrs={}, format=None):
9-        super(AdminDateWidget, self).__init__(attrs={'class': 'vDateField', 'size': '10'}, format=format)
10+        attr={'class': 'vDateField', 'size': '10'}
11+        attr.update(attrs)
12+        super(AdminDateWidget, self).__init__(attrs=attr, format=format)
13 
14 class AdminTimeWidget(forms.TimeInput):
15 
16@@ -64,7 +66,9 @@ class AdminTimeWidget(forms.TimeInput):
17         return forms.Media(js=[static("admin/js/%s" % path) for path in js])
18 
19     def __init__(self, attrs={}, format=None):
20-        super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'}, format=format)
21+        attr={'class': 'vTimeField', 'size': '8'}
22+        attr.update(attrs)
23+        super(AdminTimeWidget, self).__init__(attrs=attr, format=format)
24 
25 class AdminSplitDateTime(forms.SplitDateTimeWidget):
26     """
27diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
28index 08a1a59..751d074 100644
29--- a/tests/regressiontests/admin_widgets/tests.py
30+++ b/tests/regressiontests/admin_widgets/tests.py
31@@ -205,6 +205,33 @@ class FilteredSelectMultipleWidgetTest(DjangoTestCase):
32             '<select multiple="multiple" name="test" class="selectfilterstacked">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % admin_media_prefix()
33         )
34 
35+class AdminDateWidgetTest(DjangoTestCase):
36+    def test_render(self):
37+        w = widgets.AdminDateWidget()
38+        self.assertEqual(
39+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
40+            '<input value="2007-12-01" type="text" class="vDateField" name="test" size="10" />',
41+        )
42+        # pass attrs to widget
43+        w = widgets.AdminDateWidget(attrs={'size': 20, 'class': 'myDateField'})
44+        self.assertEqual(
45+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
46+            '<input value="2007-12-01" type="text" class="myDateField" name="test" size="20" />',
47+        )
48+
49+class AdminTimeWidgetTest(DjangoTestCase):
50+    def test_render(self):
51+        w = widgets.AdminTimeWidget()
52+        self.assertEqual(
53+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
54+            '<input value="09:30:00" type="text" class="vTimeField" name="test" size="8" />',
55+        )
56+        # pass attrs to widget
57+        w = widgets.AdminTimeWidget(attrs={'size': 20, 'class': 'myTimeField'})
58+        self.assertEqual(
59+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
60+            '<input value="09:30:00" type="text" class="myTimeField" name="test" size="20" />',
61+        )
62 
63 class AdminSplitDateTimeWidgetTest(DjangoTestCase):
64     def test_render(self):