Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13621 -- Corrected the handling of input formats on date/time …

…form fields. Thanks to bufke for the report, zerok and jacmkno for their work on the patch, and Karen, Jannis and Alex for feedback.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13484 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 75d018f63164a9e307c0e7604af903fbbe23c21f 1 parent 25f0ff4
Russell Keith-Magee authored August 05, 2010
26  django/forms/widgets.py
@@ -308,9 +308,13 @@ def __init__(self, attrs=None, format=None):
308 308
         super(DateInput, self).__init__(attrs)
309 309
         if format:
310 310
             self.format = format
  311
+            self.manual_format = True
  312
+        else:
  313
+            self.format = formats.get_format('DATE_INPUT_FORMATS')[0]
  314
+            self.manual_format = False
311 315
 
312 316
     def _format_value(self, value):
313  
-        if self.is_localized:
  317
+        if self.is_localized and not self.manual_format:
314 318
             return formats.localize_input(value)
315 319
         elif hasattr(value, 'strftime'):
316 320
             value = datetime_safe.new_date(value)
@@ -336,9 +340,13 @@ def __init__(self, attrs=None, format=None):
336 340
         super(DateTimeInput, self).__init__(attrs)
337 341
         if format:
338 342
             self.format = format
  343
+            self.manual_format = True
  344
+        else:
  345
+            self.format = formats.get_format('DATETIME_INPUT_FORMATS')[0]
  346
+            self.manual_format = False
339 347
 
340 348
     def _format_value(self, value):
341  
-        if self.is_localized:
  349
+        if self.is_localized and not self.manual_format:
342 350
             return formats.localize_input(value)
343 351
         elif hasattr(value, 'strftime'):
344 352
             value = datetime_safe.new_datetime(value)
@@ -364,9 +372,13 @@ def __init__(self, attrs=None, format=None):
364 372
         super(TimeInput, self).__init__(attrs)
365 373
         if format:
366 374
             self.format = format
  375
+            self.manual_format = True
  376
+        else:
  377
+            self.format = formats.get_format('TIME_INPUT_FORMATS')[0]
  378
+            self.manual_format = False
367 379
 
368 380
     def _format_value(self, value):
369  
-        if self.is_localized:
  381
+        if self.is_localized and not self.manual_format:
370 382
             return formats.localize_input(value)
371 383
         elif hasattr(value, 'strftime'):
372 384
             return value.strftime(self.format)
@@ -751,12 +763,8 @@ class SplitDateTimeWidget(MultiWidget):
751 763
     time_format = TimeInput.format
752 764
 
753 765
     def __init__(self, attrs=None, date_format=None, time_format=None):
754  
-        if date_format:
755  
-            self.date_format = date_format
756  
-        if time_format:
757  
-            self.time_format = time_format
758  
-        widgets = (DateInput(attrs=attrs, format=self.date_format),
759  
-                   TimeInput(attrs=attrs, format=self.time_format))
  766
+        widgets = (DateInput(attrs=attrs, format=date_format),
  767
+                   TimeInput(attrs=attrs, format=time_format))
760 768
         super(SplitDateTimeWidget, self).__init__(widgets, attrs)
761 769
 
762 770
     def decompress(self, value):
894  tests/regressiontests/forms/input_formats.py
... ...
@@ -0,0 +1,894 @@
  1
+from datetime import time, date, datetime
  2
+from unittest import TestCase
  3
+
  4
+from django import forms
  5
+from django.conf import settings
  6
+from django.utils.translation import activate, deactivate
  7
+
  8
+
  9
+class LocalizedTimeTests(TestCase):
  10
+    def setUp(self):
  11
+        self.old_TIME_INPUT_FORMATS = settings.TIME_INPUT_FORMATS
  12
+        self.old_USE_L10N = settings.USE_L10N
  13
+
  14
+        settings.TIME_INPUT_FORMATS = ["%I:%M:%S %p", "%I:%M %p"]
  15
+        settings.USE_L10N = True
  16
+
  17
+        activate('de')
  18
+
  19
+    def tearDown(self):
  20
+        settings.TIME_INPUT_FORMATS = self.old_TIME_INPUT_FORMATS
  21
+        settings.USE_L10N = self.old_USE_L10N
  22
+
  23
+        deactivate()
  24
+
  25
+    def test_timeField(self):
  26
+        "TimeFields can parse dates in the default format"
  27
+        f = forms.TimeField()
  28
+        # Parse a time in an unaccepted format; get an error
  29
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  30
+
  31
+        # Parse a time in a valid format, get a parsed result
  32
+        result = f.clean('13:30:05')
  33
+        self.assertEqual(result, time(13,30,5))
  34
+
  35
+        # Check that the parsed result does a round trip
  36
+        text = f.widget._format_value(result)
  37
+        self.assertEqual(text, '13:30:05')
  38
+
  39
+        # Parse a time in a valid, but non-default format, get a parsed result
  40
+        result = f.clean('13:30')
  41
+        self.assertEqual(result, time(13,30,0))
  42
+
  43
+        # Check that the parsed result does a round trip to default format
  44
+        text = f.widget._format_value(result)
  45
+        self.assertEqual(text, "13:30:00")
  46
+
  47
+    def test_localized_timeField(self):
  48
+        "Localized TimeFields act as unlocalized widgets"
  49
+        f = forms.TimeField(localize=True)
  50
