Ticket #11716: 11716.diff

File 11716.diff, 12.9 KB (added by Tim Graham, 14 years ago)

updated patch to trunk + tests passing

  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index 0f567bd..9f99c5d 100644
    a b from django.utils.translation import ugettext as _  
    1313from django.utils.safestring import mark_safe
    1414from django.utils.encoding import force_unicode
    1515from django.conf import settings
     16from django.core.exceptions import ValidationError
    1617from django.core.urlresolvers import reverse, NoReverseMatch
    1718
    1819class FilteredSelectMultiple(forms.SelectMultiple):
    class ForeignKeyRawIdWidget(forms.TextInput):  
    147148        try:
    148149            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
    149150            return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))
    150         except (ValueError, self.rel.to.DoesNotExist):
     151        except (ValidationError, ValueError, self.rel.to.DoesNotExist):
    151152            return ''
    152153
    153154class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
  • django/contrib/comments/views/utils.py

    diff --git a/django/contrib/comments/views/utils.py b/django/contrib/comments/views/utils.py
    index 8b729d2..458da03 100644
    a b from django.http import HttpResponseRedirect  
    88from django.core import urlresolvers
    99from django.shortcuts import render_to_response
    1010from django.template import RequestContext
    11 from django.core.exceptions import ObjectDoesNotExist
     11from django.core.exceptions import ObjectDoesNotExist, ValidationError
    1212from django.contrib import comments
    1313
    1414def next_redirect(data, default, default_view, **get_kwargs):
    def confirmation_view(template, doc="Display a confirmation view."):  
    3939        if 'c' in request.GET:
    4040            try:
    4141                comment = comments.get_model().objects.get(pk=request.GET['c'])
    42             except (ObjectDoesNotExist, ValueError):
     42            except (ObjectDoesNotExist, ValidationError):
    4343                pass
    4444        return render_to_response(template,
    4545            {'comment': comment},
  • django/contrib/contenttypes/views.py

    diff --git a/django/contrib/contenttypes/views.py b/django/contrib/contenttypes/views.py
    index ac0feff..5163f21 100644
    a b  
    11from django import http
    22from django.contrib.contenttypes.models import ContentType
    33from django.contrib.sites.models import Site, get_current_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."
    def shortcut(request, content_type_id, object_id):  
    1111        if not content_type.model_class():
    1212            raise http.Http404("Content type %s object has no associated model" % content_type_id)
    1313        obj = content_type.get_object_for_this_type(pk=object_id)
    14     except (ObjectDoesNotExist, ValueError):
     14    except (ObjectDoesNotExist, ValidationError):
    1515        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
    1616    try:
    1717        absurl = obj.get_absolute_url()
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index fd0a295..3831bb2 100644
    a b class AutoField(Field):  
    474474        pass
    475475
    476476    def get_prep_value(self, value):
    477         if value is None:
    478             return None
    479         return int(value)
     477        return self.to_python(value)
    480478
    481479    def contribute_to_class(self, cls, name):
    482480        assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
    class BooleanField(Field):  
    523521        return super(BooleanField, self).get_prep_lookup(lookup_type, value)
    524522
    525523    def get_prep_value(self, value):
    526         if value is None:
    527             return None
    528         return bool(value)
     524        return self.to_python(value)
    529525
    530526    def formfield(self, **kwargs):
    531527        # Unlike most fields, BooleanField figures out include_blank from
    class FloatField(Field):  
    843839    description = _("Floating point number")
    844840
    845841    def get_prep_value(self, value):
    846         if value is None:
    847             return None
    848         return float(value)
     842        return self.to_python(value)
    849843
    850844    def get_internal_type(self):
    851845        return "FloatField"
    class IntegerField(Field):  
    871865    description = _("Integer")
    872866
    873867    def get_prep_value(self, value):
    874         if value is None:
    875             return None
    876         return int(value)
     868        return self.to_python(value)
    877869
    878870    def get_prep_lookup(self, lookup_type, value):
    879871        if (lookup_type == 'gte' or lookup_type == 'lt') \
    class NullBooleanField(Field):  
    963955        return super(NullBooleanField, self).get_prep_lookup(lookup_type, value)
    964956
    965957    def get_prep_value(self, value):
    966         if value is None:
    967             return None
    968         return bool(value)
     958        return self.to_python(value)
    969959
    970960    def formfield(self, **kwargs):
    971961        defaults = {
    class XMLField(TextField):  
    11381128    def __init__(self, verbose_name=None, name=None, schema_path=None, **kwargs):
    11391129        self.schema_path = schema_path
    11401130        Field.__init__(self, verbose_name, name, **kwargs)
    1141 
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 2f41dbf..15faa5b 100644
    a b class ModelMultipleChoiceField(ModelChoiceField):  
    10401040        for pk in value:
    10411041            try:
    10421042                self.queryset.filter(pk=pk)
    1043             except ValueError:
     1043            except ValidationError:
    10441044                raise ValidationError(self.error_messages['invalid_pk_value'] % pk)
    10451045        qs = self.queryset.filter(pk__in=value)
    10461046        pks = set([force_unicode(o.pk) for o in qs])
  • tests/modeltests/expressions/tests.py

    diff --git a/tests/modeltests/expressions/tests.py b/tests/modeltests/expressions/tests.py
    index 0a136ae..7305ffa 100644
    a b  
    1 from django.core.exceptions import FieldError
     1from django.core.exceptions import FieldError, ValidationError
    22from django.db.models import F
    33from django.test import TestCase
    44
    class ExpressionsTests(TestCase):  
    215215            ceo=test_gmbh.ceo
    216216        )
    217217        acme.num_employees = F("num_employees") + 16
    218         self.assertRaises(TypeError, acme.save)
     218        self.assertRaises(ValidationError, acme.save)
  • tests/regressiontests/admin_widgets/tests.py

    diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
    index 4f9abb6..14945a0 100644
    a b class AdminForeignKeyRawIdWidget(DjangoTestCase):  
    170170            'Select a valid choice. That choice is not one of the available choices.')
    171171
    172172    def test_invalid_target_id(self):
     173        response = self.client.post('%s/admin_widgets/event/add/' % self.admin_root,
     174            {"band": -1234})
     175        self.assertContains(response,
     176            'Select a valid choice. That choice is not one of the available choices.')
    173177
    174         for test_str in ('Iñtërnâtiônàlizætiøn', "1234'", -1234):
     178        for test_str in ('Iñtërnâtiônàlizætiøn', "1234'"):
    175179            # This should result in an error message, not a server exception.
    176180            response = self.client.post('%s/admin_widgets/event/add/' % self.admin_root,
    177181                {"band": test_str})
    178182
    179             self.assertContains(response,
    180                 'Select a valid choice. That choice is not one of the available choices.')
     183            self.assertContains(response, 'This value must be an integer.')
    181184
    182185
    183186class FilteredSelectMultipleWidgetTest(TestCase):
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index a0b4593..5d1c3f1 100644
    a b class BasicFieldTests(test.TestCase):  
    4848        except ValidationError, e:
    4949            self.fail("NullBooleanField failed validation with value of None: %s" % e.messages)
    5050
     51class AutoFieldTests(test.TestCase):
     52    def test_to_python(self):
     53        f = models.AutoField(primary_key=True)
     54        self.assertEqual(f.to_python(3), 3)
     55        self.assertEqual(f.to_python("3"), 3)
     56        self.assertEqual(f.to_python(None), None)
     57        self.assertRaises(ValidationError, f.to_python, "abc")
     58
     59    def test_get_db_prep_value(self):
     60        from django.db import connection
     61        f = models.AutoField(primary_key=True)
     62        self.assertEqual(f.get_db_prep_value(3, connection=connection), 3)
     63        self.assertEqual(f.get_db_prep_value("3", connection=connection), 3)
     64        self.assertEqual(f.get_db_prep_value(None, connection=connection), None)
     65        self.assertRaises(ValidationError, f.get_db_prep_value, "abc", connection=connection)
     66
    5167class DecimalFieldTests(test.TestCase):
    5268    def test_to_python(self):
    5369        f = models.DecimalField(max_digits=4, decimal_places=2)
    5470        self.assertEqual(f.to_python(3), Decimal("3"))
    5571        self.assertEqual(f.to_python("3.14"), Decimal("3.14"))
     72        self.assertEqual(f.to_python(None), None)
    5673        self.assertRaises(ValidationError, f.to_python, "abc")
    5774
     75    def test_get_db_prep_value(self):
     76        from django.db import connection
     77        f = models.DecimalField(max_digits=4, decimal_places=2)
     78        self.assertEqual(f.get_db_prep_value(3, connection=connection), 3)
     79        self.assertEqual(f.get_db_prep_value("3.14", connection=connection), Decimal("3.14"))
     80        self.assertEqual(f.to_python(None), None)
     81        self.assertRaises(ValidationError, f.get_db_prep_value, "abc", connection=connection)
     82
    5883    def test_default(self):
    5984        f = models.DecimalField(default=Decimal("0.00"))
    6085        self.assertEqual(f.get_default(), Decimal("0.00"))
    class BooleanFieldTests(unittest.TestCase):  
    128153        self.assertEqual(f.get_db_prep_lookup('exact', False, connection=connection), [False])
    129154        self.assertEqual(f.get_db_prep_lookup('exact', '0', connection=connection), [False])
    130155        self.assertEqual(f.get_db_prep_lookup('exact', 0, connection=connection), [False])
    131         self.assertEqual(f.get_db_prep_lookup('exact', None, connection=connection), [None])
     156
    132157
    133158    def _test_to_python(self, f):
    134159        self.assertTrue(f.to_python(1) is True)
    135160        self.assertTrue(f.to_python(0) is False)
    136161
    137162    def test_booleanfield_get_db_prep_lookup(self):
     163        from django.db import connection
    138164        self._test_get_db_prep_lookup(models.BooleanField())
     165        self.assertRaises(ValidationError, models.BooleanField().get_db_prep_lookup, 'exact', None, connection=connection)
    139166
    140167    def test_nullbooleanfield_get_db_prep_lookup(self):
     168        from django.db import connection
    141169        self._test_get_db_prep_lookup(models.NullBooleanField())
     170        self.assertEqual(models.NullBooleanField().get_db_prep_lookup('exact', None, connection=connection), [None])
    142171
    143172    def test_booleanfield_to_python(self):
    144173        self._test_to_python(models.BooleanField())
    class ValidationTest(test.TestCase):  
    270299        f = models.BooleanField()
    271300        self.assertRaises(ValidationError, f.clean, None, None)
    272301
     302class IntegerFieldTests(test.TestCase):
     303    def test_to_python(self):
     304        f = models.IntegerField()
     305        self.assertEqual(f.to_python(3), 3)
     306        self.assertEqual(f.to_python("3"), 3)
     307        self.assertEqual(f.to_python(None), None)
     308        self.assertRaises(ValidationError, f.to_python, "abc")
     309
     310    def test_get_db_prep_value(self):
     311        from django.db import connection
     312        f = models.IntegerField()
     313        self.assertEqual(f.get_db_prep_value(3, connection=connection), 3)
     314        self.assertEqual(f.get_db_prep_value("3", connection=connection), 3)
     315        self.assertEqual(f.get_db_prep_value(None, connection=connection), None)
     316        self.assertRaises(ValidationError, f.get_db_prep_value, "abc", connection=connection)
     317
     318class FloatFieldTests(test.TestCase):
     319    def test_to_python(self):
     320        f = models.FloatField()
     321        self.assertEqual(f.to_python(3), float("3"))
     322        self.assertEqual(f.to_python("3.14"), float("3.14"))
     323        self.assertEqual(f.to_python(None), None)
     324        self.assertRaises(ValidationError, f.to_python, "abc")
     325
     326    def test_get_db_prep_value(self):
     327        from django.db import connection
     328        f = models.FloatField()
     329        self.assertEqual(f.get_db_prep_value(3, connection=connection), float(3))
     330        self.assertEqual(f.get_db_prep_value("3.14", connection=connection), float("3.14"))
     331        self.assertEqual(f.to_python(None), None)
     332        self.assertRaises(ValidationError, f.get_db_prep_value, "abc", connection=connection)
    273333
    274334class BigIntegerFieldTests(test.TestCase):
    275335    def test_limits(self):
Back to Top