Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2674 -- Added stricter date validation so that things like 200…

…6-11-31

are caught. Thanks, Gary Wilson.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3815 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a6a4e9b2441d1f70d345596528fe39f09ae96874 1 parent b05e550
Malcolm Tredinnick authored
1  AUTHORS
@@ -151,6 +151,7 @@ answer newbie questions, and generally made Django that much better:
151 151
     Milton Waddams
152 152
     Dan Watson <http://theidioteque.net/>
153 153
     Rachel Willmer <http://www.willmer.com/kb/>
  154
+    Gary Wilson <gary.wilson@gmail.com>
154 155
     wojtek
155 156
     ye7cakf02@sneakemail.com
156 157
     Cheng Zhang
23  django/core/validators.py
@@ -13,7 +13,7 @@
13 13
 from django.utils.functional import Promise, lazy
14 14
 import re
15 15
 
16  
-_datere = r'(19|2\d)\d{2}-((?:0?[1-9])|(?:1[0-2]))-((?:0?[1-9])|(?:[12][0-9])|(?:3[0-1]))'
  16
+_datere = r'\d{4}-\d{1,2}-\d{1,2}'
17 17
 _timere = r'(?:[01]?[0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?'
18 18
 alnum_re = re.compile(r'^\w+$')
19 19
 alnumurl_re = re.compile(r'^[-\w/]+$')
@@ -122,9 +122,29 @@ def isOnlyLetters(field_data, all_data):
122 122
     if not field_data.isalpha():
123 123
         raise ValidationError, gettext("Only alphabetical characters are allowed here.")
124 124
 
  125
+def _isValidDate(date_string):
  126
+    """
  127
+    A helper function used by isValidANSIDate and isValidANSIDatetime to
  128
+    check if the date is valid.  The date string is assumed to already be in
  129
+    YYYY-MM-DD format.
  130
+    """
  131
+    from datetime import date
  132
+    # Could use time.strptime here and catch errors, but datetime.date below
  133
+    # produces much friendlier error messages.
  134
+    year, month, day = map(int, date_string.split('-'))
  135
+    # This check is needed because strftime is used when saving the date
  136
+    # value to the database, and strftime requires that the year be >=1900.
  137
+    if year < 1900:
  138
+        raise ValidationError, gettext('Year must be 1900 or later.')
  139
+    try:
  140
+        date(year, month, day)
  141
+    except ValueError, e:
  142
+        raise ValidationError, gettext('Invalid date: %s.' % e)    
  143
+
125 144
 def isValidANSIDate(field_data, all_data):
126 145
     if not ansi_date_re.search(field_data):
127 146
         raise ValidationError, gettext('Enter a valid date in YYYY-MM-DD format.')
  147
+    _isValidDate(field_data)
128 148
 
129 149
 def isValidANSITime(field_data, all_data):
130 150
     if not ansi_time_re.search(field_data):
@@ -133,6 +153,7 @@ def isValidANSITime(field_data, all_data):
133 153
 def isValidANSIDatetime(field_data, all_data):
134 154
     if not ansi_datetime_re.search(field_data):
135 155
         raise ValidationError, gettext('Enter a valid date/time in YYYY-MM-DD HH:MM format.')
  156
+    _isValidDate(field_data.split()[0])
136 157
 
137 158
 def isValidEmail(field_data, all_data):
138 159
     if not email_re.search(field_data):

0 notes on commit a6a4e9b

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