+        # Parse a time in an unaccepted format; get an error
  51
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  52
+
  53
+        # Parse a time in a valid format, get a parsed result
  54
+        result = f.clean('13:30:05')
  55
+        self.assertEqual(result, time(13,30,5))
  56
+
  57
+        # Check that the parsed result does a round trip to the same format
  58
+        text = f.widget._format_value(result)
  59
+        self.assertEqual(text, '13:30:05')
  60
+
  61
+        # Parse a time in a valid format, get a parsed result
  62
+        result = f.clean('13:30')
  63
+        self.assertEqual(result, time(13,30,0))
  64
+
  65
+        # Check that the parsed result does a round trip to default format
  66
+        text = f.widget._format_value(result)
  67
+        self.assertEqual(text, "13:30:00")
  68
+
  69
+    def test_timeField_with_inputformat(self):
  70
+        "TimeFields with manually specified input formats can accept those formats"
  71
+        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"])
  72
+        # Parse a time in an unaccepted format; get an error
  73
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  74
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  75
+
  76
+        # Parse a time in a valid format, get a parsed result
  77
+        result = f.clean('13.30.05')
  78
+        self.assertEqual(result, time(13,30,5))
  79
+
  80
+        # Check that the parsed result does a round trip to the same format
  81
+        text = f.widget._format_value(result)
  82
+        self.assertEqual(text, "13:30:05")
  83
+
  84
+        # Parse a time in a valid format, get a parsed result
  85
+        result = f.clean('13.30')
  86
+        self.assertEqual(result, time(13,30,0))
  87
+
  88
+        # Check that the parsed result does a round trip to default format
  89
+        text = f.widget._format_value(result)
  90
+        self.assertEqual(text, "13:30:00")
  91
+
  92
+    def test_localized_timeField_with_inputformat(self):
  93
+        "Localized TimeFields with manually specified input formats can accept those formats"
  94
+        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True)
  95
+        # Parse a time in an unaccepted format; get an error
  96
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  97
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  98
+
  99
+        # Parse a time in a valid format, get a parsed result
  100
+        result = f.clean('13.30.05')
  101
+        self.assertEqual(result, time(13,30,5))
  102
+
  103
+        # # Check that the parsed result does a round trip to the same format
  104
+        text = f.widget._format_value(result)
  105
+        self.assertEqual(text, "13:30:05")
  106
+
  107
+        # Parse a time in a valid format, get a parsed result
  108
+        result = f.clean('13.30')
  109
+        self.assertEqual(result, time(13,30,0))
  110
+
  111
+        # Check that the parsed result does a round trip to default format
  112
+        text = f.widget._format_value(result)
  113
+        self.assertEqual(text, "13:30:00")
  114
+
  115
+
  116
+class CustomTimeInputFormatsTests(TestCase):
  117
+    def setUp(self):
  118
+        self.old_TIME_INPUT_FORMATS = settings.TIME_INPUT_FORMATS
  119
+        settings.TIME_INPUT_FORMATS = ["%I:%M:%S %p", "%I:%M %p"]
  120
+
  121
+    def tearDown(self):
  122
+        settings.TIME_INPUT_FORMATS = self.old_TIME_INPUT_FORMATS
  123
+
  124
+    def test_timeField(self):
  125
+        "TimeFields can parse dates in the default format"
  126
+        f = forms.TimeField()
  127
+        # Parse a time in an unaccepted format; get an error
  128
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  129
+
  130
+        # Parse a time in a valid format, get a parsed result
  131
+        result = f.clean('1:30:05 PM')
  132
+        self.assertEqual(result, time(13,30,5))
  133
+
  134
+        # Check that the parsed result does a round trip
  135
+        text = f.widget._format_value(result)
  136
+        self.assertEqual(text, '01:30:05 PM')
  137
+
  138
+        # Parse a time in a valid, but non-default format, get a parsed result
  139
+        result = f.clean('1:30 PM')
  140
+        self.assertEqual(result, time(13,30,0))
  141
+
  142
+        # Check that the parsed result does a round trip to default format
  143
+        text = f.widget._format_value(result)
  144
+        self.assertEqual(text, "01:30:00 PM")
  145
+
  146
+    def test_localized_timeField(self):
  147
+        "Localized TimeFields act as unlocalized widgets"
  148
+        f = forms.TimeField(localize=True)
  149
+        # Parse a time in an unaccepted format; get an error
  150
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  151
+
  152
+        # Parse a time in a valid format, get a parsed result
  153
+        result = f.clean('1:30:05 PM')
  154
+        self.assertEqual(result, time(13,30,5))
  155
+
  156
+        # Check that the parsed result does a round trip to the same format
  157
+        text = f.widget._format_value(result)
  158
+        self.assertEqual(text, '01:30:05 PM')
  159
+
  160
+        # Parse a time in a valid format, get a parsed result
  161
+        result = f.clean('01:30 PM')
  162
+        self.assertEqual(result, time(13,30,0))
  163
+
  164
+        # Check that the parsed result does a round trip to default format
  165
+        text = f.widget._format_value(result)
  166
+        self.assertEqual(text, "01:30:00 PM")
  167
+
  168
+    def test_timeField_with_inputformat(self):
  169
+        "TimeFields with manually specified input formats can accept those formats"
  170
+        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"])
  171
