summaryrefslogtreecommitdiff
path: root/mirrors/views.py
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2010-07-02 19:24:54 -0500
committerDan McGee <dan@archlinux.org>2010-07-02 19:24:54 -0500
commitd29d23ecf39fe1ef4a25702618a6ab710ce191b7 (patch)
tree50c507132f02d36d35b8e2ec83af737838f75572 /mirrors/views.py
parent1a945dd13731ef8703b4124fda19b80f756fd5fa (diff)
Get mirrorlist view ready for general use
Make the page much more flexible- allow multiple countries to be selected rather than just one in the form. Also add a lot more text to the page, and move the 'all' option out into its own subheading rather than being in the same form. Both GET and POST requests are now allowed for ease of use from non-browser scenarios or those that wish to update their mirrorlist automatically and submit parameters to the URL. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'mirrors/views.py')
-rw-r--r--mirrors/views.py36
1 files changed, 17 insertions, 19 deletions
diff --git a/mirrors/views.py b/mirrors/views.py
index a67a2d7c..fbff697d 100644
--- a/mirrors/views.py
+++ b/mirrors/views.py
@@ -1,50 +1,48 @@
from django import forms
-from django.core.urlresolvers import reverse
from django.db.models import Q
-from django.http import HttpResponseRedirect
-from django.shortcuts import get_object_or_404, render_to_response
+from django.shortcuts import render_to_response
from django.template import RequestContext
+from django.views.decorators.csrf import csrf_exempt
from main.models import Mirror, MirrorUrl
from main.utils import make_choice
class MirrorlistForm(forms.Form):
- country = forms.ChoiceField(required=False)
+ country = forms.MultipleChoiceField(required=False)
def __init__(self, *args, **kwargs):
super(MirrorlistForm, self).__init__(*args, **kwargs)
- mirrors = Mirror.objects.values_list(
+ mirrors = Mirror.objects.filter(active=True).values_list(
'country', flat=True).distinct().order_by('country')
- self.fields['country'].choices = [('all', 'All')] + make_choice(
+ self.fields['country'].choices = make_choice(
[mirror for mirror in mirrors])
-
-def choose(request):
- if request.POST:
- form = MirrorlistForm(data=request.POST)
+@csrf_exempt
+def generate(request):
+ if request.REQUEST.get('country', ''):
+ form = MirrorlistForm(data=request.REQUEST)
if form.is_valid():
- country = form.cleaned_data['country']
- return HttpResponseRedirect(reverse(generate,
- kwargs = { 'country' : country }))
+ countries = form.cleaned_data['country']
+ return find_mirrors(request, countries)
else:
form = MirrorlistForm()
return render_to_response('mirrors/index.html', {'mirrorlist_form': form},
context_instance=RequestContext(request))
-def generate(request, country=None):
+def find_mirrors(request, countries=None):
qset = MirrorUrl.objects.select_related().filter(
Q(protocol__protocol__iexact='HTTP') |
Q(protocol__protocol__iexact='FTP'),
mirror__public=True, mirror__active=True, mirror__isos=True
)
- if country and country != 'all':
- qset = qset.filter(mirror__country__iexact=country)
+ if countries and 'all' not in countries:
+ qset = qset.filter(mirror__country__in=countries)
qset = qset.order_by('mirror__country', 'mirror__name', 'url')
- res = render_to_response('mirrors/mirrorlist.txt',
- {
+ res = render_to_response('mirrors/mirrorlist.txt', {
'mirror_urls': qset,
},
- mimetype='text/plain')
+ mimetype='text/plain',
+ context_instance=RequestContext(request))
return res
# vim: set ts=4 sw=4 et: