|
1 import datetime |
|
2 import decimal |
|
3 import re |
|
4 import time |
|
5 import math |
|
6 from itertools import tee |
|
7 |
|
8 import django.utils.copycompat as copy |
|
9 |
|
10 from django.db import connection |
|
11 from django.db.models.fields.subclassing import LegacyConnection |
|
12 from django.db.models.query_utils import QueryWrapper |
|
13 from django.conf import settings |
|
14 from django import forms |
|
15 from django.core import exceptions, validators |
|
16 from django.utils.datastructures import DictWrapper |
|
17 from django.utils.functional import curry |
|
18 from django.utils.text import capfirst |
|
19 from django.utils.translation import ugettext_lazy as _ |
|
20 from django.utils.encoding import smart_unicode, force_unicode, smart_str |
|
21 from django.utils import datetime_safe |
|
22 |
|
23 class NOT_PROVIDED: |
|
24 pass |
|
25 |
|
26 # The values to use for "blank" in SelectFields. Will be appended to the start of most "choices" lists. |
|
27 BLANK_CHOICE_DASH = [("", "---------")] |
|
28 BLANK_CHOICE_NONE = [("", "None")] |
|
29 |
|
30 class FieldDoesNotExist(Exception): |
|
31 pass |
|
32 |
|
33 # A guide to Field parameters: |
|
34 # |
|
35 # * name: The name of the field specifed in the model. |
|
36 # * attname: The attribute to use on the model object. This is the same as |
|
37 # "name", except in the case of ForeignKeys, where "_id" is |
|
38 # appended. |
|
39 # * db_column: The db_column specified in the model (or None). |
|
40 # * column: The database column for this field. This is the same as |
|
41 # "attname", except if db_column is specified. |
|
42 # |
|
43 # Code that introspects values, or does other dynamic things, should use |
|
44 # attname. For example, this gets the primary key value of object "obj": |
|
45 # |
|
46 # getattr(obj, opts.pk.attname) |
|
47 |
|
48 class Field(object): |
|
49 """Base class for all field types""" |
|
50 __metaclass__ = LegacyConnection |
|
51 |
|
52 # Designates whether empty strings fundamentally are allowed at the |
|
53 # database level. |
|
54 empty_strings_allowed = True |
|
55 |
|
56 # These track each time a Field instance is created. Used to retain order. |
|
57 # The auto_creation_counter is used for fields that Django implicitly |
|
58 # creates, creation_counter is used for all user-specified fields. |
|
59 creation_counter = 0 |
|
60 auto_creation_counter = -1 |
|
61 default_validators = [] # Default set of validators |
|
62 default_error_messages = { |
|
63 'invalid_choice': _(u'Value %r is not a valid choice.'), |
|
64 'null': _(u'This field cannot be null.'), |
|
65 'blank': _(u'This field cannot be blank.'), |
|
66 } |
|
67 |
|
68 # Generic field type description, usually overriden by subclasses |
|
69 def _description(self): |
|
70 return _(u'Field of type: %(field_type)s') % { |
|
71 'field_type': self.__class__.__name__ |
|
72 } |
|
73 description = property(_description) |
|
74 |
|
75 def __init__(self, verbose_name=None, name=None, primary_key=False, |
|
76 max_length=None, unique=False, blank=False, null=False, |
|
77 db_index=False, rel=None, default=NOT_PROVIDED, editable=True, |
|
78 serialize=True, unique_for_date=None, unique_for_month=None, |
|
79 unique_for_year=None, choices=None, help_text='', db_column=None, |
|
80 db_tablespace=None, auto_created=False, validators=[], |
|
81 error_messages=None): |
|
82 self.name = name |
|
83 self.verbose_name = verbose_name |
|
84 self.primary_key = primary_key |
|
85 self.max_length, self._unique = max_length, unique |
|
86 self.blank, self.null = blank, null |
|
87 # Oracle treats the empty string ('') as null, so coerce the null |
|
88 # option whenever '' is a possible value. |
|
89 if self.empty_strings_allowed and connection.features.interprets_empty_strings_as_nulls: |
|
90 self.null = True |
|
91 self.rel = rel |
|
92 self.default = default |
|
93 self.editable = editable |
|
94 self.serialize = serialize |
|
95 self.unique_for_date, self.unique_for_month = unique_for_date, unique_for_month |
|
96 self.unique_for_year = unique_for_year |
|
97 self._choices = choices or [] |
|
98 self.help_text = help_text |
|
99 self.db_column = db_column |
|
100 self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE |
|
101 self.auto_created = auto_created |
|
102 |
|
103 # Set db_index to True if the field has a relationship and doesn't explicitly set db_index. |
|
104 self.db_index = db_index |
|
105 |
|
106 # Adjust the appropriate creation counter, and save our local copy. |
|
107 if auto_created: |
|
108 self.creation_counter = Field.auto_creation_counter |
|
109 Field.auto_creation_counter -= 1 |
|
110 else: |
|
111 self.creation_counter = Field.creation_counter |
|
112 Field.creation_counter += 1 |
|
113 |
|
114 self.validators = self.default_validators + validators |
|
115 |
|
116 messages = {} |
|
117 for c in reversed(self.__class__.__mro__): |
|
118 messages.update(getattr(c, 'default_error_messages', {})) |
|
119 messages.update(error_messages or {}) |
|
120 self.error_messages = messages |
|
121 |
|
122 def __cmp__(self, other): |
|
123 # This is needed because bisect does not take a comparison function. |
|
124 return cmp(self.creation_counter, other.creation_counter) |
|
125 |
|
126 def __deepcopy__(self, memodict): |
|
127 # We don't have to deepcopy very much here, since most things are not |
|
128 # intended to be altered after initial creation. |
|
129 obj = copy.copy(self) |
|
130 if self.rel: |
|
131 obj.rel = copy.copy(self.rel) |
|
132 memodict[id(self)] = obj |
|
133 return obj |
|
134 |
|
135 def to_python(self, value): |
|
136 """ |
|
137 Converts the input value into the expected Python data type, raising |
|
138 django.core.exceptions.ValidationError if the data can't be converted. |
|
139 Returns the converted value. Subclasses should override this. |
|
140 """ |
|
141 return value |
|
142 |
|
143 def run_validators(self, value): |
|
144 if value in validators.EMPTY_VALUES: |
|
145 return |
|
146 |
|
147 errors = [] |
|
148 for v in self.validators: |
|
149 try: |
|
150 v(value) |
|
151 except exceptions.ValidationError, e: |
|
152 if hasattr(e, 'code') and e.code in self.error_messages: |
|
153 message = self.error_messages[e.code] |
|
154 if e.params: |
|
155 message = message % e.params |
|
156 errors.append(message) |
|
157 else: |
|
158 errors.extend(e.messages) |
|
159 if errors: |
|
160 raise exceptions.ValidationError(errors) |
|
161 |
|
162 def validate(self, value, model_instance): |
|
163 """ |
|
164 Validates value and throws ValidationError. Subclasses should override |
|
165 this to provide validation logic. |
|
166 """ |
|
167 if not self.editable: |
|
168 # Skip validation for non-editable fields. |
|
169 return |
|
170 if self._choices and value: |
|
171 for option_key, option_value in self.choices: |
|
172 if isinstance(option_value, (list, tuple)): |
|
173 # This is an optgroup, so look inside the group for options. |
|
174 for optgroup_key, optgroup_value in option_value: |
|
175 if value == optgroup_key: |
|
176 return |
|
177 elif value == option_key: |
|
178 return |
|
179 raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value) |
|
180 |
|
181 if value is None and not self.null: |
|
182 raise exceptions.ValidationError(self.error_messages['null']) |
|
183 |
|
184 if not self.blank and value in validators.EMPTY_VALUES: |
|
185 raise exceptions.ValidationError(self.error_messages['blank']) |
|
186 |
|
187 def clean(self, value, model_instance): |
|
188 """ |
|
189 Convert the value's type and run validation. Validation errors from to_python |
|
190 and validate are propagated. The correct value is returned if no error is |
|
191 raised. |
|
192 """ |
|
193 value = self.to_python(value) |
|
194 self.validate(value, model_instance) |
|
195 self.run_validators(value) |
|
196 return value |
|
197 |
|
198 def db_type(self, connection): |
|
199 """ |
|
200 Returns the database column data type for this field, for the provided |
|
201 connection. |
|
202 """ |
|
203 # The default implementation of this method looks at the |
|
204 # backend-specific DATA_TYPES dictionary, looking up the field by its |
|
205 # "internal type". |
|
206 # |
|
207 # A Field class can implement the get_internal_type() method to specify |
|
208 # which *preexisting* Django Field class it's most similar to -- i.e., |
|
209 # an XMLField is represented by a TEXT column type, which is the same |
|
210 # as the TextField Django field type, which means XMLField's |
|
211 # get_internal_type() returns 'TextField'. |
|
212 # |
|
213 # But the limitation of the get_internal_type() / data_types approach |
|
214 # is that it cannot handle database column types that aren't already |
|
215 # mapped to one of the built-in Django field types. In this case, you |
|
216 # can implement db_type() instead of get_internal_type() to specify |
|
217 # exactly which wacky database column type you want to use. |
|
218 data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") |
|
219 try: |
|
220 return connection.creation.data_types[self.get_internal_type()] % data |
|
221 except KeyError: |
|
222 return None |
|
223 |
|
224 def unique(self): |
|
225 return self._unique or self.primary_key |
|
226 unique = property(unique) |
|
227 |
|
228 def set_attributes_from_name(self, name): |
|
229 self.name = name |
|
230 self.attname, self.column = self.get_attname_column() |
|
231 if self.verbose_name is None and name: |
|
232 self.verbose_name = name.replace('_', ' ') |
|
233 |
|
234 def contribute_to_class(self, cls, name): |
|
235 self.set_attributes_from_name(name) |
|
236 self.model = cls |
|
237 cls._meta.add_field(self) |
|
238 if self.choices: |
|
239 setattr(cls, 'get_%s_display' % self.name, curry(cls._get_FIELD_display, field=self)) |
|
240 |
|
241 def get_attname(self): |
|
242 return self.name |
|
243 |
|
244 def get_attname_column(self): |
|
245 attname = self.get_attname() |
|
246 column = self.db_column or attname |
|
247 return attname, column |
|
248 |
|
249 def get_cache_name(self): |
|
250 return '_%s_cache' % self.name |
|
251 |
|
252 def get_internal_type(self): |
|
253 return self.__class__.__name__ |
|
254 |
|
255 def pre_save(self, model_instance, add): |
|
256 "Returns field's value just before saving." |
|
257 return getattr(model_instance, self.attname) |
|
258 |
|
259 def get_prep_value(self, value): |
|
260 "Perform preliminary non-db specific value checks and conversions." |
|
261 return value |
|
262 |
|
263 def get_db_prep_value(self, value, connection, prepared=False): |
|
264 """Returns field's value prepared for interacting with the database |
|
265 backend. |
|
266 |
|
267 Used by the default implementations of ``get_db_prep_save``and |
|
268 `get_db_prep_lookup``` |
|
269 """ |
|
270 if not prepared: |
|
271 value = self.get_prep_value(value) |
|
272 return value |
|
273 |
|
274 def get_db_prep_save(self, value, connection): |
|
275 "Returns field's value prepared for saving into a database." |
|
276 return self.get_db_prep_value(value, connection=connection, prepared=False) |
|
277 |
|
278 def get_prep_lookup(self, lookup_type, value): |
|
279 "Perform preliminary non-db specific lookup checks and conversions" |
|
280 if hasattr(value, 'prepare'): |
|
281 return value.prepare() |
|
282 if hasattr(value, '_prepare'): |
|
283 return value._prepare() |
|
284 |
|
285 if lookup_type in ( |
|
286 'regex', 'iregex', 'month', 'day', 'week_day', 'search', |
|
287 'contains', 'icontains', 'iexact', 'startswith', 'istartswith', |
|
288 'endswith', 'iendswith', 'isnull' |
|
289 ): |
|
290 return value |
|
291 elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'): |
|
292 return self.get_prep_value(value) |
|
293 elif lookup_type in ('range', 'in'): |
|
294 return [self.get_prep_value(v) for v in value] |
|
295 elif lookup_type == 'year': |
|
296 try: |
|
297 return int(value) |
|
298 except ValueError: |
|
299 raise ValueError("The __year lookup type requires an integer argument") |
|
300 |
|
301 raise TypeError("Field has invalid lookup: %s" % lookup_type) |
|
302 |
|
303 def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False): |
|
304 "Returns field's value prepared for database lookup." |
|
305 if not prepared: |
|
306 value = self.get_prep_lookup(lookup_type, value) |
|
307 if hasattr(value, 'get_compiler'): |
|
308 value = value.get_compiler(connection=connection) |
|
309 if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'): |
|
310 # If the value has a relabel_aliases method, it will need to |
|
311 # be invoked before the final SQL is evaluated |
|
312 if hasattr(value, 'relabel_aliases'): |
|
313 return value |
|
314 if hasattr(value, 'as_sql'): |
|
315 sql, params = value.as_sql() |
|
316 else: |
|
317 sql, params = value._as_sql(connection=connection) |
|
318 return QueryWrapper(('(%s)' % sql), params) |
|
319 |
|
320 if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'): |
|
321 return [value] |
|
322 elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'): |
|
323 return [self.get_db_prep_value(value, connection=connection, prepared=prepared)] |
|
324 elif lookup_type in ('range', 'in'): |
|
325 return [self.get_db_prep_value(v, connection=connection, prepared=prepared) for v in value] |
|
326 elif lookup_type in ('contains', 'icontains'): |
|
327 return ["%%%s%%" % connection.ops.prep_for_like_query(value)] |
|
328 elif lookup_type == 'iexact': |
|
329 return [connection.ops.prep_for_iexact_query(value)] |
|
330 elif lookup_type in ('startswith', 'istartswith'): |
|
331 return ["%s%%" % connection.ops.prep_for_like_query(value)] |
|
332 elif lookup_type in ('endswith', 'iendswith'): |
|
333 return ["%%%s" % connection.ops.prep_for_like_query(value)] |
|
334 elif lookup_type == 'isnull': |
|
335 return [] |
|
336 elif lookup_type == 'year': |
|
337 if self.get_internal_type() == 'DateField': |
|
338 return connection.ops.year_lookup_bounds_for_date_field(value) |
|
339 else: |
|
340 return connection.ops.year_lookup_bounds(value) |
|
341 |
|
342 def has_default(self): |
|
343 "Returns a boolean of whether this field has a default value." |
|
344 return self.default is not NOT_PROVIDED |
|
345 |
|
346 def get_default(self): |
|
347 "Returns the default value for this field." |
|
348 if self.has_default(): |
|
349 if callable(self.default): |
|
350 return self.default() |
|
351 return force_unicode(self.default, strings_only=True) |
|
352 if not self.empty_strings_allowed or (self.null and not connection.features.interprets_empty_strings_as_nulls): |
|
353 return None |
|
354 return "" |
|
355 |
|
356 def get_validator_unique_lookup_type(self): |
|
357 return '%s__exact' % self.name |
|
358 |
|
359 def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): |
|
360 """Returns choices with a default blank choices included, for use |
|
361 as SelectField choices for this field.""" |
|
362 first_choice = include_blank and blank_choice or [] |
|
363 if self.choices: |
|
364 return first_choice + list(self.choices) |
|
365 rel_model = self.rel.to |
|
366 if hasattr(self.rel, 'get_related_field'): |
|
367 lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)] |
|
368 else: |
|
369 lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)] |
|
370 return first_choice + lst |
|
371 |
|
372 def get_choices_default(self): |
|
373 return self.get_choices() |
|
374 |
|
375 def get_flatchoices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): |
|
376 "Returns flattened choices with a default blank choice included." |
|
377 first_choice = include_blank and blank_choice or [] |
|
378 return first_choice + list(self.flatchoices) |
|
379 |
|
380 def _get_val_from_obj(self, obj): |
|
381 if obj is not None: |
|
382 return getattr(obj, self.attname) |
|
383 else: |
|
384 return self.get_default() |
|
385 |
|
386 def value_to_string(self, obj): |
|
387 """ |
|
388 Returns a string value of this field from the passed obj. |
|
389 This is used by the serialization framework. |
|
390 """ |
|
391 return smart_unicode(self._get_val_from_obj(obj)) |
|
392 |
|
393 def bind(self, fieldmapping, original, bound_field_class): |
|
394 return bound_field_class(self, fieldmapping, original) |
|
395 |
|
396 def _get_choices(self): |
|
397 if hasattr(self._choices, 'next'): |
|
398 choices, self._choices = tee(self._choices) |
|
399 return choices |
|
400 else: |
|
401 return self._choices |
|
402 choices = property(_get_choices) |
|
403 |
|
404 def _get_flatchoices(self): |
|
405 """Flattened version of choices tuple.""" |
|
406 flat = [] |
|
407 for choice, value in self.choices: |
|
408 if isinstance(value, (list, tuple)): |
|
409 flat.extend(value) |
|
410 else: |
|
411 flat.append((choice,value)) |
|
412 return flat |
|
413 flatchoices = property(_get_flatchoices) |
|
414 |
|
415 def save_form_data(self, instance, data): |
|
416 setattr(instance, self.name, data) |
|
417 |
|
418 def formfield(self, form_class=forms.CharField, **kwargs): |
|
419 "Returns a django.forms.Field instance for this database Field." |
|
420 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} |
|
421 if self.has_default(): |
|
422 if callable(self.default): |
|
423 defaults['initial'] = self.default |
|
424 defaults['show_hidden_initial'] = True |
|
425 else: |
|
426 defaults['initial'] = self.get_default() |
|
427 if self.choices: |
|
428 # Fields with choices get special treatment. |
|
429 include_blank = self.blank or not (self.has_default() or 'initial' in kwargs) |
|
430 defaults['choices'] = self.get_choices(include_blank=include_blank) |
|
431 defaults['coerce'] = self.to_python |
|
432 if self.null: |
|
433 defaults['empty_value'] = None |
|
434 form_class = forms.TypedChoiceField |
|
435 # Many of the subclass-specific formfield arguments (min_value, |
|
436 # max_value) don't apply for choice fields, so be sure to only pass |
|
437 # the values that TypedChoiceField will understand. |
|
438 for k in kwargs.keys(): |
|
439 if k not in ('coerce', 'empty_value', 'choices', 'required', |
|
440 'widget', 'label', 'initial', 'help_text', |
|
441 'error_messages', 'show_hidden_initial'): |
|
442 del kwargs[k] |
|
443 defaults.update(kwargs) |
|
444 return form_class(**defaults) |
|
445 |
|
446 def value_from_object(self, obj): |
|
447 "Returns the value of this field in the given model instance." |
|
448 return getattr(obj, self.attname) |
|
449 |
|
450 class AutoField(Field): |
|
451 description = _("Integer") |
|
452 |
|
453 empty_strings_allowed = False |
|
454 default_error_messages = { |
|
455 'invalid': _(u'This value must be an integer.'), |
|
456 } |
|
457 def __init__(self, *args, **kwargs): |
|
458 assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__ |
|
459 kwargs['blank'] = True |
|
460 Field.__init__(self, *args, **kwargs) |
|
461 |
|
462 def to_python(self, value): |
|
463 if value is None: |
|
464 return value |
|
465 try: |
|
466 return int(value) |
|
467 except (TypeError, ValueError): |
|
468 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
469 |
|
470 def validate(self, value, model_instance): |
|
471 pass |
|
472 |
|
473 def get_prep_value(self, value): |
|
474 if value is None: |
|
475 return None |
|
476 return int(value) |
|
477 |
|
478 def contribute_to_class(self, cls, name): |
|
479 assert not cls._meta.has_auto_field, "A model can't have more than one AutoField." |
|
480 super(AutoField, self).contribute_to_class(cls, name) |
|
481 cls._meta.has_auto_field = True |
|
482 cls._meta.auto_field = self |
|
483 |
|
484 def formfield(self, **kwargs): |
|
485 return None |
|
486 |
|
487 class BooleanField(Field): |
|
488 empty_strings_allowed = False |
|
489 default_error_messages = { |
|
490 'invalid': _(u'This value must be either True or False.'), |
|
491 } |
|
492 description = _("Boolean (Either True or False)") |
|
493 def __init__(self, *args, **kwargs): |
|
494 kwargs['blank'] = True |
|
495 if 'default' not in kwargs and not kwargs.get('null'): |
|
496 kwargs['default'] = False |
|
497 Field.__init__(self, *args, **kwargs) |
|
498 |
|
499 def get_internal_type(self): |
|
500 return "BooleanField" |
|
501 |
|
502 def to_python(self, value): |
|
503 if value in (True, False): |
|
504 # if value is 1 or 0 than it's equal to True or False, but we want |
|
505 # to return a true bool for semantic reasons. |
|
506 return bool(value) |
|
507 if value in ('t', 'True', '1'): |
|
508 return True |
|
509 if value in ('f', 'False', '0'): |
|
510 return False |
|
511 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
512 |
|
513 def get_prep_lookup(self, lookup_type, value): |
|
514 # Special-case handling for filters coming from a web request (e.g. the |
|
515 # admin interface). Only works for scalar values (not lists). If you're |
|
516 # passing in a list, you might as well make things the right type when |
|
517 # constructing the list. |
|
518 if value in ('1', '0'): |
|
519 value = bool(int(value)) |
|
520 return super(BooleanField, self).get_prep_lookup(lookup_type, value) |
|
521 |
|
522 def get_prep_value(self, value): |
|
523 if value is None: |
|
524 return None |
|
525 return bool(value) |
|
526 |
|
527 def formfield(self, **kwargs): |
|
528 # Unlike most fields, BooleanField figures out include_blank from |
|
529 # self.null instead of self.blank. |
|
530 if self.choices: |
|
531 include_blank = self.null or not (self.has_default() or 'initial' in kwargs) |
|
532 defaults = {'choices': self.get_choices(include_blank=include_blank)} |
|
533 else: |
|
534 defaults = {'form_class': forms.BooleanField} |
|
535 defaults.update(kwargs) |
|
536 return super(BooleanField, self).formfield(**defaults) |
|
537 |
|
538 class CharField(Field): |
|
539 description = _("String (up to %(max_length)s)") |
|
540 |
|
541 def __init__(self, *args, **kwargs): |
|
542 super(CharField, self).__init__(*args, **kwargs) |
|
543 self.validators.append(validators.MaxLengthValidator(self.max_length)) |
|
544 |
|
545 def get_internal_type(self): |
|
546 return "CharField" |
|
547 |
|
548 def to_python(self, value): |
|
549 if isinstance(value, basestring) or value is None: |
|
550 return value |
|
551 return smart_unicode(value) |
|
552 |
|
553 def get_prep_value(self, value): |
|
554 return self.to_python(value) |
|
555 |
|
556 def formfield(self, **kwargs): |
|
557 # Passing max_length to forms.CharField means that the value's length |
|
558 # will be validated twice. This is considered acceptable since we want |
|
559 # the value in the form field (to pass into widget for example). |
|
560 defaults = {'max_length': self.max_length} |
|
561 defaults.update(kwargs) |
|
562 return super(CharField, self).formfield(**defaults) |
|
563 |
|
564 # TODO: Maybe move this into contrib, because it's specialized. |
|
565 class CommaSeparatedIntegerField(CharField): |
|
566 default_validators = [validators.validate_comma_separated_integer_list] |
|
567 description = _("Comma-separated integers") |
|
568 |
|
569 def formfield(self, **kwargs): |
|
570 defaults = { |
|
571 'error_messages': { |
|
572 'invalid': _(u'Enter only digits separated by commas.'), |
|
573 } |
|
574 } |
|
575 defaults.update(kwargs) |
|
576 return super(CommaSeparatedIntegerField, self).formfield(**defaults) |
|
577 |
|
578 ansi_date_re = re.compile(r'^\d{4}-\d{1,2}-\d{1,2}$') |
|
579 |
|
580 class DateField(Field): |
|
581 description = _("Date (without time)") |
|
582 |
|
583 empty_strings_allowed = False |
|
584 default_error_messages = { |
|
585 'invalid': _('Enter a valid date in YYYY-MM-DD format.'), |
|
586 'invalid_date': _('Invalid date: %s'), |
|
587 } |
|
588 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): |
|
589 self.auto_now, self.auto_now_add = auto_now, auto_now_add |
|
590 #HACKs : auto_now_add/auto_now should be done as a default or a pre_save. |
|
591 if auto_now or auto_now_add: |
|
592 kwargs['editable'] = False |
|
593 kwargs['blank'] = True |
|
594 Field.__init__(self, verbose_name, name, **kwargs) |
|
595 |
|
596 def get_internal_type(self): |
|
597 return "DateField" |
|
598 |
|
599 def to_python(self, value): |
|
600 if value is None: |
|
601 return value |
|
602 if isinstance(value, datetime.datetime): |
|
603 return value.date() |
|
604 if isinstance(value, datetime.date): |
|
605 return value |
|
606 |
|
607 if not ansi_date_re.search(value): |
|
608 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
609 # Now that we have the date string in YYYY-MM-DD format, check to make |
|
610 # sure it's a valid date. |
|
611 # We could use time.strptime here and catch errors, but datetime.date |
|
612 # produces much friendlier error messages. |
|
613 year, month, day = map(int, value.split('-')) |
|
614 try: |
|
615 return datetime.date(year, month, day) |
|
616 except ValueError, e: |
|
617 msg = self.error_messages['invalid_date'] % _(str(e)) |
|
618 raise exceptions.ValidationError(msg) |
|
619 |
|
620 def pre_save(self, model_instance, add): |
|
621 if self.auto_now or (self.auto_now_add and add): |
|
622 value = datetime.datetime.now() |
|
623 setattr(model_instance, self.attname, value) |
|
624 return value |
|
625 else: |
|
626 return super(DateField, self).pre_save(model_instance, add) |
|
627 |
|
628 def contribute_to_class(self, cls, name): |
|
629 super(DateField,self).contribute_to_class(cls, name) |
|
630 if not self.null: |
|
631 setattr(cls, 'get_next_by_%s' % self.name, |
|
632 curry(cls._get_next_or_previous_by_FIELD, field=self, is_next=True)) |
|
633 setattr(cls, 'get_previous_by_%s' % self.name, |
|
634 curry(cls._get_next_or_previous_by_FIELD, field=self, is_next=False)) |
|
635 |
|
636 def get_prep_lookup(self, lookup_type, value): |
|
637 # For "__month", "__day", and "__week_day" lookups, convert the value |
|
638 # to an int so the database backend always sees a consistent type. |
|
639 if lookup_type in ('month', 'day', 'week_day'): |
|
640 return int(value) |
|
641 return super(DateField, self).get_prep_lookup(lookup_type, value) |
|
642 |
|
643 def get_prep_value(self, value): |
|
644 return self.to_python(value) |
|
645 |
|
646 def get_db_prep_value(self, value, connection, prepared=False): |
|
647 # Casts dates into the format expected by the backend |
|
648 if not prepared: |
|
649 value = self.get_prep_value(value) |
|
650 return connection.ops.value_to_db_date(value) |
|
651 |
|
652 def value_to_string(self, obj): |
|
653 val = self._get_val_from_obj(obj) |
|
654 if val is None: |
|
655 data = '' |
|
656 else: |
|
657 data = datetime_safe.new_date(val).strftime("%Y-%m-%d") |
|
658 return data |
|
659 |
|
660 def formfield(self, **kwargs): |
|
661 defaults = {'form_class': forms.DateField} |
|
662 defaults.update(kwargs) |
|
663 return super(DateField, self).formfield(**defaults) |
|
664 |
|
665 class DateTimeField(DateField): |
|
666 default_error_messages = { |
|
667 'invalid': _(u'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.'), |
|
668 } |
|
669 description = _("Date (with time)") |
|
670 |
|
671 def get_internal_type(self): |
|
672 return "DateTimeField" |
|
673 |
|
674 def to_python(self, value): |
|
675 if value is None: |
|
676 return value |
|
677 if isinstance(value, datetime.datetime): |
|
678 return value |
|
679 if isinstance(value, datetime.date): |
|
680 return datetime.datetime(value.year, value.month, value.day) |
|
681 |
|
682 # Attempt to parse a datetime: |
|
683 value = smart_str(value) |
|
684 # split usecs, because they are not recognized by strptime. |
|
685 if '.' in value: |
|
686 try: |
|
687 value, usecs = value.split('.') |
|
688 usecs = int(usecs) |
|
689 except ValueError: |
|
690 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
691 else: |
|
692 usecs = 0 |
|
693 kwargs = {'microsecond': usecs} |
|
694 try: # Seconds are optional, so try converting seconds first. |
|
695 return datetime.datetime(*time.strptime(value, '%Y-%m-%d %H:%M:%S')[:6], |
|
696 **kwargs) |
|
697 |
|
698 except ValueError: |
|
699 try: # Try without seconds. |
|
700 return datetime.datetime(*time.strptime(value, '%Y-%m-%d %H:%M')[:5], |
|
701 **kwargs) |
|
702 except ValueError: # Try without hour/minutes/seconds. |
|
703 try: |
|
704 return datetime.datetime(*time.strptime(value, '%Y-%m-%d')[:3], |
|
705 **kwargs) |
|
706 except ValueError: |
|
707 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
708 |
|
709 def get_prep_value(self, value): |
|
710 return self.to_python(value) |
|
711 |
|
712 def get_db_prep_value(self, value, connection, prepared=False): |
|
713 # Casts dates into the format expected by the backend |
|
714 if not prepared: |
|
715 value = self.get_prep_value(value) |
|
716 return connection.ops.value_to_db_datetime(value) |
|
717 |
|
718 def value_to_string(self, obj): |
|
719 val = self._get_val_from_obj(obj) |
|
720 if val is None: |
|
721 data = '' |
|
722 else: |
|
723 d = datetime_safe.new_datetime(val) |
|
724 data = d.strftime('%Y-%m-%d %H:%M:%S') |
|
725 return data |
|
726 |
|
727 def formfield(self, **kwargs): |
|
728 defaults = {'form_class': forms.DateTimeField} |
|
729 defaults.update(kwargs) |
|
730 return super(DateTimeField, self).formfield(**defaults) |
|
731 |
|
732 class DecimalField(Field): |
|
733 empty_strings_allowed = False |
|
734 default_error_messages = { |
|
735 'invalid': _(u'This value must be a decimal number.'), |
|
736 } |
|
737 description = _("Decimal number") |
|
738 |
|
739 def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs): |
|
740 self.max_digits, self.decimal_places = max_digits, decimal_places |
|
741 Field.__init__(self, verbose_name, name, **kwargs) |
|
742 |
|
743 def get_internal_type(self): |
|
744 return "DecimalField" |
|
745 |
|
746 def to_python(self, value): |
|
747 if value is None: |
|
748 return value |
|
749 try: |
|
750 return decimal.Decimal(value) |
|
751 except decimal.InvalidOperation: |
|
752 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
753 |
|
754 def _format(self, value): |
|
755 if isinstance(value, basestring) or value is None: |
|
756 return value |
|
757 else: |
|
758 return self.format_number(value) |
|
759 |
|
760 def format_number(self, value): |
|
761 """ |
|
762 Formats a number into a string with the requisite number of digits and |
|
763 decimal places. |
|
764 """ |
|
765 # Method moved to django.db.backends.util. |
|
766 # |
|
767 # It is preserved because it is used by the oracle backend |
|
768 # (django.db.backends.oracle.query), and also for |
|
769 # backwards-compatibility with any external code which may have used |
|
770 # this method. |
|
771 from django.db.backends import util |
|
772 return util.format_number(value, self.max_digits, self.decimal_places) |
|
773 |
|
774 def get_db_prep_save(self, value, connection): |
|
775 return connection.ops.value_to_db_decimal(self.to_python(value), |
|
776 self.max_digits, self.decimal_places) |
|
777 |
|
778 def get_prep_value(self, value): |
|
779 return self.to_python(value) |
|
780 |
|
781 def formfield(self, **kwargs): |
|
782 defaults = { |
|
783 'max_digits': self.max_digits, |
|
784 'decimal_places': self.decimal_places, |
|
785 'form_class': forms.DecimalField, |
|
786 } |
|
787 defaults.update(kwargs) |
|
788 return super(DecimalField, self).formfield(**defaults) |
|
789 |
|
790 class EmailField(CharField): |
|
791 default_validators = [validators.validate_email] |
|
792 description = _("E-mail address") |
|
793 |
|
794 def __init__(self, *args, **kwargs): |
|
795 kwargs['max_length'] = kwargs.get('max_length', 75) |
|
796 CharField.__init__(self, *args, **kwargs) |
|
797 |
|
798 class FilePathField(Field): |
|
799 description = _("File path") |
|
800 |
|
801 def __init__(self, verbose_name=None, name=None, path='', match=None, recursive=False, **kwargs): |
|
802 self.path, self.match, self.recursive = path, match, recursive |
|
803 kwargs['max_length'] = kwargs.get('max_length', 100) |
|
804 Field.__init__(self, verbose_name, name, **kwargs) |
|
805 |
|
806 def formfield(self, **kwargs): |
|
807 defaults = { |
|
808 'path': self.path, |
|
809 'match': self.match, |
|
810 'recursive': self.recursive, |
|
811 'form_class': forms.FilePathField, |
|
812 } |
|
813 defaults.update(kwargs) |
|
814 return super(FilePathField, self).formfield(**defaults) |
|
815 |
|
816 def get_internal_type(self): |
|
817 return "FilePathField" |
|
818 |
|
819 class FloatField(Field): |
|
820 empty_strings_allowed = False |
|
821 default_error_messages = { |
|
822 'invalid': _("This value must be a float."), |
|
823 } |
|
824 description = _("Floating point number") |
|
825 |
|
826 def get_prep_value(self, value): |
|
827 if value is None: |
|
828 return None |
|
829 return float(value) |
|
830 |
|
831 def get_internal_type(self): |
|
832 return "FloatField" |
|
833 |
|
834 def to_python(self, value): |
|
835 if value is None: |
|
836 return value |
|
837 try: |
|
838 return float(value) |
|
839 except (TypeError, ValueError): |
|
840 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
841 |
|
842 def formfield(self, **kwargs): |
|
843 defaults = {'form_class': forms.FloatField} |
|
844 defaults.update(kwargs) |
|
845 return super(FloatField, self).formfield(**defaults) |
|
846 |
|
847 class IntegerField(Field): |
|
848 empty_strings_allowed = False |
|
849 default_error_messages = { |
|
850 'invalid': _("This value must be an integer."), |
|
851 } |
|
852 description = _("Integer") |
|
853 |
|
854 def get_prep_value(self, value): |
|
855 if value is None: |
|
856 return None |
|
857 return int(value) |
|
858 |
|
859 def get_prep_lookup(self, lookup_type, value): |
|
860 if (lookup_type == 'gte' or lookup_type == 'lt') \ |
|
861 and isinstance(value, float): |
|
862 value = math.ceil(value) |
|
863 return super(IntegerField, self).get_prep_lookup(lookup_type, value) |
|
864 |
|
865 def get_internal_type(self): |
|
866 return "IntegerField" |
|
867 |
|
868 def to_python(self, value): |
|
869 if value is None: |
|
870 return value |
|
871 try: |
|
872 return int(value) |
|
873 except (TypeError, ValueError): |
|
874 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
875 |
|
876 def formfield(self, **kwargs): |
|
877 defaults = {'form_class': forms.IntegerField} |
|
878 defaults.update(kwargs) |
|
879 return super(IntegerField, self).formfield(**defaults) |
|
880 |
|
881 class BigIntegerField(IntegerField): |
|
882 empty_strings_allowed = False |
|
883 description = _("Big (8 byte) integer") |
|
884 MAX_BIGINT = 9223372036854775807 |
|
885 def get_internal_type(self): |
|
886 return "BigIntegerField" |
|
887 |
|
888 def formfield(self, **kwargs): |
|
889 defaults = {'min_value': -BigIntegerField.MAX_BIGINT - 1, |
|
890 'max_value': BigIntegerField.MAX_BIGINT} |
|
891 defaults.update(kwargs) |
|
892 return super(BigIntegerField, self).formfield(**defaults) |
|
893 |
|
894 class IPAddressField(Field): |
|
895 empty_strings_allowed = False |
|
896 description = _("IP address") |
|
897 def __init__(self, *args, **kwargs): |
|
898 kwargs['max_length'] = 15 |
|
899 Field.__init__(self, *args, **kwargs) |
|
900 |
|
901 def get_internal_type(self): |
|
902 return "IPAddressField" |
|
903 |
|
904 def formfield(self, **kwargs): |
|
905 defaults = {'form_class': forms.IPAddressField} |
|
906 defaults.update(kwargs) |
|
907 return super(IPAddressField, self).formfield(**defaults) |
|
908 |
|
909 class NullBooleanField(Field): |
|
910 empty_strings_allowed = False |
|
911 default_error_messages = { |
|
912 'invalid': _("This value must be either None, True or False."), |
|
913 } |
|
914 description = _("Boolean (Either True, False or None)") |
|
915 |
|
916 def __init__(self, *args, **kwargs): |
|
917 kwargs['null'] = True |
|
918 kwargs['blank'] = True |
|
919 Field.__init__(self, *args, **kwargs) |
|
920 |
|
921 def get_internal_type(self): |
|
922 return "NullBooleanField" |
|
923 |
|
924 def to_python(self, value): |
|
925 if value is None: |
|
926 return None |
|
927 if value in (True, False): |
|
928 return bool(value) |
|
929 if value in ('None',): |
|
930 return None |
|
931 if value in ('t', 'True', '1'): |
|
932 return True |
|
933 if value in ('f', 'False', '0'): |
|
934 return False |
|
935 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
936 |
|
937 def get_prep_lookup(self, lookup_type, value): |
|
938 # Special-case handling for filters coming from a web request (e.g. the |
|
939 # admin interface). Only works for scalar values (not lists). If you're |
|
940 # passing in a list, you might as well make things the right type when |
|
941 # constructing the list. |
|
942 if value in ('1', '0'): |
|
943 value = bool(int(value)) |
|
944 return super(NullBooleanField, self).get_prep_lookup(lookup_type, value) |
|
945 |
|
946 def get_prep_value(self, value): |
|
947 if value is None: |
|
948 return None |
|
949 return bool(value) |
|
950 |
|
951 def formfield(self, **kwargs): |
|
952 defaults = { |
|
953 'form_class': forms.NullBooleanField, |
|
954 'required': not self.blank, |
|
955 'label': capfirst(self.verbose_name), |
|
956 'help_text': self.help_text} |
|
957 defaults.update(kwargs) |
|
958 return super(NullBooleanField, self).formfield(**defaults) |
|
959 |
|
960 class PositiveIntegerField(IntegerField): |
|
961 description = _("Integer") |
|
962 |
|
963 def get_internal_type(self): |
|
964 return "PositiveIntegerField" |
|
965 |
|
966 def formfield(self, **kwargs): |
|
967 defaults = {'min_value': 0} |
|
968 defaults.update(kwargs) |
|
969 return super(PositiveIntegerField, self).formfield(**defaults) |
|
970 |
|
971 class PositiveSmallIntegerField(IntegerField): |
|
972 description = _("Integer") |
|
973 def get_internal_type(self): |
|
974 return "PositiveSmallIntegerField" |
|
975 |
|
976 def formfield(self, **kwargs): |
|
977 defaults = {'min_value': 0} |
|
978 defaults.update(kwargs) |
|
979 return super(PositiveSmallIntegerField, self).formfield(**defaults) |
|
980 |
|
981 class SlugField(CharField): |
|
982 description = _("String (up to %(max_length)s)") |
|
983 def __init__(self, *args, **kwargs): |
|
984 kwargs['max_length'] = kwargs.get('max_length', 50) |
|
985 # Set db_index=True unless it's been set manually. |
|
986 if 'db_index' not in kwargs: |
|
987 kwargs['db_index'] = True |
|
988 super(SlugField, self).__init__(*args, **kwargs) |
|
989 |
|
990 def get_internal_type(self): |
|
991 return "SlugField" |
|
992 |
|
993 def formfield(self, **kwargs): |
|
994 defaults = {'form_class': forms.SlugField} |
|
995 defaults.update(kwargs) |
|
996 return super(SlugField, self).formfield(**defaults) |
|
997 |
|
998 class SmallIntegerField(IntegerField): |
|
999 description = _("Integer") |
|
1000 |
|
1001 def get_internal_type(self): |
|
1002 return "SmallIntegerField" |
|
1003 |
|
1004 class TextField(Field): |
|
1005 description = _("Text") |
|
1006 |
|
1007 def get_internal_type(self): |
|
1008 return "TextField" |
|
1009 |
|
1010 def get_prep_value(self, value): |
|
1011 if isinstance(value, basestring) or value is None: |
|
1012 return value |
|
1013 return smart_unicode(value) |
|
1014 |
|
1015 def formfield(self, **kwargs): |
|
1016 defaults = {'widget': forms.Textarea} |
|
1017 defaults.update(kwargs) |
|
1018 return super(TextField, self).formfield(**defaults) |
|
1019 |
|
1020 class TimeField(Field): |
|
1021 description = _("Time") |
|
1022 |
|
1023 empty_strings_allowed = False |
|
1024 default_error_messages = { |
|
1025 'invalid': _('Enter a valid time in HH:MM[:ss[.uuuuuu]] format.'), |
|
1026 } |
|
1027 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): |
|
1028 self.auto_now, self.auto_now_add = auto_now, auto_now_add |
|
1029 if auto_now or auto_now_add: |
|
1030 kwargs['editable'] = False |
|
1031 Field.__init__(self, verbose_name, name, **kwargs) |
|
1032 |
|
1033 def get_internal_type(self): |
|
1034 return "TimeField" |
|
1035 |
|
1036 def to_python(self, value): |
|
1037 if value is None: |
|
1038 return None |
|
1039 if isinstance(value, datetime.time): |
|
1040 return value |
|
1041 if isinstance(value, datetime.datetime): |
|
1042 # Not usually a good idea to pass in a datetime here (it loses |
|
1043 # information), but this can be a side-effect of interacting with a |
|
1044 # database backend (e.g. Oracle), so we'll be accommodating. |
|
1045 return value.time() |
|
1046 |
|
1047 # Attempt to parse a datetime: |
|
1048 value = smart_str(value) |
|
1049 # split usecs, because they are not recognized by strptime. |
|
1050 if '.' in value: |
|
1051 try: |
|
1052 value, usecs = value.split('.') |
|
1053 usecs = int(usecs) |
|
1054 except ValueError: |
|
1055 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
1056 else: |
|
1057 usecs = 0 |
|
1058 kwargs = {'microsecond': usecs} |
|
1059 |
|
1060 try: # Seconds are optional, so try converting seconds first. |
|
1061 return datetime.time(*time.strptime(value, '%H:%M:%S')[3:6], |
|
1062 **kwargs) |
|
1063 except ValueError: |
|
1064 try: # Try without seconds. |
|
1065 return datetime.time(*time.strptime(value, '%H:%M')[3:5], |
|
1066 **kwargs) |
|
1067 except ValueError: |
|
1068 raise exceptions.ValidationError(self.error_messages['invalid']) |
|
1069 |
|
1070 def pre_save(self, model_instance, add): |
|
1071 if self.auto_now or (self.auto_now_add and add): |
|
1072 value = datetime.datetime.now().time() |
|
1073 setattr(model_instance, self.attname, value) |
|
1074 return value |
|
1075 else: |
|
1076 return super(TimeField, self).pre_save(model_instance, add) |
|
1077 |
|
1078 def get_prep_value(self, value): |
|
1079 return self.to_python(value) |
|
1080 |
|
1081 def get_db_prep_value(self, value, connection, prepared=False): |
|
1082 # Casts times into the format expected by the backend |
|
1083 if not prepared: |
|
1084 value = self.get_prep_value(value) |
|
1085 return connection.ops.value_to_db_time(value) |
|
1086 |
|
1087 def value_to_string(self, obj): |
|
1088 val = self._get_val_from_obj(obj) |
|
1089 if val is None: |
|
1090 data = '' |
|
1091 else: |
|
1092 data = val.strftime("%H:%M:%S") |
|
1093 return data |
|
1094 |
|
1095 def formfield(self, **kwargs): |
|
1096 defaults = {'form_class': forms.TimeField} |
|
1097 defaults.update(kwargs) |
|
1098 return super(TimeField, self).formfield(**defaults) |
|
1099 |
|
1100 class URLField(CharField): |
|
1101 description = _("URL") |
|
1102 |
|
1103 def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): |
|
1104 kwargs['max_length'] = kwargs.get('max_length', 200) |
|
1105 CharField.__init__(self, verbose_name, name, **kwargs) |
|
1106 self.validators.append(validators.URLValidator(verify_exists=verify_exists)) |
|
1107 |
|
1108 class XMLField(TextField): |
|
1109 description = _("XML text") |
|
1110 |
|
1111 def __init__(self, verbose_name=None, name=None, schema_path=None, **kwargs): |
|
1112 self.schema_path = schema_path |
|
1113 Field.__init__(self, verbose_name, name, **kwargs) |
|
1114 |