+        # Parse a time in an unaccepted format; get an error
  172
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  173
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  174
+
  175
+        # Parse a time in a valid format, get a parsed result
  176
+        result = f.clean('13.30.05')
  177
+        self.assertEqual(result, time(13,30,5))
  178
+
  179
+        # Check that the parsed result does a round trip to the same format
  180
+        text = f.widget._format_value(result)
  181
+        self.assertEqual(text, "01:30:05 PM")
  182
+
  183
+        # Parse a time in a valid format, get a parsed result
  184
+        result = f.clean('13.30')
  185
+        self.assertEqual(result, time(13,30,0))
  186
+
  187
+        # Check that the parsed result does a round trip to default format
  188
+        text = f.widget._format_value(result)
  189
+        self.assertEqual(text, "01:30:00 PM")
  190
+
  191
+    def test_localized_timeField_with_inputformat(self):
  192
+        "Localized TimeFields with manually specified input formats can accept those formats"
  193
+        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True)
  194
+        # Parse a time in an unaccepted format; get an error
  195
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  196
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  197
+
  198
+        # Parse a time in a valid format, get a parsed result
  199
+        result = f.clean('13.30.05')
  200
+        self.assertEqual(result, time(13,30,5))
  201
+
  202
+        # # Check that the parsed result does a round trip to the same format
  203
+        text = f.widget._format_value(result)
  204
+        self.assertEqual(text, "01:30:05 PM")
  205
+
  206
+        # Parse a time in a valid format, get a parsed result
  207
+        result = f.clean('13.30')
  208
+        self.assertEqual(result, time(13,30,0))
  209
+
  210
+        # Check that the parsed result does a round trip to default format
  211
+        text = f.widget._format_value(result)
  212
+        self.assertEqual(text, "01:30:00 PM")
  213
+
  214
+
  215
+class SimpleTimeFormatTests(TestCase):
  216
+    def test_timeField(self):
  217
+        "TimeFields can parse dates in the default format"
  218
+        f = forms.TimeField()
  219
+        # Parse a time in an unaccepted format; get an error
  220
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  221
+
  222
+        # Parse a time in a valid format, get a parsed result
  223
+        result = f.clean('13:30:05')
  224
+        self.assertEqual(result, time(13,30,5))
  225
+
  226
+        # Check that the parsed result does a round trip to the same format
  227
+        text = f.widget._format_value(result)
  228
+        self.assertEqual(text, "13:30:05")
  229
+
  230
+        # Parse a time in a valid, but non-default format, get a parsed result
  231
+        result = f.clean('13:30')
  232
+        self.assertEqual(result, time(13,30,0))
  233
+
  234
+        # Check that the parsed result does a round trip to default format
  235
+        text = f.widget._format_value(result)
  236
+        self.assertEqual(text, "13:30:00")
  237
+
  238
+    def test_localized_timeField(self):
  239
+        "Localized TimeFields in a non-localized environment act as unlocalized widgets"
  240
+        f = forms.TimeField()
  241
+        # Parse a time in an unaccepted format; get an error
  242
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
  243
+
  244
+        # Parse a time in a valid format, get a parsed result
  245
+        result = f.clean('13:30:05')
  246
+        self.assertEqual(result, time(13,30,5))
  247
+
  248
+        # Check that the parsed result does a round trip to the same format
  249
+        text = f.widget._format_value(result)
  250
+        self.assertEqual(text, "13:30:05")
  251
+
  252
+        # Parse a time in a valid format, get a parsed result
  253
+        result = f.clean('13:30')
  254
+        self.assertEqual(result, time(13,30,0))
  255
+
  256
+        # Check that the parsed result does a round trip to default format
  257
+        text = f.widget._format_value(result)
  258
+        self.assertEqual(text, "13:30:00")
  259
+
  260
+    def test_timeField_with_inputformat(self):
  261
+        "TimeFields with manually specified input formats can accept those formats"
  262
+        f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"])
  263
+        # Parse a time in an unaccepted format; get an error
  264
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  265
+
  266
+        # Parse a time in a valid format, get a parsed result
  267
+        result = f.clean('1:30:05 PM')
  268
+        self.assertEqual(result, time(13,30,5))
  269
+
  270
+        # Check that the parsed result does a round trip to the same format
  271
+        text = f.widget._format_value(result)
  272
+        self.assertEqual(text, "13:30:05")
  273
+
  274
+        # Parse a time in a valid format, get a parsed result
  275
+        result = f.clean('1:30 PM')
  276
+        self.assertEqual(result, time(13,30,0))
  277
+
  278
+        # Check that the parsed result does a round trip to default format
  279
+        text = f.widget._format_value(result)
  280
+        self.assertEqual(text, "13:30:00")
  281
+
  282
+    def test_localized_timeField_with_inputformat(self):
  283
+        "Localized TimeFields with manually specified input formats can accept those formats"
  284
+        f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"], localize=True)
  285
+        # Parse a time in an unaccepted format; get an error
  286
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
  287
+
  288
+        # Parse a time in a valid format, get a parsed result
  289
+        result = f.clean('1:30:05 PM')
  290
+        self.assertEqual(result, time(13,30,5))
  291
+
  292
+        # Check that the parsed result does a round trip to the same format
  293
+        text = f.widget._format_value(result)
  294
+        self.assertEqual(text, "13:30:05")
  295
+
  296
+        # Parse a time in a valid format, get a parsed result
  297
+        result = f.clean('1:30 PM')
  298
+        self.assertEqual(result, time(13,30,0))
  299
+
  300
+        # Check that the parsed result does a round trip to default format
  301
+        text = f.widget._format_value(result)
  302
+        self.assertEqual(text, "13:30:00")
  303
+
  304
+
  305
+class LocalizedDateTests(TestCase):
  306
+    def setUp(self):
  307
+        self.old_DATE_INPUT_FORMATS = settings.DATE_INPUT_FORMATS
  308
+        self.old_USE_L10N = settings.USE_L10N
  309
+
  310
+        settings.DATE_INPUT_FORMATS = ["%d/%m/%Y", "%d-%m-%Y"]
  311
+        settings.USE_L10N = True
  312
+
  313
+        activate('de')
  314
+
  315
+    def tearDown(self):
  316
+        settings.DATE_INPUT_FORMATS = self.old_DATE_INPUT_FORMATS
  317
+        settings.USE_L10N = self.old_USE_L10N
  318
+
  319
+        deactivate()
  320
+
  321
+    def test_dateField(self):
  322
+        "DateFields can parse dates in the default format"
  323
+        f = forms.DateField()
  324
+        # Parse a date in an unaccepted format; get an error
  325
+        self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
  326
+
  327
+        # Parse a date in a valid format, get a parsed result
  328
+        result = f.clean('21.12.2010')
  329
+        self.assertEqual(result, date(2010,12,21))
  330
+
  331
+        # Check that the parsed result does a round trip
  332
+        text = f.widget._format_value(result)
  333
+        self.assertEqual(text, '21.12.2010')
  334
+
  335
+        # Parse a date in a valid, but non-default format, get a parsed result
  336
+        result = f.clean('21.12.10')
  337
+        self.assertEqual(result, date(2010,12,21))
  338
+
  339
+        # Check that the parsed result does a round trip to default format
  340
+        text = f.widget._format_value(result)
  341
+        self.assertEqual(text, "21.12.2010")
  342
+
  343
+    def test_localized_dateField(self):
  344
+        "Localized DateFields act as unlocalized widgets"
  345
+        f = forms.DateField(localize=True)
  346
+        # Parse a date in an unaccepted format; get an error
  347
+        self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
  348
+
  349
+        # Parse a date in a valid format, get a parsed result
  350
+        result = f.clean('21.12.2010')
  351
+        self.assertEqual(result, date(2010,12,21))
  352
+
  353
+        # Check that the parsed result does a round trip to the same format
  354
+        text = f.widget._format_value(result)
  355
+        self.assertEqual(text, '21.12.2010')
  356
+
  357
+        # Parse a date in a valid format, get a parsed result
  358
+        result = f.clean('21.12.10')
  359
+        self.assertEqual(result, date(2010,12,21))
  360
+
  361
+        # Check that the parsed result does a round trip to default format
  362
+        text = f.widget._format_value(result)
  363
+        self.assertEqual(text, "21.12.2010")
  364
+
  365
+    def test_dateField_with_inputformat(self):
  366
+        "DateFields with manually specified input formats can accept those formats"
  367
+        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"])
  368
+        # Parse a date in an unaccepted format; get an error
  369
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  370
+        self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
  371
+        self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
  372
+
  373
+        # Parse a date in a valid format, get a parsed result
  374
+        result = f.clean('12.21.2010')
  375
+        self.assertEqual(result, date(2010,12,21))
  376
+
  377
+        # Check that the parsed result does a round trip to the same format
  378
+        text = f.widget._format_value(result)
  379
+        self.assertEqual(text, "21.12.2010")
  380
+
  381
+        # Parse a date in a valid format, get a parsed result
  382
+        result = f.clean('12-21-2010')
  383
+        self.assertEqual(result, date(2010,12,21))
  384
+
  385
+        # Check that the parsed result does a round trip to default format
  386
+        text = f.widget._format_value(result)
  387
+        self.assertEqual(text, "21.12.2010")
  388
+
  389
+    def test_localized_dateField_with_inputformat(self):
  390
+        "Localized DateFields with manually specified input formats can accept those formats"
  391
+        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True)
  392
+        # Parse a date in an unaccepted format; get an error
  393
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  394
+        self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
  395
+        self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
  396
+
  397
+        # Parse a date in a valid format, get a parsed result
  398
+        result = f.clean('12.21.2010')
  399
+        self.assertEqual(result, date(2010,12,21))
  400
+
  401
+        # # Check that the parsed result does a round trip to the same format
  402
+        text = f.widget._format_value(result)
  403
+        self.assertEqual(text, "21.12.2010")
  404
+
  405
+        # Parse a date in a valid format, get a parsed result
  406
+        result = f.clean('12-21-2010')
  407
+        self.assertEqual(result, date(2010,12,21))
  408
+
  409
+        # Check that the parsed result does a round trip to default format
  410
+        text = f.widget._format_value(result)
  411
+        self.assertEqual(text, "21.12.2010")
  412
+
  413
+class CustomDateInputFormatsTests(TestCase):
  414
+    def setUp(self):
  415
+        self.old_DATE_INPUT_FORMATS = settings.DATE_INPUT_FORMATS
  416
+        settings.DATE_INPUT_FORMATS = ["%d.%m.%Y", "%d-%m-%Y"]
  417
+
  418
+    def tearDown(self):
  419
+        settings.DATE_INPUT_FORMATS = self.old_DATE_INPUT_FORMATS
  420
+
  421
+    def test_dateField(self):
  422
+        "DateFields can parse dates in the default format"
  423
+        f = forms.DateField()
  424
+        # Parse a date in an unaccepted format; get an error
  425
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  426
+
  427
+        # Parse a date in a valid format, get a parsed result
  428
+        result = f.clean('21.12.2010')
  429
+        self.assertEqual(result, date(2010,12,21))
  430
+
  431
+        # Check that the parsed result does a round trip
  432
+        text = f.widget._format_value(result)
  433
+        self.assertEqual(text, '21.12.2010')
  434
+
  435
+        # Parse a date in a valid, but non-default format, get a parsed result
  436
+        result = f.clean('21-12-2010')
  437
+        self.assertEqual(result, date(2010,12,21))
  438
+
  439
+        # Check that the parsed result does a round trip to default format
  440
+        text = f.widget._format_value(result)
  441
+        self.assertEqual(text, "21.12.2010")
  442
+
  443
+    def test_localized_dateField(self):
  444
+        "Localized DateFields act as unlocalized widgets"
  445
+        f = forms.DateField(localize=True)
  446
+        # Parse a date in an unaccepted format; get an error
  447
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  448
+
  449
+        # Parse a date in a valid format, get a parsed result
  450
+        result = f.clean('21.12.2010')
  451
+        self.assertEqual(result, date(2010,12,21))
  452
+
  453
+        # Check that the parsed result does a round trip to the same format
  454
+        text = f.widget._format_value(result)
  455
+        self.assertEqual(text, '21.12.2010')
  456
+
  457
+        # Parse a date in a valid format, get a parsed result
  458
+        result = f.clean('21-12-2010')
  459
+        self.assertEqual(result, date(2010,12,21))
  460
+
  461
+        # Check that the parsed result does a round trip to default format
  462
+        text = f.widget._format_value(result)
  463
+        self.assertEqual(text, "21.12.2010")
  464
+
  465
+    def test_dateField_with_inputformat(self):
  466
+        "DateFields with manually specified input formats can accept those formats"
  467
+        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"])
  468
+        # Parse a date in an unaccepted format; get an error
  469
+        self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
  470
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  471
+
  472
+        # Parse a date in a valid format, get a parsed result
  473
+        result = f.clean('12.21.2010')
  474
+        self.assertEqual(result, date(2010,12,21))
  475
+
  476
+        # Check that the parsed result does a round trip to the same format
  477
+        text = f.widget._format_value(result)
  478
+        self.assertEqual(text, "21.12.2010")
  479
+
  480
+        # Parse a date in a valid format, get a parsed result
  481
+        result = f.clean('12-21-2010')
  482
+        self.assertEqual(result, date(2010,12,21))
  483
+
  484
+        # Check that the parsed result does a round trip to default format
  485
+        text = f.widget._format_value(result)
  486
+        self.assertEqual(text, "21.12.2010")
  487
+
  488
+    def test_localized_dateField_with_inputformat(self):
  489
+        "Localized DateFields with manually specified input formats can accept those formats"
  490
+        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True)
  491
+        # Parse a date in an unaccepted format; get an error
  492
+        self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
  493
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  494
+
  495
+        # Parse a date in a valid format, get a parsed result
  496
+        result = f.clean('12.21.2010')
  497
+        self.assertEqual(result, date(2010,12,21))
  498
+
  499
+        # # Check that the parsed result does a round trip to the same format
  500
+        text = f.widget._format_value(result)
  501
+        self.assertEqual(text, "21.12.2010")
  502
+
  503
+        # Parse a date in a valid format, get a parsed result
  504
+        result = f.clean('12-21-2010')
  505
+        self.assertEqual(result, date(2010,12,21))
  506
+
  507
+        # Check that the parsed result does a round trip to default format
  508
+        text = f.widget._format_value(result)
  509
+        self.assertEqual(text, "21.12.2010")
  510
+
  511
+class SimpleDateFormatTests(TestCase):
  512
+    def test_dateField(self):
  513
+        "DateFields can parse dates in the default format"
  514
+        f = forms.DateField()
  515
+        # Parse a date in an unaccepted format; get an error
  516
+        self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
  517
+
  518
+        # Parse a date in a valid format, get a parsed result
  519
+        result = f.clean('2010-12-21')
  520
+        self.assertEqual(result, date(2010,12,21))
  521
+
  522
+        # Check that the parsed result does a round trip to the same format
  523
+        text = f.widget._format_value(result)
  524
+        self.assertEqual(text, "2010-12-21")
  525
+
  526
+        # Parse a date in a valid, but non-default format, get a parsed result
  527
+        result = f.clean('12/21/2010')
  528
+        self.assertEqual(result, date(2010,12,21))
  529
+
  530
+        # Check that the parsed result does a round trip to default format
  531
+        text = f.widget._format_value(result)
  532
+        self.assertEqual(text, "2010-12-21")
  533
+
  534
+    def test_localized_dateField(self):
  535
+        "Localized DateFields in a non-localized environment act as unlocalized widgets"
  536
+        f = forms.DateField()
  537
+        # Parse a date in an unaccepted format; get an error
  538
+        self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
  539
+
  540
