Code

Ticket #13560: 13560.2.diff

File 13560.2.diff, 10.2 KB (added by russellm, 4 years ago)

Minor cleanup of v1 patch

Line 
1diff -r 4e639b179fc5 django/contrib/admin/widgets.py
2--- a/django/contrib/admin/widgets.py   Fri May 21 08:54:15 2010 +0000
3+++ b/django/contrib/admin/widgets.py   Fri May 21 17:24:05 2010 +0800
4@@ -44,7 +44,7 @@
5             (name, self.verbose_name.replace('"', '\\"'), int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX))
6         return mark_safe(u''.join(output))
7 
8-class AdminDateWidget(forms.DateTimeInput):
9+class AdminDateWidget(forms.DateInput):
10     class Media:
11         js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js",
12               settings.ADMIN_MEDIA_PREFIX + "js/admin/DateTimeShortcuts.js")
13diff -r 4e639b179fc5 django/forms/fields.py
14--- a/django/forms/fields.py    Fri May 21 08:54:15 2010 +0000
15+++ b/django/forms/fields.py    Fri May 21 17:24:05 2010 +0800
16@@ -105,6 +105,8 @@
17 
18         # Trigger the localization machinery if needed.
19         self.localize = localize
20+        if self.localize:
21+            widget.is_localized = True
22 
23         # Hook into self.widget_attrs() for any Field-specific HTML attributes.
24         extra_attrs = self.widget_attrs(widget)
25@@ -125,9 +127,6 @@
26 
27         self.validators = self.default_validators + validators
28 
29-    def localize_value(self, value):
30-        return formats.localize_input(value)
31-
32     def to_python(self, value):
33         return value
34 
35@@ -843,9 +842,14 @@
36         errors = self.default_error_messages.copy()
37         if 'error_messages' in kwargs:
38             errors.update(kwargs['error_messages'])
39+        localize = kwargs.get('localize', False)
40         fields = (
41-            DateField(input_formats=input_date_formats, error_messages={'invalid': errors['invalid_date']}),
42-            TimeField(input_formats=input_time_formats, error_messages={'invalid': errors['invalid_time']}),
43+            DateField(input_formats=input_date_formats,
44+                      error_messages={'invalid': errors['invalid_date']},
45+                      localize=localize),
46+            TimeField(input_formats=input_time_formats,
47+                      error_messages={'invalid': errors['invalid_time']},
48+                      localize=localize),
49         )
50         super(SplitDateTimeField, self).__init__(fields, *args, **kwargs)
51 
52diff -r 4e639b179fc5 django/forms/forms.py
53--- a/django/forms/forms.py     Fri May 21 08:54:15 2010 +0000
54+++ b/django/forms/forms.py     Fri May 21 17:24:05 2010 +0800
55@@ -443,8 +443,6 @@
56             name = self.html_name
57         else:
58             name = self.html_initial_name
59-        if self.field.localize:
60-            data = self.field.localize_value(data)
61         return widget.render(name, data, attrs=attrs)
62 
63     def as_text(self, attrs=None, **kwargs):
64diff -r 4e639b179fc5 django/forms/widgets.py
65--- a/django/forms/widgets.py   Fri May 21 08:54:15 2010 +0000
66+++ b/django/forms/widgets.py   Fri May 21 17:24:05 2010 +0800
67@@ -10,7 +10,7 @@
68 from django.utils.translation import ugettext
69 from django.utils.encoding import StrAndUnicode, force_unicode
70 from django.utils.safestring import mark_safe
71-from django.utils import formats
72+from django.utils import datetime_safe, formats
73 import time
74 import datetime
75 from util import flatatt
76@@ -133,6 +133,7 @@
77     __metaclass__ = MediaDefiningClass
78     is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
79     needs_multipart_form = False # Determines does this widget need multipart-encrypted form
80+    is_localized = False
81 
82     def __init__(self, attrs=None):
83         if attrs is not None:
84@@ -208,12 +209,18 @@
85     """
86     input_type = None # Subclasses must define this.
87 
88+    def _format_value(self, value):
89+        if self.is_localized:
90+            return formats.localize_input(value)
91+        return value
92+
93     def render(self, name, value, attrs=None):
94-        if value is None: value = ''
95+        if value is None:
96+            value = ''
97         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
98         if value != '':
99             # Only add the 'value' attribute if a value is non-empty.
100-            final_attrs['value'] = force_unicode(value)
101+            final_attrs['value'] = force_unicode(self._format_value(value))
102         return mark_safe(u'<input%s />' % flatatt(final_attrs))
103 
104 class TextInput(Input):
105@@ -295,7 +302,7 @@
106 
107 class DateInput(Input):
108     input_type = 'text'
109-    format = None
110+    format = '%Y-%m-%d'     # '2006-10-25'
111 
112     def __init__(self, attrs=None, format=None):
113         super(DateInput, self).__init__(attrs)
114@@ -303,16 +310,13 @@
115             self.format = format
116 
117     def _format_value(self, value):
118-        if value is None:
119-            return ''
120+        if self.is_localized:
121+            return formats.localize_input(value)
122         elif hasattr(value, 'strftime'):
123-            return formats.localize_input(value, self.format)
124+            value = datetime_safe.new_date(value)
125+            return value.strftime(self.format)
126         return value
127 
128-    def render(self, name, value, attrs=None):
129-        value = self._format_value(value)
130-        return super(DateInput, self).render(name, value, attrs)
131-
132     def _has_changed(self, initial, data):
133         # If our field has show_hidden_initial=True, initial will be a string
134         # formatted by HiddenInput using formats.localize_input, which is not
135@@ -326,7 +330,7 @@
136 
137 class DateTimeInput(Input):
138     input_type = 'text'
139-    format = None
140+    format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'
141 
142     def __init__(self, attrs=None, format=None):
143         super(DateTimeInput, self).__init__(attrs)
144@@ -334,16 +338,13 @@
145             self.format = format
146 
147     def _format_value(self, value):
148-        if value is None:
149-            return ''
150+        if self.is_localized:
151+            return formats.localize_input(value)
152         elif hasattr(value, 'strftime'):
153-            return formats.localize_input(value, self.format)
154+            value = datetime_safe.new_datetime(value)
155+            return value.strftime(self.format)
156         return value
157 
158-    def render(self, name, value, attrs=None):
159-        value = self._format_value(value)
160-        return super(DateTimeInput, self).render(name, value, attrs)
161-
162     def _has_changed(self, initial, data):
163         # If our field has show_hidden_initial=True, initial will be a string
164         # formatted by HiddenInput using formats.localize_input, which is not
165@@ -357,7 +358,7 @@
166 
167 class TimeInput(Input):
168     input_type = 'text'
169-    format = None
170+    format = '%H:%M:%S'     # '14:30:59'
171 
172     def __init__(self, attrs=None, format=None):
173         super(TimeInput, self).__init__(attrs)
174@@ -365,16 +366,12 @@
175             self.format = format
176 
177     def _format_value(self, value):
178-        if value is None:
179-            return ''
180+        if self.is_localized:
181+            return formats.localize_input(value)
182         elif hasattr(value, 'strftime'):
183-            return formats.localize_input(value, self.format)
184+            return value.strftime(self.format)
185         return value
186 
187-    def render(self, name, value, attrs=None):
188-        value = self._format_value(value)
189-        return super(TimeInput, self).render(name, value, attrs)
190-
191     def _has_changed(self, initial, data):
192         # If our field has show_hidden_initial=True, initial will be a string
193         # formatted by HiddenInput using formats.localize_input, which is not
194@@ -674,6 +671,9 @@
195         super(MultiWidget, self).__init__(attrs)
196 
197     def render(self, name, value, attrs=None):
198+        if self.is_localized:
199+            for widget in self.widgets:
200+                widget.is_localized = self.is_localized
201         # value is a list of values, each corresponding to a widget
202         # in self.widgets.
203         if not isinstance(value, list):
204diff -r 4e639b179fc5 tests/regressiontests/admin_widgets/models.py
205--- a/tests/regressiontests/admin_widgets/models.py     Fri May 21 08:54:15 2010 +0000
206+++ b/tests/regressiontests/admin_widgets/models.py     Fri May 21 17:24:05 2010 +0800
207@@ -105,6 +105,7 @@
208 <p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
209 >>> activate('de-at')
210 >>> settings.USE_L10N = True
211+>>> w.is_localized = True
212 >>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
213 <p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
214 >>> deactivate()
215diff -r 4e639b179fc5 tests/regressiontests/forms/widgets.py
216--- a/tests/regressiontests/forms/widgets.py    Fri May 21 08:54:15 2010 +0000
217+++ b/tests/regressiontests/forms/widgets.py    Fri May 21 17:24:05 2010 +0800
218@@ -1135,6 +1135,7 @@
219 u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
220 >>> activate('de-at')
221 >>> settings.USE_L10N = True
222+>>> w.is_localized = True
223 >>> w.render('date', d)
224 u'<input type="text" name="date" value="17.09.2007 12:51:34" />'
225 >>> deactivate()
226@@ -1176,6 +1177,7 @@
227 
228 >>> activate('de-at')
229 >>> settings.USE_L10N = True
230+>>> w.is_localized = True
231 >>> w.render('date', d)
232 u'<input type="text" name="date" value="17.09.2007" />'
233 >>> deactivate()
234@@ -1220,6 +1222,7 @@
235 
236 >>> activate('de-at')
237 >>> settings.USE_L10N = True
238+>>> w.is_localized = True
239 >>> w.render('date', d)
240 u'<input type="text" name="date" value="17.09.2007" />'
241 >>> deactivate()
242@@ -1259,6 +1262,7 @@
243 u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" name="date_1" value="12:51:00" />'
244 >>> activate('de-at')
245 >>> settings.USE_L10N = True
246+>>> w.is_localized = True
247 >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
248 u'<input type="hidden" name="date_0" value="17.09.2007" /><input type="hidden" name="date_1" value="12:51:00" />'
249 >>> deactivate()
250diff -r 4e639b179fc5 tests/regressiontests/i18n/forms.py
251--- a/tests/regressiontests/i18n/forms.py       Fri May 21 08:54:15 2010 +0000
252+++ b/tests/regressiontests/i18n/forms.py       Fri May 21 17:24:05 2010 +0800
253@@ -16,6 +16,7 @@
254 class CompanyForm(forms.ModelForm):
255     cents_payed = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)
256     products_delivered = forms.IntegerField(localize=True)
257+    date_added = forms.DateTimeField(localize=True)
258 
259     class Meta:
260         model = Company