summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2006-09-24 12:09:32 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2006-09-24 12:09:32 +0000
commita6a4e9b2441d1f70d345596528fe39f09ae96874 (patch)
tree516bad5e2ddcfdd08b573d970c2f484dcf0acf07
parentb05e5501a455a86e577e70617ca0f376041568c9 (diff)
Fixed #2674 -- Added stricter date validation so that things like 2006-11-31
are caught. Thanks, Gary Wilson. git-svn-id: http://code.djangoproject.com/svn/django/trunk@3815 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--AUTHORS1
-rw-r--r--django/core/validators.py23
2 files changed, 23 insertions, 1 deletions
diff --git a/AUTHORS b/AUTHORS
index c5d14eb86d..215f396b2a 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -151,6 +151,7 @@ answer newbie questions, and generally made Django that much better:
Milton Waddams
Dan Watson <http://theidioteque.net/>
Rachel Willmer <http://www.willmer.com/kb/>
+ Gary Wilson <gary.wilson@gmail.com>
wojtek
ye7cakf02@sneakemail.com
Cheng Zhang
diff --git a/django/core/validators.py b/django/core/validators.py
index 705425ea18..f2f3f44914 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -13,7 +13,7 @@ from django.utils.translation import gettext, gettext_lazy, ngettext
from django.utils.functional import Promise, lazy
import re
-_datere = r'(19|2\d)\d{2}-((?:0?[1-9])|(?:1[0-2]))-((?:0?[1-9])|(?:[12][0-9])|(?:3[0-1]))'
+_datere = r'\d{4}-\d{1,2}-\d{1,2}'
_timere = r'(?:[01]?[0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?'
alnum_re = re.compile(r'^\w+$')
alnumurl_re = re.compile(r'^[-\w/]+$')
@@ -122,9 +122,29 @@ def isOnlyLetters(field_data, all_data):
if not field_data.isalpha():
raise ValidationError, gettext("Only alphabetical characters are allowed here.")
+def _isValidDate(date_string):
+ """
+ A helper function used by isValidANSIDate and isValidANSIDatetime to
+ check if the date is valid. The date string is assumed to already be in
+ YYYY-MM-DD format.
+ """
+ from datetime import date
+ # Could use time.strptime here and catch errors, but datetime.date below
+ # produces much friendlier error messages.
+ year, month, day = map(int, date_string.split('-'))
+ # This check is needed because strftime is used when saving the date
+ # value to the database, and strftime requires that the year be >=1900.
+ if year < 1900:
+ raise ValidationError, gettext('Year must be 1900 or later.')
+ try:
+ date(year, month, day)
+ except ValueError, e:
+ raise ValidationError, gettext('Invalid date: %s.' % e)
+
def isValidANSIDate(field_data, all_data):
if not ansi_date_re.search(field_data):
raise ValidationError, gettext('Enter a valid date in YYYY-MM-DD format.')
+ _isValidDate(field_data)
def isValidANSITime(field_data, all_data):
if not ansi_time_re.search(field_data):
@@ -133,6 +153,7 @@ def isValidANSITime(field_data, all_data):
def isValidANSIDatetime(field_data, all_data):
if not ansi_datetime_re.search(field_data):
raise ValidationError, gettext('Enter a valid date/time in YYYY-MM-DD HH:MM format.')
+ _isValidDate(field_data.split()[0])
def isValidEmail(field_data, all_data):
if not email_re.search(field_data):