+        # Parse a date in a valid format, get a parsed result
  541
+        result = f.clean('2010-12-21')
  542
+        self.assertEqual(result, date(2010,12,21))
  543
+
  544
+        # Check that the parsed result does a round trip to the same format
  545
+        text = f.widget._format_value(result)
  546
+        self.assertEqual(text, "2010-12-21")
  547
+
  548
+        # Parse a date in a valid format, get a parsed result
  549
+        result = f.clean('12/21/2010')
  550
+        self.assertEqual(result, date(2010,12,21))
  551
+
  552
+        # Check that the parsed result does a round trip to default format
  553
+        text = f.widget._format_value(result)
  554
+        self.assertEqual(text, "2010-12-21")
  555
+
  556
+    def test_dateField_with_inputformat(self):
  557
+        "DateFields with manually specified input formats can accept those formats"
  558
+        f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"])
  559
+        # Parse a date in an unaccepted format; get an error
  560
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  561
+
  562
+        # Parse a date in a valid format, get a parsed result
  563
+        result = f.clean('21.12.2010')
  564
+        self.assertEqual(result, date(2010,12,21))
  565
+
  566
+        # Check that the parsed result does a round trip to the same format
  567
+        text = f.widget._format_value(result)
  568
+        self.assertEqual(text, "2010-12-21")
  569
+
  570
+        # Parse a date in a valid format, get a parsed result
  571
+        result = f.clean('21-12-2010')
  572
+        self.assertEqual(result, date(2010,12,21))
  573
+
  574
+        # Check that the parsed result does a round trip to default format
  575
+        text = f.widget._format_value(result)
  576
+        self.assertEqual(text, "2010-12-21")
  577
+
  578
+    def test_localized_dateField_with_inputformat(self):
  579
+        "Localized DateFields with manually specified input formats can accept those formats"
  580
+        f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"], localize=True)
  581
+        # Parse a date in an unaccepted format; get an error
  582
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
  583
+
  584
+        # Parse a date in a valid format, get a parsed result
  585
+        result = f.clean('21.12.2010')
  586
+        self.assertEqual(result, date(2010,12,21))
  587
+
  588
+        # Check that the parsed result does a round trip to the same format
  589
+        text = f.widget._format_value(result)
  590
+        self.assertEqual(text, "2010-12-21")
  591
+
  592
+        # Parse a date in a valid format, get a parsed result
  593
+        result = f.clean('21-12-2010')
  594
+        self.assertEqual(result, date(2010,12,21))
  595
+
  596
+        # Check that the parsed result does a round trip to default format
  597
+        text = f.widget._format_value(result)
  598
+        self.assertEqual(text, "2010-12-21")
  599
+
  600
+class LocalizedDateTimeTests(TestCase):
  601
+    def setUp(self):
  602
+        self.old_DATETIME_INPUT_FORMATS = settings.DATETIME_INPUT_FORMATS
  603
+        self.old_USE_L10N = settings.USE_L10N
  604
+
  605
+        settings.DATETIME_INPUT_FORMATS = ["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"]
  606
+        settings.USE_L10N = True
  607
+
  608
+        activate('de')
  609
+
  610
+    def tearDown(self):
  611
+        settings.DATETIME_INPUT_FORMATS = self.old_DATETIME_INPUT_FORMATS
  612
+        settings.USE_L10N = self.old_USE_L10N
  613
+
  614
+        deactivate()
  615
+
  616
+    def test_dateTimeField(self):
  617
+        "DateTimeFields can parse dates in the default format"
  618
+        f = forms.DateTimeField()
  619
+        # Parse a date in an unaccepted format; get an error
  620
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
  621
+
  622
+        # Parse a date in a valid format, get a parsed result
  623
+        result = f.clean('21.12.2010 13:30:05')
  624
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  625
+
  626
+        # Check that the parsed result does a round trip
  627
+        text = f.widget._format_value(result)
  628
+        self.assertEqual(text, '21.12.2010 13:30:05')
  629
+
  630
+        # Parse a date in a valid, but non-default format, get a parsed result
  631
+        result = f.clean('21.12.2010 13:30')
  632
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  633
+
  634
+        # Check that the parsed result does a round trip to default format
  635
+        text = f.widget._format_value(result)
  636
+        self.assertEqual(text, "21.12.2010 13:30:00")
  637
+
  638
+    def test_localized_dateTimeField(self):
  639
+        "Localized DateTimeFields act as unlocalized widgets"
  640
+        f = forms.DateTimeField(localize=True)
  641
+        # Parse a date in an unaccepted format; get an error
  642
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
  643
+
  644
+        # Parse a date in a valid format, get a parsed result
  645
+        result = f.clean('21.12.2010 13:30:05')
  646
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  647
+
  648
+        # Check that the parsed result does a round trip to the same format
  649
+        text = f.widget._format_value(result)
  650
+        self.assertEqual(text, '21.12.2010 13:30:05')
  651
+
  652
+        # Parse a date in a valid format, get a parsed result
  653
+        result = f.clean('21.12.2010 13:30')
  654
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  655
+
  656
+        # Check that the parsed result does a round trip to default format
  657
+        text = f.widget._format_value(result)
  658
+        self.assertEqual(text, "21.12.2010 13:30:00")
  659
+
  660
+    def test_dateTimeField_with_inputformat(self):
  661
+        "DateTimeFields with manually specified input formats can accept those formats"
  662
+        f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"])
  663
+        # Parse a date in an unaccepted format; get an error
  664
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05 13:30:05')
  665
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
  666
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
  667
+
  668
+        # Parse a date in a valid format, get a parsed result
  669
+        result = f.clean('13.30.05 12.21.2010')
  670
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  671
+
  672
+        # Check that the parsed result does a round trip to the same format
  673
+        text = f.widget._format_value(result)
  674
+        self.assertEqual(text, "21.12.2010 13:30:05")
  675
+
  676
+        # Parse a date in a valid format, get a parsed result
  677
+        result = f.clean('13.30 12-21-2010')
  678
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  679
+
  680
+        # Check that the parsed result does a round trip to default format
  681
+        text = f.widget._format_value(result)
  682
+        self.assertEqual(text, "21.12.2010 13:30:00")
  683
+
  684
+    def test_localized_dateTimeField_with_inputformat(self):
  685
+        "Localized DateTimeFields with manually specified input formats can accept those formats"
  686
+        f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"], localize=True)
  687
+        # Parse a date in an unaccepted format; get an error
  688
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
  689
+        self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
  690
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
  691
+
  692
+        # Parse a date in a valid format, get a parsed result
  693
+        result = f.clean('13.30.05 12.21.2010')
  694
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  695
+
  696
+        # # Check that the parsed result does a round trip to the same format
  697
+        text = f.widget._format_value(result)
  698
+        self.assertEqual(text, "21.12.2010 13:30:05")
  699
+
  700
+        # Parse a date in a valid format, get a parsed result
  701
+        result = f.clean('13.30 12-21-2010')
  702
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  703
+
  704
+        # Check that the parsed result does a round trip to default format
  705
+        text = f.widget._format_value(result)
  706
+        self.assertEqual(text, "21.12.2010 13:30:00")
  707
+
  708
+
  709
+class CustomDateTimeInputFormatsTests(TestCase):
  710
+    def setUp(self):
  711
+        self.old_DATETIME_INPUT_FORMATS = settings.DATETIME_INPUT_FORMATS
  712
+        settings.DATETIME_INPUT_FORMATS = ["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"]
  713
+
  714
+    def tearDown(self):
  715
+        settings.DATETIME_INPUT_FORMATS = self.old_DATETIME_INPUT_FORMATS
  716
+
  717
+    def test_dateTimeField(self):
  718
+        "DateTimeFields can parse dates in the default format"
  719
+        f = forms.DateTimeField()
  720
+        # Parse a date in an unaccepted format; get an error
  721
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
  722
+
  723
+        # Parse a date in a valid format, get a parsed result
  724
+        result = f.clean('1:30:05 PM 21/12/2010')
  725
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  726
+
  727
+        # Check that the parsed result does a round trip
  728
+        text = f.widget._format_value(result)
  729
+        self.assertEqual(text, '01:30:05 PM 21/12/2010')
  730
+
  731
+        # Parse a date in a valid, but non-default format, get a parsed result
  732
+        result = f.clean('1:30 PM 21-12-2010')
  733
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  734
+
  735
+        # Check that the parsed result does a round trip to default format
  736
+        text = f.widget._format_value(result)
  737
+        self.assertEqual(text, "01:30:00 PM 21/12/2010")
  738
+
  739
+    def test_localized_dateTimeField(self):
  740
+        "Localized DateTimeFields act as unlocalized widgets"
  741
+        f = forms.DateTimeField(localize=True)
  742
+        # Parse a date in an unaccepted format; get an error
  743
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
  744
+
  745
+        # Parse a date in a valid format, get a parsed result
  746
+        result = f.clean('1:30:05 PM 21/12/2010')
  747
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  748
+
  749
+        # Check that the parsed result does a round trip to the same format
  750
+        text = f.widget._format_value(result)
  751
+        self.assertEqual(text, '01:30:05 PM 21/12/2010')
  752
+
  753
+        # Parse a date in a valid format, get a parsed result
  754
+        result = f.clean('1:30 PM 21-12-2010')
  755
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  756
+
  757
+        # Check that the parsed result does a round trip to default format
  758
+        text = f.widget._format_value(result)
  759
+        self.assertEqual(text, "01:30:00 PM 21/12/2010")
  760
+
  761
+    def test_dateTimeField_with_inputformat(self):
  762
+        "DateTimeFields with manually specified input formats can accept those formats"
  763
