|
2 | 2 |
|
3 | 3 | from django import forms
|
4 | 4 | from django.db.models import Q
|
5 |
| -from django.forms.widgets import SelectMultiple, CheckboxSelectMultiple |
| 5 | +from django.forms.widgets import ( |
| 6 | + Select, |
| 7 | + SelectMultiple, |
| 8 | + CheckboxSelectMultiple, |
| 9 | + TextInput, |
| 10 | + EmailInput |
| 11 | +) |
6 | 12 | from django.shortcuts import get_object_or_404, redirect, render
|
7 | 13 | from django.views.decorators.csrf import csrf_exempt
|
8 | 14 | from django_countries import countries
|
9 | 15 |
|
10 |
| -from ..models import MirrorUrl, MirrorProtocol |
| 16 | +from ..models import Mirror, MirrorUrl, MirrorProtocol |
11 | 17 | from ..utils import get_mirror_statuses
|
12 | 18 |
|
13 | 19 | import random
|
14 | 20 |
|
15 | 21 |
|
| 22 | +# This is populated later, and re-populated every refresh |
| 23 | +# This was the only way to get 3 different examples without |
| 24 | +# changing the models.py |
| 25 | +url_examples = [] |
| 26 | + |
| 27 | + |
| 28 | +class MirrorRequestForm(forms.ModelForm): |
| 29 | + upstream = forms.ModelChoiceField( |
| 30 | + queryset=Mirror.objects.filter(tier__gte=0, tier__lte=1), |
| 31 | + required=False) |
| 32 | + |
| 33 | + class Meta: |
| 34 | + model = Mirror |
| 35 | + fields = ('name', 'tier', 'upstream', 'admin_email', 'alternate_email', |
| 36 | + 'isos', 'rsync_user', 'rsync_password', 'notes') |
| 37 | + |
| 38 | + def __init__(self, *args, **kwargs): |
| 39 | + super(MirrorRequestForm, self).__init__(*args, **kwargs) |
| 40 | + fields = self.fields |
| 41 | + fields['name'].widget.attrs.update({'placeholder': 'Ex: mirror.argentina.co'}) |
| 42 | + fields['rsync_user'].widget.attrs.update({'placeholder': 'Optional'}) |
| 43 | + fields['rsync_password'].widget.attrs.update({'placeholder': 'Optional'}) |
| 44 | + fields['notes'].widget.attrs.update({'placeholder': 'Ex: Hosted by ISP GreatISO.bg'}) |
| 45 | + |
| 46 | + def as_div(self): |
| 47 | + "Returns this form rendered as HTML <divs>s." |
| 48 | + return self._html_output( |
| 49 | + normal_row=u'<div%(html_class_attr)s>%(label)s %(field)s%(help_text)s</div>', |
| 50 | + error_row=u'%s', |
| 51 | + row_ender='</div>', |
| 52 | + help_text_html=u' <span class="helptext">%s</span>', |
| 53 | + errors_on_separate_row=True) |
| 54 | + |
| 55 | + |
| 56 | +class MirrorUrlForm(forms.ModelForm): |
| 57 | + class Meta: |
| 58 | + model = MirrorUrl |
| 59 | + fields = ('url', 'country', 'bandwidth', 'active') |
| 60 | + |
| 61 | + def __init__(self, *args, **kwargs): |
| 62 | + global url_examples |
| 63 | + |
| 64 | + super(MirrorUrlForm, self).__init__(*args, **kwargs) |
| 65 | + fields = self.fields |
| 66 | + |
| 67 | + if len(url_examples) == 0: |
| 68 | + url_examples = [ |
| 69 | + 'Ex: http://mirror.argentina.co/archlinux', |
| 70 | + 'Ex: https://mirror.argentina.co/archlinux', |
| 71 | + 'Ex: rsync://mirror.argentina.co/archlinux' |
| 72 | + ] |
| 73 | + |
| 74 | + fields['url'].widget.attrs.update({'placeholder': url_examples.pop()}) |
| 75 | + |
| 76 | + def clean_url(self): |
| 77 | + # is this a valid-looking URL? |
| 78 | + url_parts = urlparse(self.cleaned_data["url"]) |
| 79 | + if not url_parts.scheme: |
| 80 | + raise forms.ValidationError("No URL scheme (protocol) provided.") |
| 81 | + if not url_parts.netloc: |
| 82 | + raise forms.ValidationError("No URL host provided.") |
| 83 | + if url_parts.params or url_parts.query or url_parts.fragment: |
| 84 | + raise forms.ValidationError( |
| 85 | + "URL parameters, query, and fragment elements are not supported.") |
| 86 | + # ensure we always save the URL with a trailing slash |
| 87 | + path = url_parts.path |
| 88 | + if not path.endswith('/'): |
| 89 | + path += '/' |
| 90 | + url = urlunsplit((url_parts.scheme, url_parts.netloc, path, '', '')) |
| 91 | + return url |
| 92 | + |
| 93 | + def as_div(self): |
| 94 | + "Returns this form rendered as HTML <divs>s." |
| 95 | + return self._html_output( |
| 96 | + normal_row=u'<div%(html_class_attr)s>%(label)s %(field)s%(help_text)s</div>', |
| 97 | + error_row=u'%s', |
| 98 | + row_ender='</div>', |
| 99 | + help_text_html=u' <span class="helptext">%s</span>', |
| 100 | + errors_on_separate_row=True) |
| 101 | + |
| 102 | + |
16 | 103 | class MirrorlistForm(forms.Form):
|
17 | 104 | country = forms.MultipleChoiceField(required=False, widget=SelectMultiple(attrs={'size': '12'}))
|
18 | 105 | protocol = forms.MultipleChoiceField(required=False, widget=CheckboxSelectMultiple)
|
@@ -127,4 +214,33 @@ def find_mirrors_simple(request, protocol):
|
127 | 214 | proto = get_object_or_404(MirrorProtocol, protocol=protocol)
|
128 | 215 | return find_mirrors(request, protocols=[proto])
|
129 | 216 |
|
| 217 | +def submit_mirror(request): |
| 218 | + # if request.method == 'POST' or len(request.GET) > 0: |
| 219 | + # data = request.POST if request.method == 'POST' else request.GET |
| 220 | + # form1 = MirrorUrlForm(data=data) |
| 221 | + # if form.is_valid(): |
| 222 | + # countries = form.cleaned_data['country'] |
| 223 | + # protocols = form.cleaned_data['protocol'] |
| 224 | + # use_status = form.cleaned_data['use_mirror_status'] |
| 225 | + # ipv4 = '4' in form.cleaned_data['ip_version'] |
| 226 | + # ipv6 = '6' in form.cleaned_data['ip_version'] |
| 227 | + # return find_mirrors(request, countries, protocols, |
| 228 | + # use_status, ipv4, ipv6) |
| 229 | + # else: |
| 230 | + form1 = MirrorRequestForm() |
| 231 | + url1 = MirrorUrlForm() |
| 232 | + url2 = MirrorUrlForm() |
| 233 | + url3 = MirrorUrlForm() |
| 234 | + |
| 235 | + return render( |
| 236 | + request, |
| 237 | + 'mirrors/mirror_submit.html', |
| 238 | + { |
| 239 | + 'submission_form1': form1, |
| 240 | + 'url1': url1, |
| 241 | + 'url2': url2, |
| 242 | + 'url3': url3 |
| 243 | + } |
| 244 | + ) |
| 245 | + |
130 | 246 | # vim: set ts=4 sw=4 et:
|
0 commit comments