diff options
author | Dan McGee <dan@archlinux.org> | 2011-02-27 12:31:06 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-02-27 12:31:12 -0600 |
commit | db1524fd64e8b5c0f43cfed7643034ee764f55fd (patch) | |
tree | 8615f3dff8c067379cadb9b25276bdd3caae0dd7 /mirrors/admin.py | |
parent | b86b71f97d2c53e7f74ab5b048e5603b495601ad (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/admin.py')
-rw-r--r-- | mirrors/admin.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/mirrors/admin.py b/mirrors/admin.py index 6990cca2..f6a72cf0 100644 --- a/mirrors/admin.py +++ b/mirrors/admin.py @@ -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 |