Ticket #26378: 26378.diff

File 26378.diff, 2.4 KB (added by Amine Yaiche, 8 years ago)
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 333afc5..4e21791 100644
    a b from django.core import checks, exceptions, validators  
    1818# When the _meta object was formalized, this exception was moved to
    1919# django.core.exceptions. It is retained here for backwards compatibility
    2020# purposes.
    21 from django.core.exceptions import FieldDoesNotExist  # NOQA
     21from django.core.exceptions import FieldDoesNotExist, ValidationError  # NOQA
    2222from django.db import connection, connections, router
    2323from django.db.models.query_utils import QueryWrapper, RegisterLookupMixin
    2424from django.utils import six, timezone
    class GenericIPAddressField(Field):  
    19941994        if not isinstance(value, six.string_types):
    19951995            value = force_text(value)
    19961996        value = value.strip()
     1997        if not self.unpack_ipv4 and ':' in value and '.' in value:
     1998            raise ValidationError(_('Mixed IPv4 and IPv6 representation is not allowed when unpack_ipv4 is disabled'),
     1999                                  code='invalid')
    19972000        if ':' in value:
    19982001            return clean_ipv6_address(value,
    19992002                self.unpack_ipv4, self.error_messages['invalid'])
  • tests/model_fields/tests.py

    diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
    index 70f039e..dd99ecc 100644
    a b class GenericIPAddressFieldTests(test.TestCase):  
    904904        with self.assertRaises(ValidationError):
    905905            form_field.clean('127.0.0.1')
    906906
     907    def test_genericipaddressfield_formfield_unpack_ipv4(self):
     908        """
     909        Test that GenericIPAddressField will reject a mixed IPv4 and IPv6 IP adress if unpack_ipv4=False.
     910        See #26378
     911        """
     912        unpack_ip_field = models.GenericIPAddressField(null=False, protocol='both', unpack_ipv4=True)
     913        not_unpack_ip_field = models.GenericIPAddressField(protocol='both', unpack_ipv4=False)
     914        unpack_ip_field.clean('::ffff:0.0.0.0', None)
     915        with self.assertRaises(ValidationError):
     916            not_unpack_ip_field.clean('::ffff:0.0.0.0', None)
     917        with self.assertRaises(ValidationError):
     918            not_unpack_ip_field.clean('::ffff:2.0.0.0', None)
     919
    907920    def test_null_value(self):
    908921        """
    909922        Null values should be resolved to None in Python (#24078).
Back to Top