1 """ |
1 """ |
2 Field classes. |
2 Field classes. |
3 """ |
3 """ |
4 |
4 |
5 import copy |
|
6 import datetime |
5 import datetime |
7 import os |
6 import os |
8 import re |
7 import re |
9 import time |
8 import time |
10 import urlparse |
9 import urlparse |
|
10 import warnings |
|
11 from decimal import Decimal, DecimalException |
11 try: |
12 try: |
12 from cStringIO import StringIO |
13 from cStringIO import StringIO |
13 except ImportError: |
14 except ImportError: |
14 from StringIO import StringIO |
15 from StringIO import StringIO |
15 |
16 |
16 # Python 2.3 fallbacks |
17 from django.core.exceptions import ValidationError |
17 try: |
18 from django.core import validators |
18 from decimal import Decimal, DecimalException |
19 import django.utils.copycompat as copy |
19 except ImportError: |
20 from django.utils import formats |
20 from django.utils._decimal import Decimal, DecimalException |
|
21 try: |
|
22 set |
|
23 except NameError: |
|
24 from sets import Set as set |
|
25 |
|
26 import django.core.exceptions |
|
27 from django.utils.translation import ugettext_lazy as _ |
21 from django.utils.translation import ugettext_lazy as _ |
28 from django.utils.encoding import smart_unicode, smart_str |
22 from django.utils.encoding import smart_unicode, smart_str |
29 |
23 from django.utils.functional import lazy |
30 from util import ErrorList, ValidationError |
24 |
31 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget |
25 # Provide this import for backwards compatibility. |
|
26 from django.core.validators import EMPTY_VALUES |
|
27 |
|
28 from util import ErrorList |
|
29 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, \ |
|
30 FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, \ |
|
31 DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget |
32 |
32 |
33 __all__ = ( |
33 __all__ = ( |
34 'Field', 'CharField', 'IntegerField', |
34 'Field', 'CharField', 'IntegerField', |
35 'DEFAULT_DATE_INPUT_FORMATS', 'DateField', |
35 'DEFAULT_DATE_INPUT_FORMATS', 'DateField', |
36 'DEFAULT_TIME_INPUT_FORMATS', 'TimeField', |
36 'DEFAULT_TIME_INPUT_FORMATS', 'TimeField', |
40 'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', |
40 'ComboField', 'MultiValueField', 'FloatField', 'DecimalField', |
41 'SplitDateTimeField', 'IPAddressField', 'FilePathField', 'SlugField', |
41 'SplitDateTimeField', 'IPAddressField', 'FilePathField', 'SlugField', |
42 'TypedChoiceField' |
42 'TypedChoiceField' |
43 ) |
43 ) |
44 |
44 |
45 # These values, if given to to_python(), will trigger the self.required check. |
45 def en_format(name): |
46 EMPTY_VALUES = (None, '') |
46 """ |
47 |
47 Helper function to stay backward compatible. |
|
48 """ |
|
49 from django.conf.locale.en import formats |
|
50 warnings.warn( |
|
51 "`django.forms.fields.DEFAULT_%s` is deprecated; use `django.utils.formats.get_format('%s')` instead." % (name, name), |
|
52 PendingDeprecationWarning |
|
53 ) |
|
54 return getattr(formats, name) |
|
55 |
|
56 DEFAULT_DATE_INPUT_FORMATS = lazy(lambda: en_format('DATE_INPUT_FORMATS'), tuple, list)() |
|
57 DEFAULT_TIME_INPUT_FORMATS = lazy(lambda: en_format('TIME_INPUT_FORMATS'), tuple, list)() |
|
58 DEFAULT_DATETIME_INPUT_FORMATS = lazy(lambda: en_format('DATETIME_INPUT_FORMATS'), tuple, list)() |
48 |
59 |
49 class Field(object): |
60 class Field(object): |
50 widget = TextInput # Default widget to use when rendering this type of Field. |
61 widget = TextInput # Default widget to use when rendering this type of Field. |
51 hidden_widget = HiddenInput # Default widget to use when rendering this as "hidden". |
62 hidden_widget = HiddenInput # Default widget to use when rendering this as "hidden". |
|
63 default_validators = [] # Default set of validators |
52 default_error_messages = { |
64 default_error_messages = { |
53 'required': _(u'This field is required.'), |
65 'required': _(u'This field is required.'), |
54 'invalid': _(u'Enter a valid value.'), |
66 'invalid': _(u'Enter a valid value.'), |
55 } |
67 } |
56 |
68 |
57 # Tracks each time a Field instance is created. Used to retain order. |
69 # Tracks each time a Field instance is created. Used to retain order. |
58 creation_counter = 0 |
70 creation_counter = 0 |
59 |
71 |
60 def __init__(self, required=True, widget=None, label=None, initial=None, |
72 def __init__(self, required=True, widget=None, label=None, initial=None, |
61 help_text=None, error_messages=None, show_hidden_initial=False): |
73 help_text=None, error_messages=None, show_hidden_initial=False, |
|
74 validators=[], localize=False): |
62 # required -- Boolean that specifies whether the field is required. |
75 # required -- Boolean that specifies whether the field is required. |
63 # True by default. |
76 # True by default. |
64 # widget -- A Widget class, or instance of a Widget class, that should |
77 # widget -- A Widget class, or instance of a Widget class, that should |
65 # be used for this Field when displaying it. Each Field has a |
78 # be used for this Field when displaying it. Each Field has a |
66 # default Widget that it'll use if you don't specify this. In |
79 # default Widget that it'll use if you don't specify this. In |
95 |
115 |
96 # Increase the creation counter, and save our local copy. |
116 # Increase the creation counter, and save our local copy. |
97 self.creation_counter = Field.creation_counter |
117 self.creation_counter = Field.creation_counter |
98 Field.creation_counter += 1 |
118 Field.creation_counter += 1 |
99 |
119 |
100 def set_class_error_messages(messages, klass): |
|
101 for base_class in klass.__bases__: |
|
102 set_class_error_messages(messages, base_class) |
|
103 messages.update(getattr(klass, 'default_error_messages', {})) |
|
104 |
|
105 messages = {} |
120 messages = {} |
106 set_class_error_messages(messages, self.__class__) |
121 for c in reversed(self.__class__.__mro__): |
|
122 messages.update(getattr(c, 'default_error_messages', {})) |
107 messages.update(error_messages or {}) |
123 messages.update(error_messages or {}) |
108 self.error_messages = messages |
124 self.error_messages = messages |
109 |
125 |
|
126 self.validators = self.default_validators + validators |
|
127 |
|
128 def localize_value(self, value): |
|
129 return formats.localize_input(value) |
|
130 |
|
131 def to_python(self, value): |
|
132 return value |
|
133 |
|
134 def validate(self, value): |
|
135 if value in validators.EMPTY_VALUES and self.required: |
|
136 raise ValidationError(self.error_messages['required']) |
|
137 |
|
138 def run_validators(self, value): |
|
139 if value in validators.EMPTY_VALUES: |
|
140 return |
|
141 errors = [] |
|
142 for v in self.validators: |
|
143 try: |
|
144 v(value) |
|
145 except ValidationError, e: |
|
146 if hasattr(e, 'code') and e.code in self.error_messages: |
|
147 message = self.error_messages[e.code] |
|
148 if e.params: |
|
149 message = message % e.params |
|
150 errors.append(message) |
|
151 else: |
|
152 errors.extend(e.messages) |
|
153 if errors: |
|
154 raise ValidationError(errors) |
|
155 |
110 def clean(self, value): |
156 def clean(self, value): |
111 """ |
157 """ |
112 Validates the given value and returns its "cleaned" value as an |
158 Validates the given value and returns its "cleaned" value as an |
113 appropriate Python object. |
159 appropriate Python object. |
114 |
160 |
115 Raises ValidationError for any errors. |
161 Raises ValidationError for any errors. |
116 """ |
162 """ |
117 if self.required and value in EMPTY_VALUES: |
163 value = self.to_python(value) |
118 raise ValidationError(self.error_messages['required']) |
164 self.validate(value) |
|
165 self.run_validators(value) |
119 return value |
166 return value |
120 |
167 |
121 def widget_attrs(self, widget): |
168 def widget_attrs(self, widget): |
122 """ |
169 """ |
123 Given a Widget instance (*not* a Widget class), returns a dictionary of |
170 Given a Widget instance (*not* a Widget class), returns a dictionary of |
131 memo[id(self)] = result |
178 memo[id(self)] = result |
132 result.widget = copy.deepcopy(self.widget, memo) |
179 result.widget = copy.deepcopy(self.widget, memo) |
133 return result |
180 return result |
134 |
181 |
135 class CharField(Field): |
182 class CharField(Field): |
136 default_error_messages = { |
|
137 'max_length': _(u'Ensure this value has at most %(max)d characters (it has %(length)d).'), |
|
138 'min_length': _(u'Ensure this value has at least %(min)d characters (it has %(length)d).'), |
|
139 } |
|
140 |
|
141 def __init__(self, max_length=None, min_length=None, *args, **kwargs): |
183 def __init__(self, max_length=None, min_length=None, *args, **kwargs): |
142 self.max_length, self.min_length = max_length, min_length |
184 self.max_length, self.min_length = max_length, min_length |
143 super(CharField, self).__init__(*args, **kwargs) |
185 super(CharField, self).__init__(*args, **kwargs) |
144 |
186 if min_length is not None: |
145 def clean(self, value): |
187 self.validators.append(validators.MinLengthValidator(min_length)) |
146 "Validates max_length and min_length. Returns a Unicode object." |
188 if max_length is not None: |
147 super(CharField, self).clean(value) |
189 self.validators.append(validators.MaxLengthValidator(max_length)) |
148 if value in EMPTY_VALUES: |
190 |
|
191 def to_python(self, value): |
|
192 "Returns a Unicode object." |
|
193 if value in validators.EMPTY_VALUES: |
149 return u'' |
194 return u'' |
150 value = smart_unicode(value) |
195 return smart_unicode(value) |
151 value_length = len(value) |
|
152 if self.max_length is not None and value_length > self.max_length: |
|
153 raise ValidationError(self.error_messages['max_length'] % {'max': self.max_length, 'length': value_length}) |
|
154 if self.min_length is not None and value_length < self.min_length: |
|
155 raise ValidationError(self.error_messages['min_length'] % {'min': self.min_length, 'length': value_length}) |
|
156 return value |
|
157 |
196 |
158 def widget_attrs(self, widget): |
197 def widget_attrs(self, widget): |
159 if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): |
198 if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): |
160 # The HTML attribute is maxlength, not max_length. |
199 # The HTML attribute is maxlength, not max_length. |
161 return {'maxlength': str(self.max_length)} |
200 return {'maxlength': str(self.max_length)} |
162 |
201 |
163 class IntegerField(Field): |
202 class IntegerField(Field): |
164 default_error_messages = { |
203 default_error_messages = { |
165 'invalid': _(u'Enter a whole number.'), |
204 'invalid': _(u'Enter a whole number.'), |
166 'max_value': _(u'Ensure this value is less than or equal to %s.'), |
205 'max_value': _(u'Ensure this value is less than or equal to %(limit_value)s.'), |
167 'min_value': _(u'Ensure this value is greater than or equal to %s.'), |
206 'min_value': _(u'Ensure this value is greater than or equal to %(limit_value)s.'), |
168 } |
207 } |
169 |
208 |
170 def __init__(self, max_value=None, min_value=None, *args, **kwargs): |
209 def __init__(self, max_value=None, min_value=None, *args, **kwargs): |
171 self.max_value, self.min_value = max_value, min_value |
|
172 super(IntegerField, self).__init__(*args, **kwargs) |
210 super(IntegerField, self).__init__(*args, **kwargs) |
173 |
211 |
174 def clean(self, value): |
212 if max_value is not None: |
|
213 self.validators.append(validators.MaxValueValidator(max_value)) |
|
214 if min_value is not None: |
|
215 self.validators.append(validators.MinValueValidator(min_value)) |
|
216 |
|
217 def to_python(self, value): |
175 """ |
218 """ |
176 Validates that int() can be called on the input. Returns the result |
219 Validates that int() can be called on the input. Returns the result |
177 of int(). Returns None for empty values. |
220 of int(). Returns None for empty values. |
178 """ |
221 """ |
179 super(IntegerField, self).clean(value) |
222 value = super(IntegerField, self).to_python(value) |
180 if value in EMPTY_VALUES: |
223 if value in validators.EMPTY_VALUES: |
181 return None |
224 return None |
|
225 if self.localize: |
|
226 value = formats.sanitize_separators(value) |
182 try: |
227 try: |
183 value = int(str(value)) |
228 value = int(str(value)) |
184 except (ValueError, TypeError): |
229 except (ValueError, TypeError): |
185 raise ValidationError(self.error_messages['invalid']) |
230 raise ValidationError(self.error_messages['invalid']) |
186 if self.max_value is not None and value > self.max_value: |
|
187 raise ValidationError(self.error_messages['max_value'] % self.max_value) |
|
188 if self.min_value is not None and value < self.min_value: |
|
189 raise ValidationError(self.error_messages['min_value'] % self.min_value) |
|
190 return value |
231 return value |
191 |
232 |
192 class FloatField(Field): |
233 class FloatField(IntegerField): |
193 default_error_messages = { |
234 default_error_messages = { |
194 'invalid': _(u'Enter a number.'), |
235 'invalid': _(u'Enter a number.'), |
195 'max_value': _(u'Ensure this value is less than or equal to %s.'), |
236 } |
196 'min_value': _(u'Ensure this value is greater than or equal to %s.'), |
237 |
197 } |
238 def to_python(self, value): |
198 |
239 """ |
199 def __init__(self, max_value=None, min_value=None, *args, **kwargs): |
240 Validates that float() can be called on the input. Returns the result |
200 self.max_value, self.min_value = max_value, min_value |
241 of float(). Returns None for empty values. |
201 Field.__init__(self, *args, **kwargs) |
242 """ |
202 |
243 value = super(IntegerField, self).to_python(value) |
203 def clean(self, value): |
244 if value in validators.EMPTY_VALUES: |
204 """ |
|
205 Validates that float() can be called on the input. Returns a float. |
|
206 Returns None for empty values. |
|
207 """ |
|
208 super(FloatField, self).clean(value) |
|
209 if not self.required and value in EMPTY_VALUES: |
|
210 return None |
245 return None |
|
246 if self.localize: |
|
247 value = formats.sanitize_separators(value) |
211 try: |
248 try: |
212 value = float(value) |
249 value = float(value) |
213 except (ValueError, TypeError): |
250 except (ValueError, TypeError): |
214 raise ValidationError(self.error_messages['invalid']) |
251 raise ValidationError(self.error_messages['invalid']) |
215 if self.max_value is not None and value > self.max_value: |
|
216 raise ValidationError(self.error_messages['max_value'] % self.max_value) |
|
217 if self.min_value is not None and value < self.min_value: |
|
218 raise ValidationError(self.error_messages['min_value'] % self.min_value) |
|
219 return value |
252 return value |
220 |
253 |
221 class DecimalField(Field): |
254 class DecimalField(Field): |
222 default_error_messages = { |
255 default_error_messages = { |
223 'invalid': _(u'Enter a number.'), |
256 'invalid': _(u'Enter a number.'), |
224 'max_value': _(u'Ensure this value is less than or equal to %s.'), |
257 'max_value': _(u'Ensure this value is less than or equal to %(limit_value)s.'), |
225 'min_value': _(u'Ensure this value is greater than or equal to %s.'), |
258 'min_value': _(u'Ensure this value is greater than or equal to %(limit_value)s.'), |
226 'max_digits': _('Ensure that there are no more than %s digits in total.'), |
259 'max_digits': _('Ensure that there are no more than %s digits in total.'), |
227 'max_decimal_places': _('Ensure that there are no more than %s decimal places.'), |
260 'max_decimal_places': _('Ensure that there are no more than %s decimal places.'), |
228 'max_whole_digits': _('Ensure that there are no more than %s digits before the decimal point.') |
261 'max_whole_digits': _('Ensure that there are no more than %s digits before the decimal point.') |
229 } |
262 } |
230 |
263 |
231 def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): |
264 def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): |
232 self.max_value, self.min_value = max_value, min_value |
|
233 self.max_digits, self.decimal_places = max_digits, decimal_places |
265 self.max_digits, self.decimal_places = max_digits, decimal_places |
234 Field.__init__(self, *args, **kwargs) |
266 Field.__init__(self, *args, **kwargs) |
235 |
267 |
236 def clean(self, value): |
268 if max_value is not None: |
|
269 self.validators.append(validators.MaxValueValidator(max_value)) |
|
270 if min_value is not None: |
|
271 self.validators.append(validators.MinValueValidator(min_value)) |
|
272 |
|
273 def to_python(self, value): |
237 """ |
274 """ |
238 Validates that the input is a decimal number. Returns a Decimal |
275 Validates that the input is a decimal number. Returns a Decimal |
239 instance. Returns None for empty values. Ensures that there are no more |
276 instance. Returns None for empty values. Ensures that there are no more |
240 than max_digits in the number, and no more than decimal_places digits |
277 than max_digits in the number, and no more than decimal_places digits |
241 after the decimal point. |
278 after the decimal point. |
242 """ |
279 """ |
243 super(DecimalField, self).clean(value) |
280 if value in validators.EMPTY_VALUES: |
244 if not self.required and value in EMPTY_VALUES: |
|
245 return None |
281 return None |
|
282 if self.localize: |
|
283 value = formats.sanitize_separators(value) |
246 value = smart_str(value).strip() |
284 value = smart_str(value).strip() |
247 try: |
285 try: |
248 value = Decimal(value) |
286 value = Decimal(value) |
249 except DecimalException: |
287 except DecimalException: |
250 raise ValidationError(self.error_messages['invalid']) |
288 raise ValidationError(self.error_messages['invalid']) |
251 |
289 return value |
|
290 |
|
291 def validate(self, value): |
|
292 super(DecimalField, self).validate(value) |
|
293 if value in validators.EMPTY_VALUES: |
|
294 return |
|
295 # Check for NaN, Inf and -Inf values. We can't compare directly for NaN, |
|
296 # since it is never equal to itself. However, NaN is the only value that |
|
297 # isn't equal to itself, so we can use this to identify NaN |
|
298 if value != value or value == Decimal("Inf") or value == Decimal("-Inf"): |
|
299 raise ValidationError(self.error_messages['invalid']) |
252 sign, digittuple, exponent = value.as_tuple() |
300 sign, digittuple, exponent = value.as_tuple() |
253 decimals = abs(exponent) |
301 decimals = abs(exponent) |
254 # digittuple doesn't include any leading zeros. |
302 # digittuple doesn't include any leading zeros. |
255 digits = len(digittuple) |
303 digits = len(digittuple) |
256 if decimals > digits: |
304 if decimals > digits: |
259 # 0 before the decimal point as a digit since that would mean |
307 # 0 before the decimal point as a digit since that would mean |
260 # we would not allow max_digits = decimal_places. |
308 # we would not allow max_digits = decimal_places. |
261 digits = decimals |
309 digits = decimals |
262 whole_digits = digits - decimals |
310 whole_digits = digits - decimals |
263 |
311 |
264 if self.max_value is not None and value > self.max_value: |
|
265 raise ValidationError(self.error_messages['max_value'] % self.max_value) |
|
266 if self.min_value is not None and value < self.min_value: |
|
267 raise ValidationError(self.error_messages['min_value'] % self.min_value) |
|
268 if self.max_digits is not None and digits > self.max_digits: |
312 if self.max_digits is not None and digits > self.max_digits: |
269 raise ValidationError(self.error_messages['max_digits'] % self.max_digits) |
313 raise ValidationError(self.error_messages['max_digits'] % self.max_digits) |
270 if self.decimal_places is not None and decimals > self.decimal_places: |
314 if self.decimal_places is not None and decimals > self.decimal_places: |
271 raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places) |
315 raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places) |
272 if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places): |
316 if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places): |
273 raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places)) |
317 raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places)) |
274 return value |
318 return value |
275 |
319 |
276 DEFAULT_DATE_INPUT_FORMATS = ( |
|
277 '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06' |
|
278 '%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006' |
|
279 '%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006' |
|
280 '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006' |
|
281 '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006' |
|
282 ) |
|
283 |
|
284 class DateField(Field): |
320 class DateField(Field): |
285 widget = DateInput |
321 widget = DateInput |
286 default_error_messages = { |
322 default_error_messages = { |
287 'invalid': _(u'Enter a valid date.'), |
323 'invalid': _(u'Enter a valid date.'), |
288 } |
324 } |
289 |
325 |
290 def __init__(self, input_formats=None, *args, **kwargs): |
326 def __init__(self, input_formats=None, *args, **kwargs): |
291 super(DateField, self).__init__(*args, **kwargs) |
327 super(DateField, self).__init__(*args, **kwargs) |
292 self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS |
328 self.input_formats = input_formats |
293 |
329 |
294 def clean(self, value): |
330 def to_python(self, value): |
295 """ |
331 """ |
296 Validates that the input can be converted to a date. Returns a Python |
332 Validates that the input can be converted to a date. Returns a Python |
297 datetime.date object. |
333 datetime.date object. |
298 """ |
334 """ |
299 super(DateField, self).clean(value) |
335 if value in validators.EMPTY_VALUES: |
300 if value in EMPTY_VALUES: |
|
301 return None |
336 return None |
302 if isinstance(value, datetime.datetime): |
337 if isinstance(value, datetime.datetime): |
303 return value.date() |
338 return value.date() |
304 if isinstance(value, datetime.date): |
339 if isinstance(value, datetime.date): |
305 return value |
340 return value |
306 for format in self.input_formats: |
341 for format in self.input_formats or formats.get_format('DATE_INPUT_FORMATS'): |
307 try: |
342 try: |
308 return datetime.date(*time.strptime(value, format)[:3]) |
343 return datetime.date(*time.strptime(value, format)[:3]) |
309 except ValueError: |
344 except ValueError: |
310 continue |
345 continue |
311 raise ValidationError(self.error_messages['invalid']) |
346 raise ValidationError(self.error_messages['invalid']) |
312 |
347 |
313 DEFAULT_TIME_INPUT_FORMATS = ( |
|
314 '%H:%M:%S', # '14:30:59' |
|
315 '%H:%M', # '14:30' |
|
316 ) |
|
317 |
|
318 class TimeField(Field): |
348 class TimeField(Field): |
319 widget = TimeInput |
349 widget = TimeInput |
320 default_error_messages = { |
350 default_error_messages = { |
321 'invalid': _(u'Enter a valid time.') |
351 'invalid': _(u'Enter a valid time.') |
322 } |
352 } |
323 |
353 |
324 def __init__(self, input_formats=None, *args, **kwargs): |
354 def __init__(self, input_formats=None, *args, **kwargs): |
325 super(TimeField, self).__init__(*args, **kwargs) |
355 super(TimeField, self).__init__(*args, **kwargs) |
326 self.input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS |
356 self.input_formats = input_formats |
327 |
357 |
328 def clean(self, value): |
358 def to_python(self, value): |
329 """ |
359 """ |
330 Validates that the input can be converted to a time. Returns a Python |
360 Validates that the input can be converted to a time. Returns a Python |
331 datetime.time object. |
361 datetime.time object. |
332 """ |
362 """ |
333 super(TimeField, self).clean(value) |
363 if value in validators.EMPTY_VALUES: |
334 if value in EMPTY_VALUES: |
|
335 return None |
364 return None |
336 if isinstance(value, datetime.time): |
365 if isinstance(value, datetime.time): |
337 return value |
366 return value |
338 for format in self.input_formats: |
367 for format in self.input_formats or formats.get_format('TIME_INPUT_FORMATS'): |
339 try: |
368 try: |
340 return datetime.time(*time.strptime(value, format)[3:6]) |
369 return datetime.time(*time.strptime(value, format)[3:6]) |
341 except ValueError: |
370 except ValueError: |
342 continue |
371 continue |
343 raise ValidationError(self.error_messages['invalid']) |
372 raise ValidationError(self.error_messages['invalid']) |
344 |
373 |
345 DEFAULT_DATETIME_INPUT_FORMATS = ( |
|
346 '%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' |
|
347 '%Y-%m-%d %H:%M', # '2006-10-25 14:30' |
|
348 '%Y-%m-%d', # '2006-10-25' |
|
349 '%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59' |
|
350 '%m/%d/%Y %H:%M', # '10/25/2006 14:30' |
|
351 '%m/%d/%Y', # '10/25/2006' |
|
352 '%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59' |
|
353 '%m/%d/%y %H:%M', # '10/25/06 14:30' |
|
354 '%m/%d/%y', # '10/25/06' |
|
355 ) |
|
356 |
|
357 class DateTimeField(Field): |
374 class DateTimeField(Field): |
358 widget = DateTimeInput |
375 widget = DateTimeInput |
359 default_error_messages = { |
376 default_error_messages = { |
360 'invalid': _(u'Enter a valid date/time.'), |
377 'invalid': _(u'Enter a valid date/time.'), |
361 } |
378 } |
362 |
379 |
363 def __init__(self, input_formats=None, *args, **kwargs): |
380 def __init__(self, input_formats=None, *args, **kwargs): |
364 super(DateTimeField, self).__init__(*args, **kwargs) |
381 super(DateTimeField, self).__init__(*args, **kwargs) |
365 self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS |
382 self.input_formats = input_formats |
366 |
383 |
367 def clean(self, value): |
384 def to_python(self, value): |
368 """ |
385 """ |
369 Validates that the input can be converted to a datetime. Returns a |
386 Validates that the input can be converted to a datetime. Returns a |
370 Python datetime.datetime object. |
387 Python datetime.datetime object. |
371 """ |
388 """ |
372 super(DateTimeField, self).clean(value) |
389 if value in validators.EMPTY_VALUES: |
373 if value in EMPTY_VALUES: |
|
374 return None |
390 return None |
375 if isinstance(value, datetime.datetime): |
391 if isinstance(value, datetime.datetime): |
376 return value |
392 return value |
377 if isinstance(value, datetime.date): |
393 if isinstance(value, datetime.date): |
378 return datetime.datetime(value.year, value.month, value.day) |
394 return datetime.datetime(value.year, value.month, value.day) |
403 kwargs['error_messages'] = error_messages |
419 kwargs['error_messages'] = error_messages |
404 super(RegexField, self).__init__(max_length, min_length, *args, **kwargs) |
420 super(RegexField, self).__init__(max_length, min_length, *args, **kwargs) |
405 if isinstance(regex, basestring): |
421 if isinstance(regex, basestring): |
406 regex = re.compile(regex) |
422 regex = re.compile(regex) |
407 self.regex = regex |
423 self.regex = regex |
408 |
424 self.validators.append(validators.RegexValidator(regex=regex)) |
409 def clean(self, value): |
425 |
410 """ |
426 class EmailField(CharField): |
411 Validates that the input matches the regular expression. Returns a |
|
412 Unicode object. |
|
413 """ |
|
414 value = super(RegexField, self).clean(value) |
|
415 if value == u'': |
|
416 return value |
|
417 if not self.regex.search(value): |
|
418 raise ValidationError(self.error_messages['invalid']) |
|
419 return value |
|
420 |
|
421 email_re = re.compile( |
|
422 r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom |
|
423 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string |
|
424 r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE) # domain |
|
425 |
|
426 class EmailField(RegexField): |
|
427 default_error_messages = { |
427 default_error_messages = { |
428 'invalid': _(u'Enter a valid e-mail address.'), |
428 'invalid': _(u'Enter a valid e-mail address.'), |
429 } |
429 } |
430 |
430 default_validators = [validators.validate_email] |
431 def __init__(self, max_length=None, min_length=None, *args, **kwargs): |
|
432 RegexField.__init__(self, email_re, max_length, min_length, *args, |
|
433 **kwargs) |
|
434 |
|
435 try: |
|
436 from django.conf import settings |
|
437 URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT |
|
438 except ImportError: |
|
439 # It's OK if Django settings aren't configured. |
|
440 URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)' |
|
441 |
|
442 |
431 |
443 class FileField(Field): |
432 class FileField(Field): |
444 widget = FileInput |
433 widget = FileInput |
445 default_error_messages = { |
434 default_error_messages = { |
446 'invalid': _(u"No file was submitted. Check the encoding type on the form."), |
435 'invalid': _(u"No file was submitted. Check the encoding type on the form."), |
528 raise ValidationError(self.error_messages['invalid_image']) |
522 raise ValidationError(self.error_messages['invalid_image']) |
529 if hasattr(f, 'seek') and callable(f.seek): |
523 if hasattr(f, 'seek') and callable(f.seek): |
530 f.seek(0) |
524 f.seek(0) |
531 return f |
525 return f |
532 |
526 |
533 url_re = re.compile( |
527 class URLField(CharField): |
534 r'^https?://' # http:// or https:// |
|
535 r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain... |
|
536 r'localhost|' #localhost... |
|
537 r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip |
|
538 r'(?::\d+)?' # optional port |
|
539 r'(?:/?|/\S+)$', re.IGNORECASE) |
|
540 |
|
541 class URLField(RegexField): |
|
542 default_error_messages = { |
528 default_error_messages = { |
543 'invalid': _(u'Enter a valid URL.'), |
529 'invalid': _(u'Enter a valid URL.'), |
544 'invalid_link': _(u'This URL appears to be a broken link.'), |
530 'invalid_link': _(u'This URL appears to be a broken link.'), |
545 } |
531 } |
546 |
532 |
547 def __init__(self, max_length=None, min_length=None, verify_exists=False, |
533 def __init__(self, max_length=None, min_length=None, verify_exists=False, |
548 validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs): |
534 validator_user_agent=validators.URL_VALIDATOR_USER_AGENT, *args, **kwargs): |
549 super(URLField, self).__init__(url_re, max_length, min_length, *args, |
535 super(URLField, self).__init__(max_length, min_length, *args, |
550 **kwargs) |
536 **kwargs) |
551 self.verify_exists = verify_exists |
537 self.validators.append(validators.URLValidator(verify_exists=verify_exists, validator_user_agent=validator_user_agent)) |
552 self.user_agent = validator_user_agent |
538 |
553 |
539 def to_python(self, value): |
554 def clean(self, value): |
540 if value: |
555 # If no URL scheme given, assume http:// |
541 if '://' not in value: |
556 if value and '://' not in value: |
542 # If no URL scheme given, assume http:// |
557 value = u'http://%s' % value |
543 value = u'http://%s' % value |
558 # If no URL path given, assume / |
544 url_fields = list(urlparse.urlsplit(value)) |
559 if value and not urlparse.urlsplit(value)[2]: |
545 if not url_fields[2]: |
560 value += '/' |
546 # the path portion may need to be added before query params |
561 value = super(URLField, self).clean(value) |
547 url_fields[2] = '/' |
562 if value == u'': |
548 value = urlparse.urlunsplit(url_fields) |
563 return value |
549 return super(URLField, self).to_python(value) |
564 if self.verify_exists: |
|
565 import urllib2 |
|
566 headers = { |
|
567 "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", |
|
568 "Accept-Language": "en-us,en;q=0.5", |
|
569 "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7", |
|
570 "Connection": "close", |
|
571 "User-Agent": self.user_agent, |
|
572 } |
|
573 try: |
|
574 req = urllib2.Request(value, None, headers) |
|
575 u = urllib2.urlopen(req) |
|
576 except ValueError: |
|
577 raise ValidationError(self.error_messages['invalid']) |
|
578 except: # urllib2.URLError, httplib.InvalidURL, etc. |
|
579 raise ValidationError(self.error_messages['invalid_link']) |
|
580 return value |
|
581 |
550 |
582 class BooleanField(Field): |
551 class BooleanField(Field): |
583 widget = CheckboxInput |
552 widget = CheckboxInput |
584 |
553 |
585 def clean(self, value): |
554 def to_python(self, value): |
586 """Returns a Python boolean object.""" |
555 """Returns a Python boolean object.""" |
587 # Explicitly check for the string 'False', which is what a hidden field |
556 # Explicitly check for the string 'False', which is what a hidden field |
588 # will submit for False. Also check for '0', since this is what |
557 # will submit for False. Also check for '0', since this is what |
589 # RadioSelect will provide. Because bool("True") == bool('1') == True, |
558 # RadioSelect will provide. Because bool("True") == bool('1') == True, |
590 # we don't need to handle that explicitly. |
559 # we don't need to handle that explicitly. |
591 if value in ('False', '0'): |
560 if value in ('False', '0'): |
592 value = False |
561 value = False |
593 else: |
562 else: |
594 value = bool(value) |
563 value = bool(value) |
595 super(BooleanField, self).clean(value) |
564 value = super(BooleanField, self).to_python(value) |
596 if not value and self.required: |
565 if not value and self.required: |
597 raise ValidationError(self.error_messages['required']) |
566 raise ValidationError(self.error_messages['required']) |
598 return value |
567 return value |
599 |
568 |
600 class NullBooleanField(BooleanField): |
569 class NullBooleanField(BooleanField): |
672 def __init__(self, *args, **kwargs): |
644 def __init__(self, *args, **kwargs): |
673 self.coerce = kwargs.pop('coerce', lambda val: val) |
645 self.coerce = kwargs.pop('coerce', lambda val: val) |
674 self.empty_value = kwargs.pop('empty_value', '') |
646 self.empty_value = kwargs.pop('empty_value', '') |
675 super(TypedChoiceField, self).__init__(*args, **kwargs) |
647 super(TypedChoiceField, self).__init__(*args, **kwargs) |
676 |
648 |
677 def clean(self, value): |
649 def to_python(self, value): |
678 """ |
650 """ |
679 Validate that the value is in self.choices and can be coerced to the |
651 Validate that the value is in self.choices and can be coerced to the |
680 right type. |
652 right type. |
681 """ |
653 """ |
682 value = super(TypedChoiceField, self).clean(value) |
654 value = super(TypedChoiceField, self).to_python(value) |
683 if value == self.empty_value or value in EMPTY_VALUES: |
655 super(TypedChoiceField, self).validate(value) |
|
656 if value == self.empty_value or value in validators.EMPTY_VALUES: |
684 return self.empty_value |
657 return self.empty_value |
685 |
|
686 # Hack alert: This field is purpose-made to use with Field.to_python as |
|
687 # a coercion function so that ModelForms with choices work. However, |
|
688 # Django's Field.to_python raises |
|
689 # django.core.exceptions.ValidationError, which is a *different* |
|
690 # exception than django.forms.util.ValidationError. So we need to catch |
|
691 # both. |
|
692 try: |
658 try: |
693 value = self.coerce(value) |
659 value = self.coerce(value) |
694 except (ValueError, TypeError, django.core.exceptions.ValidationError): |
660 except (ValueError, TypeError, ValidationError): |
695 raise ValidationError(self.error_messages['invalid_choice'] % {'value': value}) |
661 raise ValidationError(self.error_messages['invalid_choice'] % {'value': value}) |
696 return value |
662 return value |
|
663 |
|
664 def validate(self, value): |
|
665 pass |
697 |
666 |
698 class MultipleChoiceField(ChoiceField): |
667 class MultipleChoiceField(ChoiceField): |
699 hidden_widget = MultipleHiddenInput |
668 hidden_widget = MultipleHiddenInput |
700 widget = SelectMultiple |
669 widget = SelectMultiple |
701 default_error_messages = { |
670 default_error_messages = { |
702 'invalid_choice': _(u'Select a valid choice. %(value)s is not one of the available choices.'), |
671 'invalid_choice': _(u'Select a valid choice. %(value)s is not one of the available choices.'), |
703 'invalid_list': _(u'Enter a list of values.'), |
672 'invalid_list': _(u'Enter a list of values.'), |
704 } |
673 } |
705 |
674 |
706 def clean(self, value): |
675 def to_python(self, value): |
|
676 if not value: |
|
677 return [] |
|
678 elif not isinstance(value, (list, tuple)): |
|
679 raise ValidationError(self.error_messages['invalid_list']) |
|
680 return [smart_unicode(val) for val in value] |
|
681 |
|
682 def validate(self, value): |
707 """ |
683 """ |
708 Validates that the input is a list or tuple. |
684 Validates that the input is a list or tuple. |
709 """ |
685 """ |
710 if self.required and not value: |
686 if self.required and not value: |
711 raise ValidationError(self.error_messages['required']) |
687 raise ValidationError(self.error_messages['required']) |
712 elif not self.required and not value: |
|
713 return [] |
|
714 if not isinstance(value, (list, tuple)): |
|
715 raise ValidationError(self.error_messages['invalid_list']) |
|
716 new_value = [smart_unicode(val) for val in value] |
|
717 # Validate that each value in the value list is in self.choices. |
688 # Validate that each value in the value list is in self.choices. |
718 for val in new_value: |
689 for val in value: |
719 if not self.valid_value(val): |
690 if not self.valid_value(val): |
720 raise ValidationError(self.error_messages['invalid_choice'] % {'value': val}) |
691 raise ValidationError(self.error_messages['invalid_choice'] % {'value': val}) |
721 return new_value |
|
722 |
692 |
723 class ComboField(Field): |
693 class ComboField(Field): |
724 """ |
694 """ |
725 A Field whose clean() method calls multiple Field clean() methods. |
695 A Field whose clean() method calls multiple Field clean() methods. |
726 """ |
696 """ |