Ticket #16302: notes-update-comments-ip-address-with-tests.patch
File notes-update-comments-ip-address-with-tests.patch, 9.2 KB (added by , 13 years ago) |
---|
-
docs/releases/1.4.txt
341 341 a :class:`~django.db.models.fields.GenericIPAddressField` model field, 342 342 a :class:`~django.forms.fields.GenericIPAddressField` form field and 343 343 the validators :data:`~django.core.validators.validate_ipv46_address` and 344 :data:`~django.core.validators.validate_ipv6_address` 344 :data:`~django.core.validators.validate_ipv6_address`. 345 345 346 The :class:`~django.contrib.comments.models.Comment` model formerly used a 347 :class:`~django.db.models.fields.IPAddressField` to store the ip_address of 348 comment submitters. This has been updated to use the new IPv4 and IPv6 capable 349 ``GenericIPAddressField``. Unfortunately, the old type would silently truncate 350 IPv6 addresses longer than 15 characters for users of databases not having a 351 native IP address type (this does not apply to PostgreSQL). It is recommended 352 that users of the comments application resize the 353 ``django_comments.ip_address`` column in affected databases to 39 characters. 354 346 355 Updated default project layout and ``manage.py`` 347 356 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 348 357 -
tests/modeltests/validation/models.py
88 88 generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True) 89 89 v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4") 90 90 v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6") 91 ip_verbose_name = models.GenericIPAddressField("IP Address Verbose", 92 blank=True, null=True) 91 93 92 94 class GenericIPAddrUnpackUniqueTest(models.Model): 93 95 generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True) … … 102 104 auto2 = models.AutoField(primary_key=True) 103 105 except AssertionError, assertion_error: 104 106 pass # Fail silently 105 assert str(assertion_error) == u"A model can't have more than one AutoField." 106 No newline at end of file 107 assert str(assertion_error) == u"A model can't have more than one AutoField." -
django/db/models/fields/__init__.py
1010 1010 description = _("IP address") 1011 1011 default_error_messages = {} 1012 1012 1013 def __init__(self, protocol='both', unpack_ipv4=False, *args, **kwargs): 1013 def __init__(self, verbose_name=None, name=None, protocol='both', 1014 unpack_ipv4=False, **kwargs): 1014 1015 self.unpack_ipv4 = unpack_ipv4 1015 1016 self.default_validators, invalid_error_message = \ 1016 1017 validators.ip_address_validators(protocol, unpack_ipv4) 1017 1018 self.default_error_messages['invalid'] = invalid_error_message 1018 1019 kwargs['max_length'] = 39 1019 Field.__init__(self, *args, **kwargs)1020 Field.__init__(self, verbose_name, name, **kwargs) 1020 1021 1021 1022 def get_internal_type(self): 1022 1023 return "GenericIPAddressField" -
django/contrib/comments/models.py
57 57 58 58 # Metadata about the comment 59 59 submit_date = models.DateTimeField(_('date/time submitted'), default=None) 60 ip_address = models.IPAddressField(_('IP address'), blank=True, null=True) 60 ip_address = models.GenericIPAddressField(_('IP address'), 61 unpack_ipv4=True, blank=True, null=True) 61 62 is_public = models.BooleanField(_('is public'), default=True, 62 63 help_text=_('Uncheck this box to make the comment effectively ' \ 63 64 'disappear from the site.')) -
tests/modeltests/validation/models.py
88 88 generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True) 89 89 v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4") 90 90 v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6") 91 ip_verbose_name = models.GenericIPAddressField("IP Address Verbose", 92 blank=True, null=True) 91 93 92 94 class GenericIPAddrUnpackUniqueTest(models.Model): 93 95 generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True) … … 102 104 auto2 = models.AutoField(primary_key=True) 103 105 except AssertionError, assertion_error: 104 106 pass # Fail silently 105 assert str(assertion_error) == u"A model can't have more than one AutoField." 106 No newline at end of file 107 assert str(assertion_error) == u"A model can't have more than one AutoField." -
tests/regressiontests/comment_tests/tests/comment_view_tests.py
101 101 settings.DEBUG = olddebug 102 102 103 103 def testCreateValidComment(self): 104 address = "1.2.3.4" 104 105 a = Article.objects.get(pk=1) 105 106 data = self.getValidData(a) 106 self.response = self.client.post("/post/", data, REMOTE_ADDR= "1.2.3.4")107 self.response = self.client.post("/post/", data, REMOTE_ADDR=address) 107 108 self.assertEqual(self.response.status_code, 302) 108 109 self.assertEqual(Comment.objects.count(), 1) 109 110 c = Comment.objects.all()[0] 110 self.assertEqual(c.ip_address, "1.2.3.4")111 self.assertEqual(c.ip_address, address) 111 112 self.assertEqual(c.comment, "This is my comment") 112 113 114 def testCreateValidCommentIPv6(self): 115 address = "2a02::223:6cff:fe8a:2e8a" 116 a = Article.objects.get(pk=1) 117 data = self.getValidData(a) 118 self.response = self.client.post("/post/", data, REMOTE_ADDR=address) 119 self.assertEqual(self.response.status_code, 302) 120 self.assertEqual(Comment.objects.count(), 1) 121 c = Comment.objects.all()[0] 122 self.assertEqual(c.ip_address, address) 123 self.assertEqual(c.comment, "This is my comment") 124 125 def testCreateValidCommentIPv6Unpack(self): 126 address = "::ffff:18.52.18.52" 127 a = Article.objects.get(pk=1) 128 data = self.getValidData(a) 129 self.response = self.client.post("/post/", data, REMOTE_ADDR=address) 130 self.assertEqual(self.response.status_code, 302) 131 self.assertEqual(Comment.objects.count(), 1) 132 c = Comment.objects.all()[0] 133 # We trim the '::ffff:' bit off because it is an IPv4 addr 134 self.assertEqual(c.ip_address, address[7:]) 135 self.assertEqual(c.comment, "This is my comment") 136 113 137 def testPostAsAuthenticatedUser(self): 114 138 a = Article.objects.get(pk=1) 115 139 data = self.getValidData(a) -
tests/regressiontests/model_fields/tests.py
11 11 from django.utils import unittest 12 12 13 13 from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, 14 NullBooleanModel, BooleanModel, Document, RenamedField )14 NullBooleanModel, BooleanModel, Document, RenamedField, VerboseNameField) 15 15 16 16 # If PIL available, do these tests. 17 17 if Image: … … 65 65 self.assertTrue(hasattr(instance, 'get_fieldname_display')) 66 66 self.assertFalse(hasattr(instance, 'get_modelname_display')) 67 67 68 def test_field_verbose_name(self): 69 m = VerboseNameField 70 for i in range(1, 9): 71 self.assertEqual(m._meta.get_field('field%d' % i).verbose_name, 72 'verbose field%d' % i) 73 68 74 class DecimalFieldTests(test.TestCase): 69 75 def test_to_python(self): 70 76 f = models.DecimalField(max_digits=4, decimal_places=2) -
tests/regressiontests/model_fields/models.py
69 69 class RenamedField(models.Model): 70 70 modelname = models.IntegerField(name="fieldname", choices=((1,'One'),)) 71 71 72 class VerboseNameField(models.Model): 73 field1 = models.BooleanField("verbose field1") 74 field2 = models.DateField("verbose field2") 75 field3 = models.DateTimeField("verbose field3") 76 field4 = models.TimeField("verbose field4") 77 field5 = models.DecimalField("verbose field5", max_digits=6, decimal_places=1) 78 field6 = models.FilePathField("verbose field6") 79 field7 = models.GenericIPAddressField("verbose field7", protocol="ipv4") 80 field8 = models.URLField("verbose field8") 81 72 82 # This model isn't used in any test, just here to ensure it validates successfully. 73 83 # See ticket #16570. 74 84 class DecimalLessThanOne(models.Model):