2006-10-29 04:34:37 +08:00
|
|
|
"""
|
2007-10-28 11:38:08 +08:00
|
|
|
Field classes.
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
|
2007-09-14 11:29:39 +08:00
|
|
|
import copy
|
2007-05-17 05:20:35 +08:00
|
|
|
import datetime
|
2008-03-20 06:29:11 +08:00
|
|
|
import os
|
2007-05-17 05:20:35 +08:00
|
|
|
import re
|
|
|
|
import time
|
2007-10-28 11:38:08 +08:00
|
|
|
# Python 2.3 fallbacks
|
|
|
|
try:
|
|
|
|
from decimal import Decimal, DecimalException
|
|
|
|
except ImportError:
|
|
|
|
from django.utils._decimal import Decimal, DecimalException
|
|
|
|
try:
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set
|
2007-05-17 05:20:35 +08:00
|
|
|
|
2007-11-04 10:08:02 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-11-30 03:30:38 +08:00
|
|
|
from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str
|
2007-05-17 05:20:35 +08:00
|
|
|
|
2007-04-04 14:34:19 +08:00
|
|
|
from util import ErrorList, ValidationError
|
2007-10-21 22:50:47 +08:00
|
|
|
from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2007-07-13 17:09:59 +08:00
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
__all__ = (
|
|
|
|
'Field', 'CharField', 'IntegerField',
|
|
|
|
'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
|
2006-12-15 04:35:32 +08:00
|
|
|
'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
|
2006-10-29 04:34:37 +08:00
|
|
|
'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
|
2007-10-28 11:38:08 +08:00
|
|
|
'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField',
|
|
|
|
'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
|
2007-05-21 09:29:58 +08:00
|
|
|
'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
|
2008-03-20 06:29:11 +08:00
|
|
|
'SplitDateTimeField', 'IPAddressField', 'FilePathField',
|
2006-10-29 04:34:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# These values, if given to to_python(), will trigger the self.required check.
|
|
|
|
EMPTY_VALUES = (None, '')
|
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
class Field(object):
|
|
|
|
widget = TextInput # Default widget to use when rendering this type of Field.
|
2007-01-09 13:12:25 +08:00
|
|
|
hidden_widget = HiddenInput # Default widget to use when rendering this as "hidden".
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'required': _(u'This field is required.'),
|
|
|
|
'invalid': _(u'Enter a valid value.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2006-11-24 01:40:33 +08:00
|
|
|
# Tracks each time a Field instance is created. Used to retain order.
|
|
|
|
creation_counter = 0
|
|
|
|
|
2007-10-28 13:40:26 +08:00
|
|
|
def __init__(self, required=True, widget=None, label=None, initial=None,
|
|
|
|
help_text=None, error_messages=None):
|
2006-12-28 08:01:52 +08:00
|
|
|
# required -- Boolean that specifies whether the field is required.
|
|
|
|
# True by default.
|
2007-04-01 13:05:59 +08:00
|
|
|
# widget -- A Widget class, or instance of a Widget class, that should
|
|
|
|
# be used for this Field when displaying it. Each Field has a
|
|
|
|
# default Widget that it'll use if you don't specify this. In
|
|
|
|
# most cases, the default widget is TextInput.
|
|
|
|
# label -- A verbose name for this field, for use in displaying this
|
|
|
|
# field in a form. By default, Django will use a "pretty"
|
|
|
|
# version of the form field name, if the Field is part of a
|
|
|
|
# Form.
|
|
|
|
# initial -- A value to use in this Field's initial display. This value
|
|
|
|
# is *not* used as a fallback if data isn't given.
|
2007-01-29 06:10:04 +08:00
|
|
|
# help_text -- An optional string to use as "help text" for this Field.
|
2006-12-18 03:04:03 +08:00
|
|
|
if label is not None:
|
|
|
|
label = smart_unicode(label)
|
2006-12-28 08:01:52 +08:00
|
|
|
self.required, self.label, self.initial = required, label, initial
|
2007-02-20 07:43:14 +08:00
|
|
|
self.help_text = smart_unicode(help_text or '')
|
2006-10-29 04:34:37 +08:00
|
|
|
widget = widget or self.widget
|
|
|
|
if isinstance(widget, type):
|
|
|
|
widget = widget()
|
2006-12-09 02:54:53 +08:00
|
|
|
|
|
|
|
# Hook into self.widget_attrs() for any Field-specific HTML attributes.
|
|
|
|
extra_attrs = self.widget_attrs(widget)
|
|
|
|
if extra_attrs:
|
|
|
|
widget.attrs.update(extra_attrs)
|
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
self.widget = widget
|
|
|
|
|
2006-11-24 01:40:33 +08:00
|
|
|
# Increase the creation counter, and save our local copy.
|
|
|
|
self.creation_counter = Field.creation_counter
|
|
|
|
Field.creation_counter += 1
|
|
|
|
|
2007-12-02 00:50:48 +08:00
|
|
|
def set_class_error_messages(messages, klass):
|
2007-10-28 13:40:26 +08:00
|
|
|
for base_class in klass.__bases__:
|
2007-12-02 00:50:48 +08:00
|
|
|
set_class_error_messages(messages, base_class)
|
|
|
|
messages.update(getattr(klass, 'default_error_messages', {}))
|
|
|
|
|
|
|
|
messages = {}
|
|
|
|
set_class_error_messages(messages, self.__class__)
|
|
|
|
messages.update(error_messages or {})
|
|
|
|
self.error_messages = messages
|
2007-10-28 13:40:26 +08:00
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
2006-11-05 04:49:59 +08:00
|
|
|
Validates the given value and returns its "cleaned" value as an
|
2006-10-29 04:34:37 +08:00
|
|
|
appropriate Python object.
|
|
|
|
|
|
|
|
Raises ValidationError for any errors.
|
|
|
|
"""
|
|
|
|
if self.required and value in EMPTY_VALUES:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['required'])
|
2006-10-29 04:34:37 +08:00
|
|
|
return value
|
|
|
|
|
2006-12-09 02:54:53 +08:00
|
|
|
def widget_attrs(self, widget):
|
|
|
|
"""
|
|
|
|
Given a Widget instance (*not* a Widget class), returns a dictionary of
|
|
|
|
any HTML attributes that should be added to the Widget, based on this
|
|
|
|
Field.
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
2007-09-14 11:29:39 +08:00
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
result = copy.copy(self)
|
|
|
|
memo[id(self)] = result
|
|
|
|
result.widget = copy.deepcopy(self.widget, memo)
|
|
|
|
return result
|
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
class CharField(Field):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'max_length': _(u'Ensure this value has at most %(max)d characters (it has %(length)d).'),
|
|
|
|
'min_length': _(u'Ensure this value has at least %(min)d characters (it has %(length)d).'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2006-10-29 04:34:37 +08:00
|
|
|
self.max_length, self.min_length = max_length, min_length
|
2007-01-29 06:10:04 +08:00
|
|
|
super(CharField, self).__init__(*args, **kwargs)
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-10-29 04:34:37 +08:00
|
|
|
"Validates max_length and min_length. Returns a Unicode object."
|
2007-01-21 01:25:42 +08:00
|
|
|
super(CharField, self).clean(value)
|
2006-12-16 07:07:41 +08:00
|
|
|
if value in EMPTY_VALUES:
|
2007-01-21 01:31:45 +08:00
|
|
|
return u''
|
2006-11-16 14:45:29 +08:00
|
|
|
value = smart_unicode(value)
|
2007-07-13 22:13:35 +08:00
|
|
|
value_length = len(value)
|
|
|
|
if self.max_length is not None and value_length > self.max_length:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['max_length'] % {'max': self.max_length, 'length': value_length})
|
2007-07-13 22:13:35 +08:00
|
|
|
if self.min_length is not None and value_length < self.min_length:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['min_length'] % {'min': self.min_length, 'length': value_length})
|
2006-10-29 04:34:37 +08:00
|
|
|
return value
|
|
|
|
|
2006-12-09 02:54:53 +08:00
|
|
|
def widget_attrs(self, widget):
|
|
|
|
if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
|
2007-08-05 13:14:46 +08:00
|
|
|
# The HTML attribute is maxlength, not max_length.
|
2006-12-09 02:54:53 +08:00
|
|
|
return {'maxlength': str(self.max_length)}
|
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
class IntegerField(Field):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a whole number.'),
|
|
|
|
'max_value': _(u'Ensure this value is less than or equal to %s.'),
|
|
|
|
'min_value': _(u'Ensure this value is greater than or equal to %s.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, max_value=None, min_value=None, *args, **kwargs):
|
2006-12-16 07:18:47 +08:00
|
|
|
self.max_value, self.min_value = max_value, min_value
|
2007-01-29 06:10:04 +08:00
|
|
|
super(IntegerField, self).__init__(*args, **kwargs)
|
2006-12-16 07:18:47 +08:00
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
Validates that int() can be called on the input. Returns the result
|
2007-01-13 13:08:07 +08:00
|
|
|
of int(). Returns None for empty values.
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
2006-11-05 04:49:59 +08:00
|
|
|
super(IntegerField, self).clean(value)
|
2007-01-21 01:31:45 +08:00
|
|
|
if value in EMPTY_VALUES:
|
2007-01-13 13:08:07 +08:00
|
|
|
return None
|
2006-10-29 04:34:37 +08:00
|
|
|
try:
|
2007-09-15 19:45:19 +08:00
|
|
|
value = int(str(value))
|
2006-10-29 04:34:37 +08:00
|
|
|
except (ValueError, TypeError):
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2006-12-16 07:18:47 +08:00
|
|
|
if self.max_value is not None and value > self.max_value:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['max_value'] % self.max_value)
|
2006-12-16 07:18:47 +08:00
|
|
|
if self.min_value is not None and value < self.min_value:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['min_value'] % self.min_value)
|
2006-12-16 07:18:47 +08:00
|
|
|
return value
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
class FloatField(Field):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a number.'),
|
|
|
|
'max_value': _(u'Ensure this value is less than or equal to %s.'),
|
|
|
|
'min_value': _(u'Ensure this value is greater than or equal to %s.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
def __init__(self, max_value=None, min_value=None, *args, **kwargs):
|
|
|
|
self.max_value, self.min_value = max_value, min_value
|
|
|
|
Field.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates that float() can be called on the input. Returns a float.
|
|
|
|
Returns None for empty values.
|
|
|
|
"""
|
|
|
|
super(FloatField, self).clean(value)
|
|
|
|
if not self.required and value in EMPTY_VALUES:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
value = float(value)
|
|
|
|
except (ValueError, TypeError):
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-05-21 09:29:58 +08:00
|
|
|
if self.max_value is not None and value > self.max_value:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['max_value'] % self.max_value)
|
2007-05-21 09:29:58 +08:00
|
|
|
if self.min_value is not None and value < self.min_value:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['min_value'] % self.min_value)
|
2007-05-21 09:29:58 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
class DecimalField(Field):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a number.'),
|
|
|
|
'max_value': _(u'Ensure this value is less than or equal to %s.'),
|
|
|
|
'min_value': _(u'Ensure this value is greater than or equal to %s.'),
|
|
|
|
'max_digits': _('Ensure that there are no more than %s digits in total.'),
|
|
|
|
'max_decimal_places': _('Ensure that there are no more than %s decimal places.'),
|
|
|
|
'max_whole_digits': _('Ensure that there are no more than %s digits before the decimal point.')
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs):
|
|
|
|
self.max_value, self.min_value = max_value, min_value
|
|
|
|
self.max_digits, self.decimal_places = max_digits, decimal_places
|
|
|
|
Field.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates that the input is a decimal number. Returns a Decimal
|
|
|
|
instance. Returns None for empty values. Ensures that there are no more
|
|
|
|
than max_digits in the number, and no more than decimal_places digits
|
|
|
|
after the decimal point.
|
|
|
|
"""
|
|
|
|
super(DecimalField, self).clean(value)
|
|
|
|
if not self.required and value in EMPTY_VALUES:
|
|
|
|
return None
|
2007-11-30 03:30:38 +08:00
|
|
|
value = smart_str(value).strip()
|
2007-07-13 17:09:59 +08:00
|
|
|
try:
|
2007-05-21 09:29:58 +08:00
|
|
|
value = Decimal(value)
|
2007-07-13 17:09:59 +08:00
|
|
|
except DecimalException:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-09-09 03:24:46 +08:00
|
|
|
pieces = str(value).lstrip("-").split('.')
|
2007-07-13 17:09:59 +08:00
|
|
|
decimals = (len(pieces) == 2) and len(pieces[1]) or 0
|
|
|
|
digits = len(pieces[0])
|
2007-05-21 09:29:58 +08:00
|
|
|
if self.max_value is not None and value > self.max_value:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['max_value'] % self.max_value)
|
2007-05-21 09:29:58 +08:00
|
|
|
if self.min_value is not None and value < self.min_value:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['min_value'] % self.min_value)
|
2007-05-21 09:29:58 +08:00
|
|
|
if self.max_digits is not None and (digits + decimals) > self.max_digits:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
|
2007-05-21 09:29:58 +08:00
|
|
|
if self.decimal_places is not None and decimals > self.decimal_places:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places)
|
2007-05-21 09:29:58 +08:00
|
|
|
if self.max_digits is not None and self.decimal_places is not None and digits > (self.max_digits - self.decimal_places):
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places))
|
2007-05-21 09:29:58 +08:00
|
|
|
return value
|
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
DEFAULT_DATE_INPUT_FORMATS = (
|
|
|
|
'%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'
|
|
|
|
'%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006'
|
|
|
|
'%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006'
|
|
|
|
'%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006'
|
|
|
|
'%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006'
|
|
|
|
)
|
|
|
|
|
|
|
|
class DateField(Field):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a valid date.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, input_formats=None, *args, **kwargs):
|
|
|
|
super(DateField, self).__init__(*args, **kwargs)
|
2006-10-29 04:34:37 +08:00
|
|
|
self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
|
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
Validates that the input can be converted to a date. Returns a Python
|
|
|
|
datetime.date object.
|
|
|
|
"""
|
2007-01-21 01:25:42 +08:00
|
|
|
super(DateField, self).clean(value)
|
2006-10-29 04:34:37 +08:00
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return None
|
|
|
|
if isinstance(value, datetime.datetime):
|
|
|
|
return value.date()
|
|
|
|
if isinstance(value, datetime.date):
|
|
|
|
return value
|
|
|
|
for format in self.input_formats:
|
|
|
|
try:
|
|
|
|
return datetime.date(*time.strptime(value, format)[:3])
|
|
|
|
except ValueError:
|
|
|
|
continue
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2006-12-15 04:35:32 +08:00
|
|
|
DEFAULT_TIME_INPUT_FORMATS = (
|
|
|
|
'%H:%M:%S', # '14:30:59'
|
|
|
|
'%H:%M', # '14:30'
|
|
|
|
)
|
|
|
|
|
|
|
|
class TimeField(Field):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a valid time.')
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, input_formats=None, *args, **kwargs):
|
|
|
|
super(TimeField, self).__init__(*args, **kwargs)
|
2006-12-15 04:35:32 +08:00
|
|
|
self.input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates that the input can be converted to a time. Returns a Python
|
|
|
|
datetime.time object.
|
|
|
|
"""
|
2007-01-21 01:25:42 +08:00
|
|
|
super(TimeField, self).clean(value)
|
2006-12-15 04:35:32 +08:00
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return None
|
|
|
|
if isinstance(value, datetime.time):
|
|
|
|
return value
|
|
|
|
for format in self.input_formats:
|
|
|
|
try:
|
|
|
|
return datetime.time(*time.strptime(value, format)[3:6])
|
|
|
|
except ValueError:
|
|
|
|
continue
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2006-12-15 04:35:32 +08:00
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
DEFAULT_DATETIME_INPUT_FORMATS = (
|
|
|
|
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
|
|
|
|
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
|
|
|
|
'%Y-%m-%d', # '2006-10-25'
|
|
|
|
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
|
|
|
|
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
|
|
|
|
'%m/%d/%Y', # '10/25/2006'
|
|
|
|
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
|
|
|
|
'%m/%d/%y %H:%M', # '10/25/06 14:30'
|
|
|
|
'%m/%d/%y', # '10/25/06'
|
|
|
|
)
|
|
|
|
|
|
|
|
class DateTimeField(Field):
|
2007-10-21 22:50:47 +08:00
|
|
|
widget = DateTimeInput
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a valid date/time.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
2007-10-21 22:50:47 +08:00
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, input_formats=None, *args, **kwargs):
|
|
|
|
super(DateTimeField, self).__init__(*args, **kwargs)
|
2006-10-29 04:34:37 +08:00
|
|
|
self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
|
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
Validates that the input can be converted to a datetime. Returns a
|
|
|
|
Python datetime.datetime object.
|
|
|
|
"""
|
2007-01-21 01:25:42 +08:00
|
|
|
super(DateTimeField, self).clean(value)
|
2006-10-29 04:34:37 +08:00
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
return None
|
|
|
|
if isinstance(value, datetime.datetime):
|
|
|
|
return value
|
|
|
|
if isinstance(value, datetime.date):
|
|
|
|
return datetime.datetime(value.year, value.month, value.day)
|
2007-10-21 22:50:20 +08:00
|
|
|
if isinstance(value, list):
|
|
|
|
# Input comes from a SplitDateTimeWidget, for example. So, it's two
|
|
|
|
# components: date and time.
|
|
|
|
if len(value) != 2:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-10-21 22:50:20 +08:00
|
|
|
value = '%s %s' % tuple(value)
|
2006-10-29 04:34:37 +08:00
|
|
|
for format in self.input_formats:
|
|
|
|
try:
|
|
|
|
return datetime.datetime(*time.strptime(value, format)[:6])
|
|
|
|
except ValueError:
|
|
|
|
continue
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2007-07-13 20:03:20 +08:00
|
|
|
class RegexField(CharField):
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
regex can be either a string or a compiled regular expression object.
|
|
|
|
error_message is an optional error message to use, if
|
|
|
|
'Enter a valid value' is too generic for you.
|
|
|
|
"""
|
2007-10-28 13:40:26 +08:00
|
|
|
# error_message is just kept for backwards compatibility:
|
|
|
|
if error_message:
|
|
|
|
error_messages = kwargs.get('error_messages') or {}
|
|
|
|
error_messages['invalid'] = error_message
|
|
|
|
kwargs['error_messages'] = error_messages
|
2007-07-13 20:03:20 +08:00
|
|
|
super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
|
2006-10-29 04:34:37 +08:00
|
|
|
if isinstance(regex, basestring):
|
|
|
|
regex = re.compile(regex)
|
|
|
|
self.regex = regex
|
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
Validates that the input matches the regular expression. Returns a
|
|
|
|
Unicode object.
|
|
|
|
"""
|
2007-07-13 20:03:20 +08:00
|
|
|
value = super(RegexField, self).clean(value)
|
2007-01-21 01:31:45 +08:00
|
|
|
if value == u'':
|
2006-11-27 08:49:26 +08:00
|
|
|
return value
|
|
|
|
if not self.regex.search(value):
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2006-10-29 04:34:37 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
email_re = re.compile(
|
|
|
|
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
|
|
|
|
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
|
|
|
|
r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
|
|
|
|
|
|
|
|
class EmailField(RegexField):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a valid e-mail address.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2007-10-28 13:40:26 +08:00
|
|
|
RegexField.__init__(self, email_re, max_length, min_length, *args,
|
|
|
|
**kwargs)
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2006-11-07 10:32:33 +08:00
|
|
|
try:
|
|
|
|
from django.conf import settings
|
|
|
|
URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT
|
2007-12-02 23:27:44 +08:00
|
|
|
except ImportError:
|
2006-11-07 10:32:33 +08:00
|
|
|
# It's OK if Django settings aren't configured.
|
|
|
|
URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
|
|
|
|
|
2007-08-06 21:58:56 +08:00
|
|
|
class UploadedFile(StrAndUnicode):
|
|
|
|
"A wrapper for files uploaded in a FileField"
|
|
|
|
def __init__(self, filename, content):
|
|
|
|
self.filename = filename
|
|
|
|
self.content = content
|
2007-09-09 03:26:15 +08:00
|
|
|
|
2007-08-06 21:58:56 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
"""
|
|
|
|
The unicode representation is the filename, so that the pre-database-insertion
|
|
|
|
logic can use UploadedFile objects
|
|
|
|
"""
|
|
|
|
return self.filename
|
|
|
|
|
|
|
|
class FileField(Field):
|
|
|
|
widget = FileInput
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u"No file was submitted. Check the encoding type on the form."),
|
|
|
|
'missing': _(u"No file was submitted."),
|
|
|
|
'empty': _(u"The submitted file is empty."),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-08-06 21:58:56 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(FileField, self).__init__(*args, **kwargs)
|
|
|
|
|
2008-01-18 02:03:21 +08:00
|
|
|
def clean(self, data, initial=None):
|
|
|
|
super(FileField, self).clean(initial or data)
|
2007-08-06 21:58:56 +08:00
|
|
|
if not self.required and data in EMPTY_VALUES:
|
|
|
|
return None
|
2008-01-18 02:03:21 +08:00
|
|
|
elif not data and initial:
|
|
|
|
return initial
|
2007-08-06 21:58:56 +08:00
|
|
|
try:
|
|
|
|
f = UploadedFile(data['filename'], data['content'])
|
|
|
|
except TypeError:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-08-06 21:58:56 +08:00
|
|
|
except KeyError:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['missing'])
|
2007-08-06 21:58:56 +08:00
|
|
|
if not f.content:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['empty'])
|
2007-08-06 21:58:56 +08:00
|
|
|
return f
|
|
|
|
|
|
|
|
class ImageField(FileField):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid_image': _(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image."),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2008-01-18 02:03:21 +08:00
|
|
|
def clean(self, data, initial=None):
|
2007-08-06 21:58:56 +08:00
|
|
|
"""
|
|
|
|
Checks that the file-upload field data contains a valid image (GIF, JPG,
|
|
|
|
PNG, possibly others -- whatever the Python Imaging Library supports).
|
|
|
|
"""
|
2008-01-18 02:03:21 +08:00
|
|
|
f = super(ImageField, self).clean(data, initial)
|
2007-08-06 21:58:56 +08:00
|
|
|
if f is None:
|
|
|
|
return None
|
2008-01-18 23:53:19 +08:00
|
|
|
elif not data and initial:
|
|
|
|
return initial
|
2007-08-06 21:58:56 +08:00
|
|
|
from PIL import Image
|
|
|
|
from cStringIO import StringIO
|
|
|
|
try:
|
2007-09-14 15:18:27 +08:00
|
|
|
# load() is the only method that can spot a truncated JPEG,
|
|
|
|
# but it cannot be called sanely after verify()
|
|
|
|
trial_image = Image.open(StringIO(f.content))
|
|
|
|
trial_image.load()
|
|
|
|
# verify() is the only method that can spot a corrupt PNG,
|
|
|
|
# but it must be called immediately after the constructor
|
|
|
|
trial_image = Image.open(StringIO(f.content))
|
|
|
|
trial_image.verify()
|
|
|
|
except Exception: # Python Imaging Library doesn't recognize it as an image
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid_image'])
|
2007-08-06 21:58:56 +08:00
|
|
|
return f
|
2007-09-09 03:26:15 +08:00
|
|
|
|
2007-09-14 10:11:24 +08:00
|
|
|
url_re = re.compile(
|
|
|
|
r'^https?://' # http:// or https://
|
|
|
|
r'(?:(?:[A-Z0-9-]+\.)+[A-Z]{2,6}|' #domain...
|
|
|
|
r'localhost|' #localhost...
|
|
|
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
|
|
|
r'(?::\d+)?' # optional port
|
|
|
|
r'(?:/?|/\S+)$', re.IGNORECASE)
|
|
|
|
|
2006-11-02 07:52:43 +08:00
|
|
|
class URLField(RegexField):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a valid URL.'),
|
|
|
|
'invalid_link': _(u'This URL appears to be a broken link.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, verify_exists=False,
|
|
|
|
validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
|
2007-10-28 13:40:26 +08:00
|
|
|
super(URLField, self).__init__(url_re, max_length, min_length, *args,
|
|
|
|
**kwargs)
|
2006-11-02 07:52:43 +08:00
|
|
|
self.verify_exists = verify_exists
|
2006-11-07 10:32:33 +08:00
|
|
|
self.user_agent = validator_user_agent
|
2006-11-02 07:52:43 +08:00
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2007-09-14 15:02:55 +08:00
|
|
|
# If no URL scheme given, assume http://
|
|
|
|
if value and '://' not in value:
|
|
|
|
value = u'http://%s' % value
|
2007-01-21 01:25:42 +08:00
|
|
|
value = super(URLField, self).clean(value)
|
2007-01-21 01:31:45 +08:00
|
|
|
if value == u'':
|
2007-01-13 13:19:15 +08:00
|
|
|
return value
|
2006-11-02 07:52:43 +08:00
|
|
|
if self.verify_exists:
|
|
|
|
import urllib2
|
2006-11-07 10:32:33 +08:00
|
|
|
from django.conf import settings
|
|
|
|
headers = {
|
|
|
|
"Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
|
|
|
|
"Accept-Language": "en-us,en;q=0.5",
|
|
|
|
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
|
|
|
|
"Connection": "close",
|
|
|
|
"User-Agent": self.user_agent,
|
|
|
|
}
|
2006-11-02 07:52:43 +08:00
|
|
|
try:
|
2006-11-10 11:39:00 +08:00
|
|
|
req = urllib2.Request(value, None, headers)
|
2006-11-07 10:32:33 +08:00
|
|
|
u = urllib2.urlopen(req)
|
2006-11-02 07:52:43 +08:00
|
|
|
except ValueError:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2006-11-02 07:52:43 +08:00
|
|
|
except: # urllib2.URLError, httplib.InvalidURL, etc.
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid_link'])
|
2006-11-02 07:52:43 +08:00
|
|
|
return value
|
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
class BooleanField(Field):
|
|
|
|
widget = CheckboxInput
|
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2007-11-30 03:22:03 +08:00
|
|
|
"""Returns a Python boolean object."""
|
2007-01-21 01:25:42 +08:00
|
|
|
super(BooleanField, self).clean(value)
|
2007-11-30 03:22:03 +08:00
|
|
|
# Explicitly check for the string 'False', which is what a hidden field
|
2007-12-04 14:02:56 +08:00
|
|
|
# will submit for False. Because bool("True") == True, we don't need to
|
|
|
|
# handle that explicitly.
|
2007-11-30 03:22:03 +08:00
|
|
|
if value == 'False':
|
2007-09-15 22:03:07 +08:00
|
|
|
return False
|
2006-10-29 04:34:37 +08:00
|
|
|
return bool(value)
|
2006-11-02 11:16:12 +08:00
|
|
|
|
2007-01-24 13:23:19 +08:00
|
|
|
class NullBooleanField(BooleanField):
|
|
|
|
"""
|
|
|
|
A field whose valid values are None, True and False. Invalid values are
|
|
|
|
cleaned to None.
|
|
|
|
"""
|
|
|
|
widget = NullBooleanSelect
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
return {True: True, False: False}.get(value, None)
|
|
|
|
|
2006-11-02 11:16:12 +08:00
|
|
|
class ChoiceField(Field):
|
2007-04-21 13:43:32 +08:00
|
|
|
widget = Select
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid_choice': _(u'Select a valid choice. That choice is not one of the available choices.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, choices=(), required=True, widget=None, label=None,
|
|
|
|
initial=None, help_text=None, *args, **kwargs):
|
|
|
|
super(ChoiceField, self).__init__(required, widget, label, initial,
|
|
|
|
help_text, *args, **kwargs)
|
2006-11-02 11:16:12 +08:00
|
|
|
self.choices = choices
|
|
|
|
|
2007-01-21 09:29:01 +08:00
|
|
|
def _get_choices(self):
|
|
|
|
return self._choices
|
|
|
|
|
|
|
|
def _set_choices(self, value):
|
|
|
|
# Setting choices also sets the choices on the widget.
|
2007-02-20 11:05:09 +08:00
|
|
|
# choices can be any iterable, but we call list() on it because
|
|
|
|
# it will be consumed more than once.
|
|
|
|
self._choices = self.widget.choices = list(value)
|
2007-01-21 09:29:01 +08:00
|
|
|
|
|
|
|
choices = property(_get_choices, _set_choices)
|
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-11-02 11:16:12 +08:00
|
|
|
"""
|
|
|
|
Validates that the input is in self.choices.
|
|
|
|
"""
|
2007-01-21 01:25:42 +08:00
|
|
|
value = super(ChoiceField, self).clean(value)
|
2007-01-21 01:31:45 +08:00
|
|
|
if value in EMPTY_VALUES:
|
|
|
|
value = u''
|
2006-11-16 14:45:29 +08:00
|
|
|
value = smart_unicode(value)
|
2007-01-21 01:31:45 +08:00
|
|
|
if value == u'':
|
2006-11-27 08:49:26 +08:00
|
|
|
return value
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
valid_values = set([smart_unicode(k) for k, v in self.choices])
|
2006-11-02 11:16:12 +08:00
|
|
|
if value not in valid_values:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
|
2006-11-02 11:16:12 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
class MultipleChoiceField(ChoiceField):
|
2007-01-09 13:12:25 +08:00
|
|
|
hidden_widget = MultipleHiddenInput
|
2007-04-21 13:43:32 +08:00
|
|
|
widget = SelectMultiple
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid_choice': _(u'Select a valid choice. %(value)s is not one of the available choices.'),
|
|
|
|
'invalid_list': _(u'Enter a list of values.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
2006-11-02 11:16:12 +08:00
|
|
|
|
2006-11-05 04:49:59 +08:00
|
|
|
def clean(self, value):
|
2006-11-02 11:16:12 +08:00
|
|
|
"""
|
|
|
|
Validates that the input is a list or tuple.
|
|
|
|
"""
|
|
|
|
if self.required and not value:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['required'])
|
2006-11-27 08:49:26 +08:00
|
|
|
elif not self.required and not value:
|
|
|
|
return []
|
|
|
|
if not isinstance(value, (list, tuple)):
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid_list'])
|
2007-07-12 21:37:59 +08:00
|
|
|
new_value = [smart_unicode(val) for val in value]
|
2006-11-02 11:16:12 +08:00
|
|
|
# Validate that each value in the value list is in self.choices.
|
2006-12-08 13:54:14 +08:00
|
|
|
valid_values = set([smart_unicode(k) for k, v in self.choices])
|
2006-11-02 11:16:12 +08:00
|
|
|
for val in new_value:
|
|
|
|
if val not in valid_values:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid_choice'] % {'value': val})
|
2006-11-02 11:16:12 +08:00
|
|
|
return new_value
|
2006-11-05 04:49:59 +08:00
|
|
|
|
|
|
|
class ComboField(Field):
|
2007-01-24 04:23:07 +08:00
|
|
|
"""
|
|
|
|
A Field whose clean() method calls multiple Field clean() methods.
|
|
|
|
"""
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, fields=(), *args, **kwargs):
|
|
|
|
super(ComboField, self).__init__(*args, **kwargs)
|
2006-11-27 08:49:26 +08:00
|
|
|
# Set 'required' to False on the individual fields, because the
|
|
|
|
# required validation will be handled by ComboField, not by those
|
|
|
|
# individual fields.
|
|
|
|
for f in fields:
|
|
|
|
f.required = False
|
2006-11-05 04:49:59 +08:00
|
|
|
self.fields = fields
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates the given value against all of self.fields, which is a
|
|
|
|
list of Field instances.
|
|
|
|
"""
|
2007-01-21 01:25:42 +08:00
|
|
|
super(ComboField, self).clean(value)
|
2006-11-05 04:49:59 +08:00
|
|
|
for field in self.fields:
|
|
|
|
value = field.clean(value)
|
|
|
|
return value
|
2007-01-24 04:23:07 +08:00
|
|
|
|
|
|
|
class MultiValueField(Field):
|
|
|
|
"""
|
2007-07-01 12:24:20 +08:00
|
|
|
A Field that aggregates the logic of multiple Fields.
|
2007-09-09 03:26:15 +08:00
|
|
|
|
2007-07-01 12:24:20 +08:00
|
|
|
Its clean() method takes a "decompressed" list of values, which are then
|
|
|
|
cleaned into a single value according to self.fields. Each value in
|
2007-01-24 04:23:07 +08:00
|
|
|
this list is cleaned by the corresponding field -- the first value is
|
|
|
|
cleaned by the first field, the second value is cleaned by the second
|
|
|
|
field, etc. Once all fields are cleaned, the list of clean values is
|
|
|
|
"compressed" into a single value.
|
|
|
|
|
2007-07-01 12:24:20 +08:00
|
|
|
Subclasses should not have to implement clean(). Instead, they must
|
|
|
|
implement compress(), which takes a list of valid values and returns a
|
|
|
|
"compressed" version of those values -- a single value.
|
2007-01-24 04:23:07 +08:00
|
|
|
|
|
|
|
You'll probably want to use this with MultiWidget.
|
|
|
|
"""
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a list of values.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, fields=(), *args, **kwargs):
|
|
|
|
super(MultiValueField, self).__init__(*args, **kwargs)
|
2007-01-24 04:23:07 +08:00
|
|
|
# Set 'required' to False on the individual fields, because the
|
|
|
|
# required validation will be handled by MultiValueField, not by those
|
|
|
|
# individual fields.
|
|
|
|
for f in fields:
|
|
|
|
f.required = False
|
|
|
|
self.fields = fields
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates every value in the given list. A value is validated against
|
|
|
|
the corresponding Field in self.fields.
|
|
|
|
|
|
|
|
For example, if this MultiValueField was instantiated with
|
|
|
|
fields=(DateField(), TimeField()), clean() would call
|
|
|
|
DateField.clean(value[0]) and TimeField.clean(value[1]).
|
|
|
|
"""
|
|
|
|
clean_data = []
|
|
|
|
errors = ErrorList()
|
2007-06-23 11:32:59 +08:00
|
|
|
if not value or isinstance(value, (list, tuple)):
|
|
|
|
if not value or not [v for v in value if v not in EMPTY_VALUES]:
|
|
|
|
if self.required:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['required'])
|
2007-06-23 11:32:59 +08:00
|
|
|
else:
|
|
|
|
return self.compress([])
|
|
|
|
else:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-01-24 04:23:07 +08:00
|
|
|
for i, field in enumerate(self.fields):
|
|
|
|
try:
|
|
|
|
field_value = value[i]
|
2007-04-26 20:46:04 +08:00
|
|
|
except IndexError:
|
2007-01-24 04:23:07 +08:00
|
|
|
field_value = None
|
|
|
|
if self.required and field_value in EMPTY_VALUES:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['required'])
|
2007-01-24 04:23:07 +08:00
|
|
|
try:
|
|
|
|
clean_data.append(field.clean(field_value))
|
|
|
|
except ValidationError, e:
|
|
|
|
# Collect all validation errors in a single list, which we'll
|
|
|
|
# raise at the end of clean(), rather than raising a single
|
|
|
|
# exception for the first error we encounter.
|
|
|
|
errors.extend(e.messages)
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
return self.compress(clean_data)
|
|
|
|
|
|
|
|
def compress(self, data_list):
|
|
|
|
"""
|
|
|
|
Returns a single value for the given list of values. The values can be
|
|
|
|
assumed to be valid.
|
|
|
|
|
|
|
|
For example, if this MultiValueField was instantiated with
|
|
|
|
fields=(DateField(), TimeField()), this might return a datetime
|
|
|
|
object created by combining the date and time in data_list.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError('Subclasses must implement this method.')
|
|
|
|
|
2008-03-20 06:29:11 +08:00
|
|
|
class FilePathField(ChoiceField):
|
|
|
|
def __init__(self, path, match=None, recursive=False, required=True,
|
|
|
|
widget=Select, label=None, initial=None, help_text=None,
|
|
|
|
*args, **kwargs):
|
|
|
|
self.path, self.match, self.recursive = path, match, recursive
|
|
|
|
super(FilePathField, self).__init__(choices=(), required=required,
|
|
|
|
widget=widget, label=label, initial=initial, help_text=help_text,
|
|
|
|
*args, **kwargs)
|
|
|
|
self.choices = []
|
|
|
|
if self.match is not None:
|
|
|
|
self.match_re = re.compile(self.match)
|
|
|
|
if recursive:
|
|
|
|
for root, dirs, files in os.walk(self.path):
|
|
|
|
for f in files:
|
|
|
|
if self.match is None or self.match_re.search(f):
|
|
|
|
f = os.path.join(root, f)
|
|
|
|
self.choices.append((f, f.replace(path, "", 1)))
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
for f in os.listdir(self.path):
|
|
|
|
full_file = os.path.join(self.path, f)
|
|
|
|
if os.path.isfile(full_file) and (self.match is None or self.match_re.search(f)):
|
|
|
|
self.choices.append((full_file, f))
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
self.widget.choices = self.choices
|
|
|
|
|
2007-01-24 04:23:07 +08:00
|
|
|
class SplitDateTimeField(MultiValueField):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid_date': _(u'Enter a valid date.'),
|
|
|
|
'invalid_time': _(u'Enter a valid time.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-01-29 06:10:04 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2007-10-28 13:40:26 +08:00
|
|
|
errors = self.default_error_messages.copy()
|
|
|
|
if 'error_messages' in kwargs:
|
|
|
|
errors.update(kwargs['error_messages'])
|
|
|
|
fields = (
|
|
|
|
DateField(error_messages={'invalid': errors['invalid_date']}),
|
|
|
|
TimeField(error_messages={'invalid': errors['invalid_time']}),
|
|
|
|
)
|
2007-01-29 06:10:04 +08:00
|
|
|
super(SplitDateTimeField, self).__init__(fields, *args, **kwargs)
|
2007-01-24 04:23:07 +08:00
|
|
|
|
|
|
|
def compress(self, data_list):
|
|
|
|
if data_list:
|
2007-06-23 11:32:59 +08:00
|
|
|
# Raise a validation error if time or date is empty
|
|
|
|
# (possible if SplitDateTimeField has required=False).
|
|
|
|
if data_list[0] in EMPTY_VALUES:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid_date'])
|
2007-06-23 11:32:59 +08:00
|
|
|
if data_list[1] in EMPTY_VALUES:
|
2007-10-28 13:40:26 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid_time'])
|
2007-01-24 04:23:07 +08:00
|
|
|
return datetime.datetime.combine(*data_list)
|
|
|
|
return None
|
2007-09-16 19:38:32 +08:00
|
|
|
|
|
|
|
ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
|
|
|
|
|
|
|
|
class IPAddressField(RegexField):
|
2007-10-28 13:40:26 +08:00
|
|
|
default_error_messages = {
|
2007-11-04 10:08:02 +08:00
|
|
|
'invalid': _(u'Enter a valid IPv4 address.'),
|
2007-10-28 13:40:26 +08:00
|
|
|
}
|
|
|
|
|
2007-09-16 19:38:32 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2007-10-28 13:40:26 +08:00
|
|
|
super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs)
|