Auto map the protocol URL field

And perform better validation when doing so.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-02-27 12:31:06 -06:00
parent b86b71f97d
commit db1524fd64
2 changed files with 29 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import re
from urlparse import urlparse, urlunsplit
from django import forms
from django.contrib import admin
@ -9,15 +10,26 @@ class MirrorUrlForm(forms.ModelForm):
class Meta:
model = MirrorUrl
def clean_url(self):
# is this a valid-looking URL?
url_parts = urlparse(self.cleaned_data["url"])
if not url_parts.scheme:
raise forms.ValidationError("No URL scheme (protocol) provided.")
if not url_parts.netloc:
raise forms.ValidationError("No URL host provided.")
if url_parts.params or url_parts.query or url_parts.fragment:
raise forms.ValidationError(
"URL parameters, query, and fragment elements are not supported.")
# ensure we always save the URL with a trailing slash
url = self.cleaned_data["url"].strip()
if url[-1] == '/':
return url
return url + '/'
path = url_parts.path
if not path.endswith('/'):
path += '/'
url = urlunsplit((url_parts.scheme, url_parts.netloc, path, '', ''))
return url
class MirrorUrlInlineAdmin(admin.TabularInline):
model = MirrorUrl
form = MirrorUrlForm
readonly_fields = ('protocol',)
extra = 3
# ripped off from django.forms.fields, adding netmask ability

View File

@ -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 Meta:
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