rewrote application filters

This commit is contained in:
Oskar Hahn 2012-04-18 19:57:39 +02:00
parent 9f70b2ff1c
commit 07e9d8e9f9
2 changed files with 46 additions and 17 deletions

View File

@ -27,17 +27,17 @@
</select> </select>
</form> </form>
</p> </p>
{{ applications|length }} {% trans "Applications" %} {{ applications.exist }} {% trans "Applications" %}
<table> <table>
<tr> <tr>
<th><a href="?sort=number{% if 'number' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Number" %}</a></th> <th><a href="?sort=number{% if 'number' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Number" %}</a></th>
<th><a href="?sort=aversion__title{% if 'aversion__title' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Application title" %}</a></th> <th><a href="?sort=title{% if 'title' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Application title" %}</a></th>
{% if min_supporters > 0 %} {% if min_supporters > 0 %}
<th><a href="?sort=supporter{% if 'supporter' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Number of supporters" %}</a></th> <th><a href="?sort=supporter{% if 'supporter' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Number of supporters" %}</a></th>
{% endif %} {% endif %}
<th><a href="?sort=status{% if 'status' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Status" %}</a></th> <th><a href="?sort=status{% if 'status' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Status" %}</a></th>
<th><a href="?sort=submitter{% if 'submitter' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Submitter" %}</a></th> <th><a href="?sort=submitter{% if 'submitter' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Submitter" %}</a></th>
<th><a href="?sort=aversion__time{% if 'aversion__time' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Creation Time" %}<a></th> <th><a href="?sort=time{% if 'time' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Creation Time" %}<a></th>
</tr> </tr>
{% for application in applications %} {% for application in applications %}
<tr class="{% cycle '' 'odd' %}"> <tr class="{% cycle '' 'odd' %}">

View File

@ -15,6 +15,12 @@ import csv
import utils.csv_ext import utils.csv_ext
import os import os
from urllib import urlencode
try:
from urlparse import parse_qs
except ImportError: # python <= 2.5
from cgi import parse_qs
from django.shortcuts import redirect from django.shortcuts import redirect
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
@ -62,28 +68,51 @@ def overview(request):
""" """
View all applications View all applications
""" """
query = Application.objects
if 'number' in request.GET:
query = query.filter(number=None)
if 'status' in request.GET:
if 'statusvalue' in request.GET and 'on' in request.GET['status']:
query = query.filter(status__iexact=request.GET['statusvalue'])
try: try:
sort = request.GET['sort'] sortfilter = parse_qs(request.COOKIES['votecollector_sortfilter'])
if sort in ['number', 'supporter', 'status', 'submitter', \ for value in sortfilter:
'aversion__time', 'aversion__title']: sortfilter[value] = sortfilter[value][0]
query = query.order_by(sort)
except KeyError: except KeyError:
query = query.order_by("number") sortfilter = {}
if 'reverse' in request.GET:
for value in [u'sort', u'reverse', u'number', u'status', u'needsup', u'statusvalue']:
if value in request.REQUEST:
if request.REQUEST[value] == '0':
try:
del sortfilter[value]
except KeyError:
pass
else:
sortfilter[value] = request.REQUEST[value]
query = Application.objects.all()
if 'number' in sortfilter:
query = query.filter(number=None)
if 'status' in sortfilter:
if 'statusvalue' in sortfilter and 'on' in sortfilter['status']:
query = query.filter(status__iexact=sortfilter['statusvalue'])
if 'sort' in sortfilter:
if sortfilter['sort'] == 'title':
sort = 'aversion__title'
elif sortfilter['sort'] == 'time':
sort = 'aversion__time'
else:
sort = sortfilter['sort']
query = query.order_by(sort)
if 'reverse' in sortfilter:
query = query.reverse() query = query.reverse()
if 'needsup' in request.GET:
# todo: rewrite this with a .filter()
if 'needsup' in sortfilter:
applications = [] applications = []
for application in query.all(): for application in query.all():
if not application.enough_supporters: if not application.enough_supporters:
applications.append(application) applications.append(application)
else: else:
applications = query.all() applications = query
return { return {
'applications': applications, 'applications': applications,
'min_supporters': int(config['application_min_supporters']), 'min_supporters': int(config['application_min_supporters']),