+        f = forms.DateTimeField(input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"])
  764
+        # Parse a date in an unaccepted format; get an error
  765
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
  766
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
  767
+
  768
+        # Parse a date in a valid format, get a parsed result
  769
+        result = f.clean('12.21.2010 13:30:05')
  770
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  771
+
  772
+        # Check that the parsed result does a round trip to the same format
  773
+        text = f.widget._format_value(result)
  774
+        self.assertEqual(text, "01:30:05 PM 21/12/2010")
  775
+
  776
+        # Parse a date in a valid format, get a parsed result
  777
+        result = f.clean('12-21-2010 13:30')
  778
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  779
+
  780
+        # Check that the parsed result does a round trip to default format
  781
+        text = f.widget._format_value(result)
  782
+        self.assertEqual(text, "01:30:00 PM 21/12/2010")
  783
+
  784
+    def test_localized_dateTimeField_with_inputformat(self):
  785
+        "Localized DateTimeFields with manually specified input formats can accept those formats"
  786
+        f = forms.DateTimeField(input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"], localize=True)
  787
+        # Parse a date in an unaccepted format; get an error
  788
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
  789
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
  790
+
  791
+        # Parse a date in a valid format, get a parsed result
  792
+        result = f.clean('12.21.2010 13:30:05')
  793
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  794
+
  795
+        # # Check that the parsed result does a round trip to the same format
  796
+        text = f.widget._format_value(result)
  797
+        self.assertEqual(text, "01:30:05 PM 21/12/2010")
  798
+
  799
+        # Parse a date in a valid format, get a parsed result
  800
+        result = f.clean('12-21-2010 13:30')
  801
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  802
+
  803
+        # Check that the parsed result does a round trip to default format
  804
+        text = f.widget._format_value(result)
  805
+        self.assertEqual(text, "01:30:00 PM 21/12/2010")
  806
+
  807
+class SimpleDateTimeFormatTests(TestCase):
  808
+    def test_dateTimeField(self):
  809
+        "DateTimeFields can parse dates in the default format"
  810
+        f = forms.DateTimeField()
  811
+        # Parse a date in an unaccepted format; get an error
  812
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
  813
+
  814
+        # Parse a date in a valid format, get a parsed result
  815
+        result = f.clean('2010-12-21 13:30:05')
  816
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  817
+
  818
+        # Check that the parsed result does a round trip to the same format
  819
+        text = f.widget._format_value(result)
  820
+        self.assertEqual(text, "2010-12-21 13:30:05")
  821
+
  822
+        # Parse a date in a valid, but non-default format, get a parsed result
  823
+        result = f.clean('12/21/2010 13:30:05')
  824
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  825
+
  826
+        # Check that the parsed result does a round trip to default format
  827
+        text = f.widget._format_value(result)
  828
+        self.assertEqual(text, "2010-12-21 13:30:05")
  829
+
  830
+    def test_localized_dateTimeField(self):
  831
+        "Localized DateTimeFields in a non-localized environment act as unlocalized widgets"
  832
+        f = forms.DateTimeField()
  833
+        # Parse a date in an unaccepted format; get an error
  834
+        self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
  835
+
  836
+        # Parse a date in a valid format, get a parsed result
  837
+        result = f.clean('2010-12-21 13:30:05')
  838
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  839
+
  840
+        # Check that the parsed result does a round trip to the same format
  841
+        text = f.widget._format_value(result)
  842
+        self.assertEqual(text, "2010-12-21 13:30:05")
  843
+
  844
+        # Parse a date in a valid format, get a parsed result
  845
+        result = f.clean('12/21/2010 13:30:05')
  846
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  847
+
  848
+        # Check that the parsed result does a round trip to default format
  849
+        text = f.widget._format_value(result)
  850
+        self.assertEqual(text, "2010-12-21 13:30:05")
  851
+
  852
+    def test_dateTimeField_with_inputformat(self):
  853
+        "DateTimeFields with manually specified input formats can accept those formats"
  854
+        f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"])
  855
+        # Parse a date in an unaccepted format; get an error
  856
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
  857
+
  858
+        # Parse a date in a valid format, get a parsed result
  859
+        result = f.clean('1:30:05 PM 21.12.2010')
  860
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  861
+
  862
+        # Check that the parsed result does a round trip to the same format
  863
+        text = f.widget._format_value(result)
  864
+        self.assertEqual(text, "2010-12-21 13:30:05")
  865
+
  866
+        # Parse a date in a valid format, get a parsed result
  867
+        result = f.clean('1:30 PM 21-12-2010')
  868
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  869
+
  870
+        # Check that the parsed result does a round trip to default format
  871
+        text = f.widget._format_value(result)
  872
+        self.assertEqual(text, "2010-12-21 13:30:00")
  873
+
  874
+    def test_localized_dateTimeField_with_inputformat(self):
  875
+        "Localized DateTimeFields with manually specified input formats can accept those formats"
  876
+        f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"], localize=True)
  877
+        # Parse a date in an unaccepted format; get an error
  878
+        self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
  879
+
  880
+        # Parse a date in a valid format, get a parsed result
  881
+        result = f.clean('1:30:05 PM 21.12.2010')
  882
+        self.assertEqual(result, datetime(2010,12,21,13,30,5))
  883
+
  884
+        # Check that the parsed result does a round trip to the same format
  885
+        text = f.widget._format_value(result)
  886
+        self.assertEqual(text, "2010-12-21 13:30:05")
  887
+
  888
+        # Parse a date in a valid format, get a parsed result
  889
+        result = f.clean('1:30 PM 21-12-2010')
  890
+        self.assertEqual(result, datetime(2010,12,21,13,30))
  891
+
  892
+        # Check that the parsed result does a round trip to default format
  893
+        text = f.widget._format_value(result)
  894
+        self.assertEqual(text, "2010-12-21 13:30:00")
2  tests/regressiontests/forms/tests.py
@@ -41,6 +41,8 @@
41 41
 from validators import TestFieldWithValidators
42 42
 from widgets import WidgetTests
43 43
 
  44
+from input_formats import *
  45
+
44 46
 __test__ = {
45 47
     'extra_tests': extra_tests,
46 48
     'form_tests': form_tests,

0 notes on commit 75d018f

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