summaryrefslogtreecommitdiff
path: root/mirrors/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'mirrors/models.py')
-rw-r--r--mirrors/models.py40
1 files changed, 37 insertions, 3 deletions
diff --git a/mirrors/models.py b/mirrors/models.py
index e070b1cd..401821a8 100644
--- a/mirrors/models.py
+++ b/mirrors/models.py
@@ -1,4 +1,8 @@
from django.db import models
+from django.core.exceptions import ValidationError
+
+import socket
+from urlparse import urlparse
TIER_CHOICES = (
(0, 'Tier 0'),
@@ -47,13 +51,43 @@ class MirrorProtocol(models.Model):
class Meta:
verbose_name = 'Mirror Protocol'
+ ordering = ('protocol',)
class MirrorUrl(models.Model):
url = models.CharField(max_length=255)
- protocol = models.ForeignKey(MirrorProtocol, related_name="urls")
+ protocol = models.ForeignKey(MirrorProtocol, related_name="urls",
+ editable=False)
mirror = models.ForeignKey(Mirror, related_name="urls")
- has_ipv4 = models.BooleanField("IPv4 capable", default=True)
- has_ipv6 = models.BooleanField("IPv6 capable", default=False)
+ has_ipv4 = models.BooleanField("IPv4 capable", default=True,
+ editable=False)
+ has_ipv6 = models.BooleanField("IPv6 capable", default=False,
+ editable=False)
+
+ def address_families(self):
+ hostname = urlparse(self.url).hostname
+ info = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM)
+ families = [x[0] for x in info]
+ return families
+
+ @property
+ def hostname(self):
+ return urlparse(self.url).hostname
+
+ def clean(self):
+ try:
+ # Auto-map the protocol field by looking at the URL
+ protocol = urlparse(self.url).scheme
+ self.protocol = MirrorProtocol.objects.get(protocol=protocol)
+ except Exception as e:
+ raise ValidationError(e)
+ try:
+ families = self.address_families()
+ self.has_ipv4 = socket.AF_INET in families
+ self.has_ipv6 = socket.AF_INET6 in families
+ except socket.error as e:
+ # We don't fail in this case; we'll just set both to False
+ self.has_ipv4 = False
+ self.has_ipv6 = False
def __unicode__(self):
return self.url