rewrote the filter and sort system for participant

This commit is contained in:
Oskar Hahn 2011-09-10 00:16:57 +02:00
parent 2973ec1f33
commit 97327a3b17
2 changed files with 52 additions and 34 deletions

View File

@ -4,32 +4,33 @@
{% block content %} {% block content %}
<h1>{% trans "Participants" %}</h1> <h1>{% trans "Participants" %}</h1>
<p><form action="{{request.url}}" name="filter" method="get"> <p><form action="{{request.url}}" name="filter" method="post">
{% csrf_token %}
{%trans "Filter" %}: {%trans "Filter" %}:
<select class="default-input" name="gender" onchange="document.forms['filter'].submit()"> <select class="default-input" name="gender" onchange="document.forms['filter'].submit()">
<option value="---">-- {%trans "Gender" %}--</option> <option value="---">-- {%trans "Gender" %}--</option>
<option value="male" {% if 'male' in request.GET.gender %}selected{% endif %}>{%trans "Male" %}</option> <option value="male" {% if 'male' in sortfilter.gender %}selected{% endif %}>{%trans "Male" %}</option>
<option value="female" {% if 'female' in request.GET.gender %}selected{% endif %}>{%trans "Female" %}</option> <option value="female" {% if 'female' in sortfilter.gender %}selected{% endif %}>{%trans "Female" %}</option>
<option value="none" {% if 'none' in request.GET.gender %}selected{% endif %}>{%trans "Not specified" %}</option> <option value="none" {% if 'none' in sortfilter.gender %}selected{% endif %}>{%trans "Not specified" %}</option>
</select> </select>
<select class="default-input" name="group" onchange="document.forms['filter'].submit()"> <select class="default-input" name="group" onchange="document.forms['filter'].submit()">
<option value="---">-- {%trans "Group" %}--</option> <option value="---">-- {%trans "Group" %}--</option>
{% for group in groups %} {% for group in groups %}
<option value="{{ group }}" {% if group in request.GET.group %}selected{% endif %}> <option value="{{ group }}" {% if group in sortfilter.group %}selected{% endif %}>
{{ group }}</option> {{ group }}</option>
{% endfor %} {% endfor %}
</select> </select>
<select class="default-input" name="type" onchange="document.forms['filter'].submit()"> <select class="default-input" name="type" onchange="document.forms['filter'].submit()">
<option value="---">-- {%trans "Type" %}--</option> <option value="---">-- {%trans "Type" %}--</option>
<option value="delegate" {% if 'delegate' in request.GET.type %}selected{% endif %}>{%trans "Delegate" %}</option> <option value="delegate" {% if 'delegate' in sortfilter.type %}selected{% endif %}>{%trans "Delegate" %}</option>
<option value="observer" {% if 'observer' in request.GET.type %}selected{% endif %}>{%trans "Observer" %}</option> <option value="observer" {% if 'observer' in sortfilter.type %}selected{% endif %}>{%trans "Observer" %}</option>
<option value="staff" {% if 'staff' in request.GET.type %}selected{% endif %}>{%trans "Staff" %}</option> <option value="staff" {% if 'staff' in sortfilter.type %}selected{% endif %}>{%trans "Staff" %}</option>
<option value="guest" {% if 'guest' in request.GET.type %}selected{% endif %}>{%trans "Guest" %}</option> <option value="guest" {% if 'guest' in sortfilter.type %}selected{% endif %}>{%trans "Guest" %}</option>
</select> </select>
<select class="default-input" name="committee" onchange="document.forms['filter'].submit()"> <select class="default-input" name="committee" onchange="document.forms['filter'].submit()">
<option value="---">-- {%trans "Committee" %}--</option> <option value="---">-- {%trans "Committee" %}--</option>
{% for committee in committees %} {% for committee in committees %}
<option value="{{ committee }}" {% if committee in request.GET.committee %}selected{% endif %}> <option value="{{ committee }}" {% if committee in sortfilter.committee %}selected{% endif %}>
{{ committee }}</option> {{ committee }}</option>
{% endfor %} {% endfor %}
</select> </select>
@ -38,15 +39,15 @@
{{ users|length }} {% trans "Participant" %} {{ users|length }} {% trans "Participant" %}
<table> <table>
<tr> <tr>
<th><a href="?sort=first_name{% if 'first_name' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "First Name" %}</a></th> <th><a href="?sort=first_name&reverse={% if 'first_name' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "First Name" %}</a></th>
<th><a href="?sort=last_name{% if 'last_name' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Last Name" %}</a></th> <th><a href="?sort=last_name&reverse={% if 'last_name' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "Last Name" %}</a></th>
<th><a href="?sort=group{% if 'group' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Group" %}</a></th> <th><a href="?sort=group&reverse={% if 'group' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "Group" %}</a></th>
<th><a href="?sort=type{% if 'type' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Type" %}</a></th> <th><a href="?sort=type&reverse={% if 'type' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "Type" %}</a></th>
<th><a href="?sort=committee{% if 'committee' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Committee" %}</a></th> <th><a href="?sort=committee&reverse={% if 'committee' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "Committee" %}</a></th>
{% if perms.participant.can_manage_participant %} {% if perms.participant.can_manage_participant %}
<th><a href="?sort=username{% if 'username' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Username" %}</a></th> <th><a href="?sort=username&reverse={% if 'username' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "Username" %}</a></th>
<th><a href="?sort=email{% if 'email' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Email" %}</a></th> <th><a href="?sort=email&reverse={% if 'email' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "Email" %}</a></th>
<th><a href="?sort=last_login{% if 'last_login' in request.GET.sort and 'reverse' not in request.GET %}&reverse{%endif%}">{%trans "Last Login" %}</a></th> <th><a href="?sort=last_login&reverse={% if 'last_login' in sortfilter.sort and 'reverse' not in sortfilter %}1{% else %}---{%endif%}">{%trans "Last Login" %}</a></th>
<th>{%trans "Actions" %}</th> <th>{%trans "Actions" %}</th>
{% endif %} {% endif %}
</tr> </tr>

