Fixed #20440 -- Ensured CharField's max_length/min_length are integers

This commit is contained in:
Tome Cvitan 2013-05-18 13:26:07 +02:00 committed by Claude Paroz
parent 0038296135
commit caf56ad174
3 changed files with 22 additions and 2 deletions

View File

@ -164,6 +164,7 @@ answer newbie questions, and generally made Django that much better:
Leah Culver <leah.culver@gmail.com> Leah Culver <leah.culver@gmail.com>
Raúl Cumplido <raulcumplido@gmail.com> Raúl Cumplido <raulcumplido@gmail.com>
flavio.curella@gmail.com flavio.curella@gmail.com
Tome Cvitan <tome@cvitan.com>
John D'Agostino <john.dagostino@gmail.com> John D'Agostino <john.dagostino@gmail.com>
dackze+django@gmail.com dackze+django@gmail.com
Jim Dalton <jim.dalton@gmail.com> Jim Dalton <jim.dalton@gmail.com>

View File

@ -198,14 +198,15 @@ class Field(object):
result.validators = self.validators[:] result.validators = self.validators[:]
return result return result
class CharField(Field): class CharField(Field):
def __init__(self, max_length=None, min_length=None, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
self.max_length, self.min_length = max_length, min_length self.max_length, self.min_length = max_length, min_length
super(CharField, self).__init__(*args, **kwargs) super(CharField, self).__init__(*args, **kwargs)
if min_length is not None: if min_length is not None:
self.validators.append(validators.MinLengthValidator(min_length)) self.validators.append(validators.MinLengthValidator(int(min_length)))
if max_length is not None: if max_length is not None:
self.validators.append(validators.MaxLengthValidator(max_length)) self.validators.append(validators.MaxLengthValidator(int(max_length)))
def to_python(self, value): def to_python(self, value):
"Returns a Unicode object." "Returns a Unicode object."
@ -220,6 +221,7 @@ class CharField(Field):
attrs.update({'maxlength': str(self.max_length)}) attrs.update({'maxlength': str(self.max_length)})
return attrs return attrs
class IntegerField(Field): class IntegerField(Field):
default_error_messages = { default_error_messages = {
'invalid': _('Enter a whole number.'), 'invalid': _('Enter a whole number.'),
@ -444,6 +446,7 @@ class TimeField(BaseTemporalField):
def strptime(self, value, format): def strptime(self, value, format):
return datetime.datetime.strptime(force_str(value), format).time() return datetime.datetime.strptime(force_str(value), format).time()
class DateTimeField(BaseTemporalField): class DateTimeField(BaseTemporalField):
widget = DateTimeInput widget = DateTimeInput
input_formats = formats.get_format_lazy('DATETIME_INPUT_FORMATS') input_formats = formats.get_format_lazy('DATETIME_INPUT_FORMATS')
@ -482,6 +485,7 @@ class DateTimeField(BaseTemporalField):
def strptime(self, value, format): def strptime(self, value, format):
return datetime.datetime.strptime(force_str(value), format) return datetime.datetime.strptime(force_str(value), format)
class RegexField(CharField): class RegexField(CharField):
def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs): def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
""" """
@ -511,6 +515,7 @@ class RegexField(CharField):
regex = property(_get_regex, _set_regex) regex = property(_get_regex, _set_regex)
class EmailField(CharField): class EmailField(CharField):
widget = EmailInput widget = EmailInput
default_validators = [validators.validate_email] default_validators = [validators.validate_email]
@ -519,6 +524,7 @@ class EmailField(CharField):
value = self.to_python(value).strip() value = self.to_python(value).strip()
return super(EmailField, self).clean(value) return super(EmailField, self).clean(value)
class FileField(Field): class FileField(Field):
widget = ClearableFileInput widget = ClearableFileInput
default_error_messages = { default_error_messages = {
@ -626,6 +632,7 @@ class ImageField(FileField):
f.seek(0) f.seek(0)
return f return f
class URLField(CharField): class URLField(CharField):
widget = URLInput widget = URLInput
default_error_messages = { default_error_messages = {
@ -792,6 +799,7 @@ class ChoiceField(Field):
return True return True
return False return False
class TypedChoiceField(ChoiceField): class TypedChoiceField(ChoiceField):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.coerce = kwargs.pop('coerce', lambda val: val) self.coerce = kwargs.pop('coerce', lambda val: val)
@ -903,6 +911,7 @@ class ComboField(Field):
value = field.clean(value) value = field.clean(value)
return value return value
class MultiValueField(Field): class MultiValueField(Field):
""" """
A Field that aggregates the logic of multiple Fields. A Field that aggregates the logic of multiple Fields.
@ -1047,6 +1056,7 @@ class FilePathField(ChoiceField):
self.widget.choices = self.choices self.widget.choices = self.choices
class SplitDateTimeField(MultiValueField): class SplitDateTimeField(MultiValueField):
widget = SplitDateTimeWidget widget = SplitDateTimeWidget
hidden_widget = SplitHiddenDateTimeWidget hidden_widget = SplitHiddenDateTimeWidget

View File

@ -125,6 +125,15 @@ class FieldsTests(SimpleTestCase):
self.assertEqual(f.max_length, None) self.assertEqual(f.max_length, None)
self.assertEqual(f.min_length, 10) self.assertEqual(f.min_length, 10)
def test_charfield_length_not_int(self):
"""
Ensure that setting min_length or max_length to something that is not a
number returns an exception.
"""
self.assertRaises(ValueError, CharField, min_length='a')
self.assertRaises(ValueError, CharField, max_length='a')
self.assertRaises(ValueError, CharField, 'a')
def test_charfield_widget_attrs(self): def test_charfield_widget_attrs(self):
""" """
Ensure that CharField.widget_attrs() always returns a dictionary. Ensure that CharField.widget_attrs() always returns a dictionary.