Ticket #1553: fields.diff
File fields.diff, 9.2 KB (added by , 19 years ago) |
---|
-
store/django_no_magic/django/db/models/fields/__init__.py
112 112 """ 113 113 if not self.blank and not field_data: 114 114 return [gettext_lazy('This field is required.')] 115 # XXX IMHO this is the place for NULL validation, to_python will return '' (blank) or None as appropriate 116 if not self.null and field_data is None: 117 return [gettext_lazy('This field is required.')] 115 118 try: 116 119 self.validate(field_data, all_data) 117 120 except validators.ValidationError, e: … … 331 334 def __init__(self, *args, **kwargs): 332 335 assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__ 333 336 kwargs['blank'] = True 337 # added because validators 338 kwargs['null'] = True 334 339 Field.__init__(self, *args, **kwargs) 335 340 336 341 def to_python(self, value): … … 368 373 Field.__init__(self, *args, **kwargs) 369 374 370 375 def to_python(self, value): 376 if value is None: 377 raise validators.ValidationError, gettext("This value can not be None.") 371 378 if value in (True, False): return value 379 if value == '': return False 372 380 if value is 't': return True 373 381 if value is 'f': return False 374 382 raise validators.ValidationError, gettext("This value must be either True or False.") … … 383 391 def to_python(self, value): 384 392 if isinstance(value, basestring): 385 393 return value 386 if value is None: 387 if self.null: 388 return value 389 else: 390 raise validators.ValidationError, gettext_lazy("This field cannot be null.") 394 if value is None: return value 391 395 return str(value) 396 397 def validate(self, field_data, all_data): 398 if not field_data: return 399 if self.maxlength < len(field_data): 400 raise validators.ValidationError, gettext_lazy("Maximum string length exceeded.") 392 401 393 402 # TODO: Maybe move this into contrib, because it's specialized. 394 403 class CommaSeparatedIntegerField(CharField): … … 395 404 def get_manipulator_field_objs(self): 396 405 return [forms.CommaSeparatedIntegerField] 397 406 407 def to_python(self, value): 408 value = super(CommaSeparatedIntegerField, self).to_python(value) 409 if not value: return [] 410 try: 411 return [int(l) for l in value.split(',')] 412 except (TypeError, ValueError): 413 raise ValidationError, gettext("Enter a whole number.") 414 415 def validate(self, field_data, all_data): 416 if not field_data: return 417 # XXX hmmm, I presume that maxlength here means the maximum entries 418 if self.maxlength < len(field_data): 419 raise validators.ValidationError, gettext_lazy("Exceeded maximum number of entries.") 420 421 398 422 class DateField(Field): 399 423 empty_strings_allowed = False 400 424 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): … … 406 430 Field.__init__(self, verbose_name, name, **kwargs) 407 431 408 432 def to_python(self, value): 433 if value is None or value == '': return None 409 434 if isinstance(value, datetime.datetime): 410 435 return value.date() 411 436 if isinstance(value, datetime.date): … … 457 482 458 483 class DateTimeField(DateField): 459 484 def to_python(self, value): 485 if value is None or value == '': return None 460 486 if isinstance(value, datetime.datetime): 461 487 return value 462 488 if isinstance(value, datetime.date): … … 625 651 def get_manipulator_field_objs(self): 626 652 return [curry(forms.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)] 627 653 654 def to_python(self, value): 655 import decimal 656 if value is None or value == '': return None 657 try: 658 return decimal.Decimal(value) 659 except (TypeError, ValueError): 660 raise validators.ValidationError, gettext_lazy("Enter a float number.") 661 662 def validate(self, field_data, all_data): 663 if len('%.0f' % abs(field_data)) > self.max_digits: 664 raise validators.ValidationError, gettext_lazy("Too many digits.") 665 628 666 class ImageField(FileField): 629 667 def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs): 630 668 self.width_field, self.height_field = width_field, height_field … … 659 697 def get_manipulator_field_objs(self): 660 698 return [forms.IntegerField] 661 699 700 def to_python(self, value): 701 if value is None or value == '': return None 702 try: 703 return int(value) 704 except (TypeError, ValueError): 705 raise validators.ValidationError, gettext_lazy("Enter a whole number.") 706 662 707 class IPAddressField(Field): 663 708 def __init__(self, *args, **kwargs): 664 709 kwargs['maxlength'] = 15 … … 678 723 def get_manipulator_field_objs(self): 679 724 return [forms.NullBooleanField] 680 725 726 def to_python(self, value): 727 if value is None or value == '': return None 728 if value in (True, False): return value 729 if value is 't': return True 730 if value is 'f': return False 731 raise validators.ValidationError, gettext("This value must be either True or False.") 732 681 733 class PhoneNumberField(IntegerField): 682 734 def get_manipulator_field_objs(self): 683 735 return [forms.PhoneNumberField] … … 689 741 def get_manipulator_field_objs(self): 690 742 return [forms.PositiveIntegerField] 691 743 744 def to_python(self, value): 745 if value is None or value == '': return None 746 try: 747 return int(value) 748 except (TypeError, ValueError): 749 raise validators.ValidationError, gettext_lazy("Enter a whole number.") 750 751 def validate(self, field_data, all_data): 752 if field_data < 0: 753 raise validators.ValidationError, gettext_lazy("Enter a whole positive number.") 754 692 755 class PositiveSmallIntegerField(IntegerField): 693 756 def get_manipulator_field_objs(self): 694 757 return [forms.PositiveSmallIntegerField] … … 693 756 def get_manipulator_field_objs(self): 694 757 return [forms.PositiveSmallIntegerField] 695 758 759 def to_python(self, value): 760 if value is None or value == '': return None 761 try: 762 return int(value) 763 except (TypeError, ValueError): 764 raise validators.ValidationError, gettext_lazy("Enter a whole number.") 765 766 def validate(self, field_data, all_data): 767 if field_data < 0: 768 raise validators.ValidationError, gettext_lazy("Enter a whole positive number.") 769 696 770 class SlugField(Field): 697 771 def __init__(self, *args, **kwargs): 698 772 kwargs['maxlength'] = 50 … … 705 779 def get_manipulator_field_objs(self): 706 780 return [forms.TextField] 707 781 782 def validate(self, field_data, all_data): 783 validators.isSlug(field_data, all_data) 784 708 785 class SmallIntegerField(IntegerField): 709 786 def get_manipulator_field_objs(self): 710 787 return [forms.SmallIntegerField] … … 709 786 def get_manipulator_field_objs(self): 710 787 return [forms.SmallIntegerField] 711 788 789 def to_python(self, value): 790 if value is None or value == '': return None 791 try: 792 return int(value) 793 except (TypeError, ValueError): 794 raise validators.ValidationError, gettext_lazy("Enter a whole number.") 795 712 796 class TextField(Field): 713 797 def get_manipulator_field_objs(self): 714 798 return [forms.LargeTextField] … … 750 834 val = self._get_val_from_obj(obj) 751 835 return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')} 752 836 837 def to_python(self, value): 838 if value is None or value == '': return None 839 try: 840 st = time.strptime(value, '%H:%M:%S') 841 return datetime.time(st.tm_hour, st.tm_min, st.tm_sec) 842 except (TypeError, ValueError): 843 raise validators.ValidationError, gettext_lazy("Enter a whole number.") 844 753 845 class URLField(Field): 754 846 def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): 755 847 if verify_exists: … … 754 846 def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): 755 847 if verify_exists: 756 848 kwargs.setdefault('validator_list', []).append(validators.isExistingURL) 849 self.verify_exists = verify_exists 757 850 Field.__init__(self, verbose_name, name, **kwargs) 758 851 759 852 def get_manipulator_field_objs(self): … … 759 852 def get_manipulator_field_objs(self): 760 853 return [forms.URLField] 761 854 855 def to_python(self, value): 856 if isinstance(value, basestring): return value 857 if value is None: return value 858 return str(value) 859 860 def validate(self, field_data, all_data): 861 validators.isValidURL(field_data, all_data) 862 if self.verify_exists: 863 validators.isExistingURL(field_data, all_data) 864 762 865 class USStateField(Field): 763 866 def get_manipulator_field_objs(self): 764 867 return [forms.USStateField]