From 10e97f1799b87ead404e4f50fe93d5c34813efdb Mon Sep 17 00:00:00 2001 From: "John C. Allwein" Date: Sat, 1 Aug 2020 15:17:45 -0400 Subject: [PATCH 1/3] fields: replace IPy dependency with ipaddress module Resolves #229 --- mirrors/fields.py | 3 +-- mirrors/tests/test_mirrorrsync.py | 2 +- requirements.txt | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/mirrors/fields.py b/mirrors/fields.py index ceb20a7e..985c11ec 100644 --- a/mirrors/fields.py +++ b/mirrors/fields.py @@ -1,10 +1,9 @@ -from IPy import IP - from django import forms from django.core import validators from django.core.exceptions import ValidationError from django.db import models +from ipaddress import ip_address as IP class IPNetworkFormField(forms.Field): def to_python(self, value): diff --git a/mirrors/tests/test_mirrorrsync.py b/mirrors/tests/test_mirrorrsync.py index 31dc325d..13e68232 100644 --- a/mirrors/tests/test_mirrorrsync.py +++ b/mirrors/tests/test_mirrorrsync.py @@ -27,4 +27,4 @@ def test_ipv4(self): def test_invalid(self): with self.assertRaises(ValueError) as e: MirrorRsync.objects.create(ip="8.8.8.8.8", mirror=self.mirror) - self.assertIn('IPv4 Address with more than 4 bytes', str(e.exception)) + self.assertIn('does not appear to be an IPv4 or IPv6 address', str(e.exception)) diff --git a/requirements.txt b/requirements.txt index 560edcb6..5a54a9c2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ -e git+git://github.com/fredj/cssmin.git@master#egg=cssmin Django==3.0.9 -IPy==1.00 Markdown==3.2.1 bencode.py==2.0.0 django-countries==5.5 From 4d3fb52274252a7e7f8a788552f3b6b717dfa95e Mon Sep 17 00:00:00 2001 From: "John C. Allwein" Date: Mon, 3 Aug 2020 20:43:39 -0400 Subject: [PATCH 2/3] fields: support subnet masks via ip_address --- mirrors/fields.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mirrors/fields.py b/mirrors/fields.py index 985c11ec..3216d831 100644 --- a/mirrors/fields.py +++ b/mirrors/fields.py @@ -3,7 +3,12 @@ from django.core.exceptions import ValidationError from django.db import models -from ipaddress import ip_address as IP +from ipaddress import ip_address, ip_interface + +def IP(address): + if '/' not in address: + return ip_address(address) + return ip_interface(address) class IPNetworkFormField(forms.Field): def to_python(self, value): From 6c2ddb76a299d67f6751ef6d5af548182c9b6a13 Mon Sep 17 00:00:00 2001 From: "John C. Allwein" Date: Mon, 3 Aug 2020 20:53:03 -0400 Subject: [PATCH 3/3] test_mirrorrsync: add test for ipv6, ipv4 masked ips --- mirrors/tests/test_mirrorrsync.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mirrors/tests/test_mirrorrsync.py b/mirrors/tests/test_mirrorrsync.py index 13e68232..e4fa0ad7 100644 --- a/mirrors/tests/test_mirrorrsync.py +++ b/mirrors/tests/test_mirrorrsync.py @@ -4,7 +4,9 @@ TEST_IPV6 = "2a0b:4342:1a31:410::" +TEST_IPV6_MASK = "2a0b:4342:1a31:410::/64" TEST_IPV4 = "8.8.8.8" +TEST_IPV4_MASK = "192.168.1.0/24" class MirrorRsyncTest(TransactionTestCase): def setUp(self): @@ -19,11 +21,21 @@ def test_ipv6(self): self.assertEqual(str(mirrorrsync), TEST_IPV6) mirrorrsync.delete() + def test_ipv6_mask(self): + mirrorsync = MirrorRsync.objects.create(ip=TEST_IPV6_MASK, mirror=self.mirror) + self.assertEqual(str(mirrorsync), TEST_IPV6_MASK) + mirrorsync.delete() + def test_ipv4(self): mirrorrsync = MirrorRsync.objects.create(ip=TEST_IPV4, mirror=self.mirror) self.assertEqual(str(mirrorrsync), TEST_IPV4) mirrorrsync.delete() + def test_ipv4_mask(self): + mirrorrsync = MirrorRsync.objects.create(ip=TEST_IPV4_MASK, mirror=self.mirror) + self.assertEqual(str(mirrorrsync), TEST_IPV4_MASK) + mirrorrsync.delete() + def test_invalid(self): with self.assertRaises(ValueError) as e: MirrorRsync.objects.create(ip="8.8.8.8.8", mirror=self.mirror)