summaryrefslogtreecommitdiff
path: root/mirrors/models.py
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-02-27 12:31:06 -0600
committerDan McGee <dan@archlinux.org>2011-02-27 12:31:12 -0600
commitdb1524fd64e8b5c0f43cfed7643034ee764f55fd (patch)
tree8615f3dff8c067379cadb9b25276bdd3caae0dd7 /mirrors/models.py
parentb86b71f97d2c53e7f74ab5b048e5603b495601ad (diff)
Auto map the protocol URL field
And perform better validation when doing so. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'mirrors/models.py')
-rw-r--r--mirrors/models.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/mirrors/models.py b/mirrors/models.py
index e070b1cd..b763fb63 100644
--- a/mirrors/models.py
+++ b/mirrors/models.py
@@ -1,4 +1,7 @@
from django.db import models
+from django.core.exceptions import ValidationError
+
+from urlparse import urlparse
TIER_CHOICES = (
(0, 'Tier 0'),
@@ -50,11 +53,20 @@ class MirrorProtocol(models.Model):
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)
+ 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)
+
def __unicode__(self):
return self.url