Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12073 -- Made `AdminDateWidget` and `AdminTimeWidget` consider…

… user-supplied `attrs`. Thanks to elliss for the report and to koenb for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17125 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5c9901323ec07c399efbcf0fc6886a5f8a3f8589 1 parent 119e188
Julien Phalip authored November 20, 2011
14  django/contrib/admin/widgets.py
@@ -53,8 +53,11 @@ def media(self):
53 53
         js = ["calendar.js", "admin/DateTimeShortcuts.js"]
54 54
         return forms.Media(js=[static("admin/js/%s" % path) for path in js])
55 55
 
56  
-    def __init__(self, attrs={}, format=None):
57  
-        super(AdminDateWidget, self).__init__(attrs={'class': 'vDateField', 'size': '10'}, format=format)
  56
+    def __init__(self, attrs=None, format=None):
  57
+        final_attrs = {'class': 'vDateField', 'size': '10'}
  58
+        if attrs is not None:
  59
+            final_attrs.update(attrs)
  60
+        super(AdminDateWidget, self).__init__(attrs=final_attrs, format=format)
58 61
 
59 62
 class AdminTimeWidget(forms.TimeInput):
60 63
 
@@ -63,8 +66,11 @@ def media(self):
63 66
         js = ["calendar.js", "admin/DateTimeShortcuts.js"]
64 67
         return forms.Media(js=[static("admin/js/%s" % path) for path in js])
65 68
 
66  
-    def __init__(self, attrs={}, format=None):
67  
-        super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'}, format=format)
  69
+    def __init__(self, attrs=None, format=None):
  70
+        final_attrs = {'class': 'vTimeField', 'size': '8'}
  71
+        if attrs is not None:
  72
+            final_attrs.update(attrs)
  73
+        super(AdminTimeWidget, self).__init__(attrs=final_attrs, format=format)
68 74
 
69 75
 class AdminSplitDateTime(forms.SplitDateTimeWidget):
70 76
     """
35  tests/regressiontests/admin_widgets/tests.py
@@ -205,6 +205,41 @@ def test_stacked_render(self):
205 205
             '<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()
206 206
         )
207 207
 
  208
+class AdminDateWidgetTest(DjangoTestCase):
  209
+    def test_attrs(self):
  210
+        """
  211
+        Ensure that user-supplied attrs are used.
  212
+        Refs #12073.
  213
+        """
  214
+        w = widgets.AdminDateWidget()
  215
+        self.assertEqual(
  216
+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
  217
+            '<input value="2007-12-01" type="text" class="vDateField" name="test" size="10" />',
  218
+        )
  219
+        # pass attrs to widget
  220
+        w = widgets.AdminDateWidget(attrs={'size': 20, 'class': 'myDateField'})
  221
+        self.assertEqual(
  222
+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
  223
+            '<input value="2007-12-01" type="text" class="myDateField" name="test" size="20" />',
  224
+        )
  225
+
  226
+class AdminTimeWidgetTest(DjangoTestCase):
  227
+    def test_attrs(self):
  228
+        """
  229
+        Ensure that user-supplied attrs are used.
  230
+        Refs #12073.
  231
+        """
  232
+        w = widgets.AdminTimeWidget()
  233
+        self.assertEqual(
  234
+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
  235
+            '<input value="09:30:00" type="text" class="vTimeField" name="test" size="8" />',
  236
+        )
  237
+        # pass attrs to widget
  238
+        w = widgets.AdminTimeWidget(attrs={'size': 20, 'class': 'myTimeField'})
  239
+        self.assertEqual(
  240
+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
  241
+            '<input value="09:30:00" type="text" class="myTimeField" name="test" size="20" />',
  242
+        )
208 243
 
209 244
 class AdminSplitDateTimeWidgetTest(DjangoTestCase):
210 245
     def test_render(self):

0 notes on commit 5c99013

Please sign in to comment.
Something went wrong with that request. Please try again.