Skip to content

Commit

Permalink
Fix required Keyword for Date*Validators
Browse files Browse the repository at this point in the history
I tried writing tests, but they are failing, I don't know why:

TypeError: validate_python() takes exactly 3 arguments (2 given)
  • Loading branch information
moschlar committed Jun 13, 2012
1 parent f6d84ac commit 14196d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions tests/test_validation.py
Expand Up @@ -423,6 +423,10 @@ class TestDateValidator(tb.ValidatorTest):
to_python_params = ['01/01/2009', 'asdf']
to_python_expected = [datetime.date(2009, 1, 1), ValidationError]

attrs = [{'required': False}, {'required': True}]
params = ['', '']
expected = [None, ValidationError]

from_python_attrs = [{}, {}]
from_python_params = [datetime.date(2009, 1, 1)]
from_python_expected = ['01/01/2009']
Expand All @@ -443,6 +447,10 @@ class TestDatetimeValidator(tb.ValidatorTest):
to_python_params = ['01/01/2009 01:00', 'asdf']
to_python_expected = [datetime.datetime.strptime('1/1/2009 1:00', '%d/%m/%Y %H:%M'), ValidationError]

attrs = [{'required': False}, {'required': True}]
params = ['', '']
expected = [None, ValidationError]

from_python_attrs = [{}, {}]
from_python_params = [datetime.datetime.strptime('1/1/2009 1:00', '%d/%m/%Y %H:%M')]
from_python_expected = ['01/01/2009 01:00']
Expand Down
11 changes: 9 additions & 2 deletions tw2/core/validation.py
Expand Up @@ -412,12 +412,19 @@ def max_str(self):

def to_python(self, value):
value = super(DateValidator, self).to_python(value)
if not value:
return None
try:
date = time.strptime(value, self.format)
return datetime.date(date.tm_year, date.tm_mon, date.tm_mday)
except ValueError:
raise ValidationError('baddate', self)

def validate_python(self, value, state):
super(DateValidator, self).validate_python(value, state)
if self.required and not value:
raise ValidationError('required', self)

def from_python(self, value):
return value and value.strftime(self.format) or ''

Expand All @@ -434,8 +441,8 @@ class DateTimeValidator(DateValidator):
format = '%d/%m/%Y %H:%M'

def to_python(self, value):
if value is None:
return value
if not value:
return None
try:
return datetime.datetime.strptime(value, self.format)
except ValueError:
Expand Down

0 comments on commit 14196d9

Please sign in to comment.