Ticket #11716: 11716-2.patch

File 11716-2.patch, 8.6 KB (added by Leo Shklovskii, 15 years ago)

updated patch against r12623

  • django/contrib/contenttypes/views.py

     
    11from django import http
    22from django.contrib.contenttypes.models import ContentType
    33from django.contrib.sites.models import Site
    4 from django.core.exceptions import ObjectDoesNotExist
     4from django.core.exceptions import ObjectDoesNotExist, ValidationError
    55
    66def shortcut(request, content_type_id, object_id):
    77    "Redirect to an object's page based on a content-type ID and an object ID."
     
    99    try:
    1010        content_type = ContentType.objects.get(pk=content_type_id)
    1111        obj = content_type.get_object_for_this_type(pk=object_id)
    12     except (ObjectDoesNotExist, ValueError):
     12    except (ObjectDoesNotExist, ValidationError):
    1313        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
    1414    try:
    1515        absurl = obj.get_absolute_url()
  • django/db/models/fields/__init__.py

     
    495495        pass
    496496
    497497    def get_prep_value(self, value):
    498         if value is None:
    499             return None
    500         return int(value)
     498        return self.to_python(value)
    501499
    502500    def contribute_to_class(self, cls, name):
    503501        assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
     
    544542        return super(BooleanField, self).get_prep_lookup(lookup_type, value)
    545543
    546544    def get_prep_value(self, value):
    547         if value is None:
    548             return None
    549         return bool(value)
     545        return self.to_python(value)
    550546
    551547    def formfield(self, **kwargs):
    552548        # Unlike most fields, BooleanField figures out include_blank from
     
    848844    description = _("Floating point number")
    849845
    850846    def get_prep_value(self, value):
    851         if value is None:
    852             return None
    853         return float(value)
     847        return self.to_python(value)
    854848
    855849    def get_internal_type(self):
    856850        return "FloatField"
     
    871865class IntegerField(Field):
    872866    empty_strings_allowed = False
    873867    default_error_messages = {
    874         'invalid': _("This value must be a float."),
     868        'invalid': _("This value must be an integer."),
    875869    }
    876870    description = _("Integer")
    877871
    878872    def get_prep_value(self, value):
    879         if value is None:
    880             return None
    881         return int(value)
     873        return self.to_python(value)
    882874
    883875    def get_internal_type(self):
    884876        return "IntegerField"
     
    961953        return super(NullBooleanField, self).get_prep_lookup(lookup_type, value)
    962954
    963955    def get_prep_value(self, value):
    964         if value is None:
    965             return None
    966         return bool(value)
     956        return self.to_python(value)
    967957
    968958    def formfield(self, **kwargs):
    969959        defaults = {
  • django/forms/models.py

     
    999999        for pk in value:
    10001000            try:
    10011001                self.queryset.filter(pk=pk)
    1002             except ValueError:
     1002            except ValidationError:
    10031003                raise ValidationError(self.error_messages['invalid_pk_value'] % pk)
    10041004        qs = self.queryset.filter(pk__in=value)
    10051005        pks = set([force_unicode(o.pk) for o in qs])
  • tests/modeltests/expressions/models.py

     
    127127>>> acme.save()
    128128Traceback (most recent call last):
    129129...
    130 TypeError: int() argument must be a string or a number...
     130ValidationError: [u'This value must be an integer.']
    131131
    132132"""}
  • tests/regressiontests/model_fields/tests.py

     
    2525            ImageFieldUsingFileTests, \
    2626            TwoImageFieldTests
    2727
    28 
     28class AutoFieldTests(django.test.TestCase):
     29    def test_to_python(self):
     30        f = models.AutoField(primary_key=True)
     31        self.assertEqual(f.to_python(3), 3)
     32        self.assertEqual(f.to_python("3"), 3)
     33        self.assertEqual(f.to_python(None), None)
     34        self.assertRaises(ValidationError, f.to_python, "abc")
     35   
     36    def test_get_db_prep_value(self):
     37        f = models.AutoField(primary_key=True)
     38        self.assertEqual(f.get_db_prep_value(3), 3)
     39        self.assertEqual(f.get_db_prep_value("3"), 3)
     40        self.assertEqual(f.get_db_prep_value(None), None)
     41        self.assertRaises(ValidationError, f.get_db_prep_value, "abc")
     42       
    2943class DecimalFieldTests(django.test.TestCase):
    3044    def test_to_python(self):
    3145        f = models.DecimalField(max_digits=4, decimal_places=2)
    3246        self.assertEqual(f.to_python(3), Decimal("3"))
    3347        self.assertEqual(f.to_python("3.14"), Decimal("3.14"))
    34         self.assertRaises(ValidationError, f.to_python, "abc")
     48        self.assertEqual(f.to_python(None), None)
     49        self.assertRaises(ValidationError, f.to_python, "abc")
    3550
     51    def test_get_db_prep_value(self):
     52        f = models.DecimalField(max_digits=4, decimal_places=2)
     53        self.assertEqual(f.get_db_prep_value(3), 3)
     54        self.assertEqual(f.get_db_prep_value("3.14"), Decimal("3.14"))
     55        self.assertEqual(f.to_python(None), None)
     56        self.assertRaises(ValidationError, f.get_db_prep_value, "abc")
     57
    3658    def test_default(self):
    3759        f = models.DecimalField(default=Decimal("0.00"))
    3860        self.assertEqual(f.get_default(), Decimal("0.00"))
     
    7395        # This should not crash. That counts as a win for our purposes.
    7496        Foo.objects.filter(d__gte=100000000000)
    7597
     98class IntegerFieldTests(django.test.TestCase):
     99    def test_to_python(self):
     100        f = models.IntegerField()
     101        self.assertEqual(f.to_python(3), 3)
     102        self.assertEqual(f.to_python("3"), 3)
     103        self.assertEqual(f.to_python(None), None)
     104        self.assertRaises(ValidationError, f.to_python, "abc")
     105   
     106    def test_get_db_prep_value(self):
     107        f = models.IntegerField()
     108        self.assertEqual(f.get_db_prep_value(3), 3)
     109        self.assertEqual(f.get_db_prep_value("3"), 3)
     110        self.assertEqual(f.get_db_prep_value(None), None)
     111        self.assertRaises(ValidationError, f.get_db_prep_value, "abc")
     112
     113class FloatFieldTests(django.test.TestCase):
     114    def test_to_python(self):
     115        f = models.FloatField()
     116        self.assertEqual(f.to_python(3), float("3"))
     117        self.assertEqual(f.to_python("3.14"), float("3.14"))
     118        self.assertEqual(f.to_python(None), None)
     119        self.assertRaises(ValidationError, f.to_python, "abc")
     120
     121    def test_get_db_prep_value(self):
     122        f = models.FloatField()
     123        self.assertEqual(f.get_db_prep_value(3), float(3))
     124        self.assertEqual(f.get_db_prep_value("3.14"), float("3.14"))
     125        self.assertEqual(f.to_python(None), None)
     126        self.assertRaises(ValidationError, f.get_db_prep_value, "abc")
     127
    76128class ForeignKeyTests(django.test.TestCase):
    77129    def test_callable_default(self):
    78130        """Test the use of a lazy callable for ForeignKey.default"""
     
    106158        self.assertEqual(f.get_db_prep_lookup('exact', False, connection=connection), [False])
    107159        self.assertEqual(f.get_db_prep_lookup('exact', '0', connection=connection), [False])
    108160        self.assertEqual(f.get_db_prep_lookup('exact', 0, connection=connection), [False])
    109         self.assertEqual(f.get_db_prep_lookup('exact', None, connection=connection), [None])
    110161
     162
    111163    def _test_to_python(self, f):
    112164        self.assertTrue(f.to_python(1) is True)
    113165        self.assertTrue(f.to_python(0) is False)
    114166
    115167    def test_booleanfield_get_db_prep_lookup(self):
     168        from django.db import connection
    116169        self._test_get_db_prep_lookup(models.BooleanField())
     170        self.assertRaises(ValidationError, models.BooleanField().get_db_prep_lookup, 'exact', None)
    117171
    118172    def test_nullbooleanfield_get_db_prep_lookup(self):
     173        from django.db import connection
    119174        self._test_get_db_prep_lookup(models.NullBooleanField())
     175        self.assertEqual(models.NullBooleanField().get_db_prep_lookup('exact', None, connection=connection), [None])
    120176
    121177    def test_booleanfield_to_python(self):
    122178        self._test_to_python(models.BooleanField())
Back to Top