From 1a63092981c685214ea189a14007da7bcc823c17 Mon Sep 17 00:00:00 2001 From: Erik Romijn Date: Sat, 28 Sep 2013 10:22:46 +0200 Subject: [PATCH] Fixed #20439 -- Started deprecation of IPAddressField --- django/db/models/fields/__init__.py | 2 ++ django/forms/fields.py | 6 ++++++ docs/internals/deprecation.txt | 2 ++ docs/ref/forms/fields.txt | 4 ++++ docs/ref/models/fields.txt | 4 ++++ docs/releases/1.7.txt | 9 +++++++++ tests/field_deconstruction/tests.py | 5 ++++- tests/forms_tests/tests/test_error_messages.py | 6 +++++- tests/forms_tests/tests/test_extra.py | 9 +++++++-- tests/inspectdb/models.py | 5 ++++- tests/model_fields/models.py | 5 ++++- tests/model_fields/tests.py | 9 ++++++--- tests/serializers_regress/models.py | 9 +++++++-- tests/string_lookup/models.py | 5 ++++- 14 files changed, 68 insertions(+), 12 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index e019855826..254e6854df 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1312,6 +1312,8 @@ class IPAddressField(Field): description = _("IPv4 address") def __init__(self, *args, **kwargs): + warnings.warn("IPAddressField has been deprecated. Use GenericIPAddressField instead.", + PendingDeprecationWarning) kwargs['max_length'] = 15 Field.__init__(self, *args, **kwargs) diff --git a/django/forms/fields.py b/django/forms/fields.py index 7e4e5df833..4e0ee29938 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -9,6 +9,7 @@ import datetime import os import re import sys +import warnings from decimal import Decimal, DecimalException from io import BytesIO @@ -1144,6 +1145,11 @@ class SplitDateTimeField(MultiValueField): class IPAddressField(CharField): default_validators = [validators.validate_ipv4_address] + def __init__(self, *args, **kwargs): + warnings.warn("IPAddressField has been deprecated. Use GenericIPAddressField instead.", + PendingDeprecationWarning) + super(IPAddressField, self).__init__(*args, **kwargs) + def to_python(self, value): if value in self.empty_values: return '' diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index c91ea2d218..5c1b1cab6f 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -436,6 +436,8 @@ these changes. :ref:`initial SQL data` in ``myapp/models/sql/``. Move your custom SQL files to ``myapp/sql/``. +* The model and form ``IPAddressField`` will be removed. + * FastCGI support via the ``runfcgi`` management command will be removed. Please deploy your project using WSGI. diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index 0e4d352132..81b65d04d4 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -657,6 +657,10 @@ For each field, we describe the default widget used if you don't specify .. class:: IPAddressField(**kwargs) + .. deprecated:: 1.7 + This field has been deprecated in favour of + :class:`~django.forms.GenericIPAddressField`. + * Default widget: :class:`TextInput` * Empty value: ``''`` (an empty string) * Normalizes to: A Unicode object. diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 1e3476166f..248c630437 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -849,6 +849,10 @@ An integer. The default form widget for this field is a .. class:: IPAddressField([**options]) +.. deprecated:: 1.7 + This field has been deprecated in favour of + :class:`~django.db.models.GenericIPAddressField`. + An IP address, in string format (e.g. "192.0.2.30"). The default form widget for this field is a :class:`~django.forms.TextInput`. diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 8dc17761b9..573ad49dc2 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -529,3 +529,12 @@ to ``utils.py`` in an effort to unify all util and utils references: ``ModelAdmin.get_formsets`` has been deprecated in favor of the new :meth:`~django.contrib.admin.ModelAdmin.get_formsets_with_inlines`, in order to better handle the case of selecting showing inlines on a ``ModelAdmin``. + +``IPAddressField`` +~~~~~~~~~~~~~~~~~~ + +The :class:`django.db.models.IPAddressField` and +:class:`django.forms.IPAddressField` fields have been deprecated in favor of +:class:`django.db.models.GenericIPAddressField` and +:class:`django.forms.GenericIPAddressField`. + diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py index 4ccf4d048c..ba09671a1e 100644 --- a/tests/field_deconstruction/tests.py +++ b/tests/field_deconstruction/tests.py @@ -1,3 +1,4 @@ +import warnings from django.test import TestCase from django.db import models @@ -173,7 +174,9 @@ class FieldDeconstructionTests(TestCase): self.assertEqual(kwargs, {}) def test_ip_address_field(self): - field = models.IPAddressField() + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + field = models.IPAddressField() name, path, args, kwargs = field.deconstruct() self.assertEqual(path, "django.db.models.IPAddressField") self.assertEqual(args, []) diff --git a/tests/forms_tests/tests/test_error_messages.py b/tests/forms_tests/tests/test_error_messages.py index a884bdc75b..2cfe6f3e36 100644 --- a/tests/forms_tests/tests/test_error_messages.py +++ b/tests/forms_tests/tests/test_error_messages.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import warnings + from django.core.files.uploadedfile import SimpleUploadedFile from django.forms import * from django.test import TestCase @@ -192,7 +194,9 @@ class FormsErrorMessagesTestCase(TestCase, AssertFormErrorsMixin): 'required': 'REQUIRED', 'invalid': 'INVALID IP ADDRESS', } - f = IPAddressField(error_messages=e) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + f = IPAddressField(error_messages=e) self.assertFormErrors(['REQUIRED'], f.clean, '') self.assertFormErrors(['INVALID IP ADDRESS'], f.clean, '127.0.0') diff --git a/tests/forms_tests/tests/test_extra.py b/tests/forms_tests/tests/test_extra.py index d0409155dd..136d11383f 100644 --- a/tests/forms_tests/tests/test_extra.py +++ b/tests/forms_tests/tests/test_extra.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import datetime +import warnings from django.forms import * from django.forms.extras import SelectDateWidget @@ -535,7 +536,9 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): self.assertEqual(f.cleaned_data['field1'], 'some text,JP,2007-04-25 06:24:00') def test_ipaddress(self): - f = IPAddressField() + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + f = IPAddressField() self.assertFormErrors(['This field is required.'], f.clean, '') self.assertFormErrors(['This field is required.'], f.clean, None) self.assertEqual(f.clean(' 127.0.0.1'), '127.0.0.1') @@ -544,7 +547,9 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '1.2.3.4.5') self.assertFormErrors(['Enter a valid IPv4 address.'], f.clean, '256.125.1.5') - f = IPAddressField(required=False) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + f = IPAddressField(required=False) self.assertEqual(f.clean(''), '') self.assertEqual(f.clean(None), '') self.assertEqual(f.clean(' 127.0.0.1'), '127.0.0.1') diff --git a/tests/inspectdb/models.py b/tests/inspectdb/models.py index c25202f248..83cd906a07 100644 --- a/tests/inspectdb/models.py +++ b/tests/inspectdb/models.py @@ -1,5 +1,6 @@ # -*- encoding: utf-8 -*- from __future__ import unicode_literals +import warnings from django.db import models @@ -49,7 +50,9 @@ class ColumnTypes(models.Model): file_path_field = models.FilePathField() float_field = models.FloatField() int_field = models.IntegerField() - ip_address_field = models.IPAddressField() + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + ip_address_field = models.IPAddressField() gen_ip_adress_field = models.GenericIPAddressField(protocol="ipv4") pos_int_field = models.PositiveIntegerField() pos_small_int_field = models.PositiveSmallIntegerField() diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index 8c304cc726..a85dfc4f04 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -1,5 +1,6 @@ import os import tempfile +import warnings from django.core.exceptions import ImproperlyConfigured @@ -85,7 +86,9 @@ class VerboseNameField(models.Model): # Don't want to depend on Pillow/PIL in this test #field_image = models.ImageField("verbose field") field12 = models.IntegerField("verbose field12") - field13 = models.IPAddressField("verbose field13") + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + field13 = models.IPAddressField("verbose field13") field14 = models.GenericIPAddressField("verbose field14", protocol="ipv4") field15 = models.NullBooleanField("verbose field15") field16 = models.PositiveIntegerField("verbose field16") diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index e4cc880bd6..ac7dd0252f 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import datetime from decimal import Decimal import unittest +import warnings from django import test from django import forms @@ -603,9 +604,11 @@ class PromiseTest(test.TestCase): def test_IPAddressField(self): lazy_func = lazy(lambda: '127.0.0.1', six.text_type) - self.assertIsInstance( - IPAddressField().get_prep_value(lazy_func()), - six.text_type) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + self.assertIsInstance( + IPAddressField().get_prep_value(lazy_func()), + six.text_type) def test_GenericIPAddressField(self): lazy_func = lazy(lambda: '127.0.0.1', six.text_type) diff --git a/tests/serializers_regress/models.py b/tests/serializers_regress/models.py index ab3d3063a4..52bc8935ad 100644 --- a/tests/serializers_regress/models.py +++ b/tests/serializers_regress/models.py @@ -4,6 +4,7 @@ A test spanning all the capabilities of all the serializers. This class sets up a model for each model field type (except for image types, because of the Pillow/PIL dependency). """ +import warnings from django.db import models from django.contrib.contenttypes import generic @@ -52,7 +53,9 @@ class BigIntegerData(models.Model): # data = models.ImageField(null=True) class IPAddressData(models.Model): - data = models.IPAddressField(null=True) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + data = models.IPAddressField(null=True) class GenericIPAddressData(models.Model): data = models.GenericIPAddressField(null=True) @@ -199,7 +202,9 @@ class IntegerPKData(models.Model): # data = models.ImageField(primary_key=True) class IPAddressPKData(models.Model): - data = models.IPAddressField(primary_key=True) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + data = models.IPAddressField(primary_key=True) class GenericIPAddressPKData(models.Model): data = models.GenericIPAddressField(primary_key=True) diff --git a/tests/string_lookup/models.py b/tests/string_lookup/models.py index a2d64cd0b2..43ed90a462 100644 --- a/tests/string_lookup/models.py +++ b/tests/string_lookup/models.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import warnings from django.db import models from django.utils.encoding import python_2_unicode_compatible @@ -49,7 +50,9 @@ class Base(models.Model): class Article(models.Model): name = models.CharField(max_length=50) text = models.TextField() - submitted_from = models.IPAddressField(blank=True, null=True) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + submitted_from = models.IPAddressField(blank=True, null=True) def __str__(self): return "Article %s" % self.name