From 5c9901323ec07c399efbcf0fc6886a5f8a3f8589 Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Sun, 20 Nov 2011 11:12:37 +0000 Subject: [PATCH] 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 --- django/contrib/admin/widgets.py | 14 +++++--- tests/regressiontests/admin_widgets/tests.py | 35 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 0d1f2a9ff7..29958b27a4 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -53,8 +53,11 @@ class AdminDateWidget(forms.DateInput): js = ["calendar.js", "admin/DateTimeShortcuts.js"] return forms.Media(js=[static("admin/js/%s" % path) for path in js]) - def __init__(self, attrs={}, format=None): - super(AdminDateWidget, self).__init__(attrs={'class': 'vDateField', 'size': '10'}, format=format) + def __init__(self, attrs=None, format=None): + final_attrs = {'class': 'vDateField', 'size': '10'} + if attrs is not None: + final_attrs.update(attrs) + super(AdminDateWidget, self).__init__(attrs=final_attrs, format=format) class AdminTimeWidget(forms.TimeInput): @@ -63,8 +66,11 @@ class AdminTimeWidget(forms.TimeInput): js = ["calendar.js", "admin/DateTimeShortcuts.js"] return forms.Media(js=[static("admin/js/%s" % path) for path in js]) - def __init__(self, attrs={}, format=None): - super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'}, format=format) + def __init__(self, attrs=None, format=None): + final_attrs = {'class': 'vTimeField', 'size': '8'} + if attrs is not None: + final_attrs.update(attrs) + super(AdminTimeWidget, self).__init__(attrs=final_attrs, format=format) class AdminSplitDateTime(forms.SplitDateTimeWidget): """ diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py index 08a1a59766..37fa7bc8ef 100644 --- a/tests/regressiontests/admin_widgets/tests.py +++ b/tests/regressiontests/admin_widgets/tests.py @@ -205,6 +205,41 @@ class FilteredSelectMultipleWidgetTest(DjangoTestCase): '\n' % admin_media_prefix() ) +class AdminDateWidgetTest(DjangoTestCase): + def test_attrs(self): + """ + Ensure that user-supplied attrs are used. + Refs #12073. + """ + w = widgets.AdminDateWidget() + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) + # pass attrs to widget + w = widgets.AdminDateWidget(attrs={'size': 20, 'class': 'myDateField'}) + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) + +class AdminTimeWidgetTest(DjangoTestCase): + def test_attrs(self): + """ + Ensure that user-supplied attrs are used. + Refs #12073. + """ + w = widgets.AdminTimeWidget() + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) + # pass attrs to widget + w = widgets.AdminTimeWidget(attrs={'size': 20, 'class': 'myTimeField'}) + self.assertEqual( + conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), + '', + ) class AdminSplitDateTimeWidgetTest(DjangoTestCase): def test_render(self):