View File

@ -11,6 +11,9 @@
""" """
import csv import csv
from urllib import urlencode
from urlparse import parse_qs
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import redirect from django.shortcuts import redirect
from django.template import RequestContext from django.template import RequestContext
@ -33,24 +36,36 @@ from django.db.models import Avg, Max, Min, Count
@permission_required('participant.can_see_participant') @permission_required('participant.can_see_participant')
@template('participant/overview.html') @template('participant/overview.html')
def get_overview(request): def get_overview(request):
query = User.objects
if 'gender' in request.GET and '---' not in request.GET['gender']:
query = query.filter(profile__gender__iexact=request.GET['gender'])
if 'group' in request.GET and '---' not in request.GET['group']:
query = query.filter(profile__group__iexact=request.GET['group'])
if 'type' in request.GET and '---' not in request.GET['type']:
query = query.filter(profile__type__iexact=request.GET['type'])
if 'committee' in request.GET and '---' not in request.GET['committee']:
query = query.filter(profile__committee__iexact=request.GET['committee'])
try: try:
sort = request.GET['sort'] sortfilter = parse_qs(request.COOKIES['participant_sortfilter'])
if sort in ['first_name', 'last_name','username','last_login','email']: except KeyError:
query = query.order_by(sort) sortfilter = {}
elif sort in ['group', 'type', 'committee']:
query = query.order_by('profile__%s' % sort) for value in ['gender', 'group', 'type', 'committee', 'sort', 'reverse']:
if value in request.REQUEST:
if request.REQUEST[value] == '---':
try:
del sortfilter[value]
except KeyError: except KeyError:
pass pass
if 'reverse' in request.GET: else:
sortfilter[value] = [request.REQUEST[value]]
query = User.objects
if 'gender' in sortfilter:
query = query.filter(profile__gender__iexact=sortfilter['gender'][0])
if 'group' in sortfilter:
query = query.filter(profile__group__iexact=sortfilter['group'][0])
if 'type' in sortfilter:
query = query.filter(profile__type__iexact=sortfilter['type'][0])
if 'committee' in sortfilter:
query = query.filter(profile__committee__iexact=sortfilter['committee'][0])
if 'sort' in sortfilter:
if sortfilter['sort'][0] in ['first_name', 'last_name','username','last_login','email']:
query = query.order_by(sortfilter['sort'][0])
elif sortfilter['sort'][0] in ['group', 'type', 'committee']:
query = query.order_by('profile__%s' % sortfilter['sort'][0])
if 'reverse' in sortfilter:
query = query.reverse() query = query.reverse()
userlist = query.all() userlist = query.all()
@ -67,6 +82,8 @@ def get_overview(request):
'users': users, 'users': users,
'groups': groups, 'groups': groups,
'committees': committees, 'committees': committees,
'cookie': ['participant_sortfilter', urlencode(sortfilter, doseq=True)],
'sortfilter': sortfilter,
} }
@permission_required('participant.can_manage_participant') @permission_required('participant.can_manage_participant')