Commit 6febaf6d authored by luxcem's avatar luxcem

adds urls and view for filtering representatives by mandates

parent ea8f7449
......@@ -3,6 +3,9 @@
- block content
- include 'memopol_representatives/search.html'
%p
Number of representatives: {{ representative_num }}
%table
- for representative in representatives
%tr
......
<form action="{% url 'representatives:index' %}" method="get">
<form action="" method="get">
{% csrf_token %}
<label for="search">Search : </label>
<input id="search" type="text" name="search">
......
{% extends 'base.html' %}
{% load by_mandate_url %}
{% block content %}
......@@ -9,26 +10,17 @@
</p>
<h1>{{ representative.full_name }}</h1>
{% for mandate in representative.mandate_set.all %}
{% if mandate.group.kind == "group" and mandate.active %}
<h3>{{ mandate.role }} of {{ mandate.group.name }}</h3>
{% endif %}
{% endfor %}
<p>
<strong>
<a href="{{ representative.current_group|by_mandate_url }}">
{{ representative.current_group.role }} of {{ representative.current_group.group.name }}
</a>
</strong>
</p>
<p>Born in {{ representative.birth_place }} the {{ representative.birth_date }} ({{ representative.get_gender_display }})</p>
<p>{{ representative.country.name }}</p>
<h2 style="clear: both">Committees</h2>
{% for mandate in representative.mandate_set.all %}
{% if mandate.group.kind == "committee" and mandate.active %}
<p>
{{ mandate.role }} of {{ mandate.group.name }}
(<a href="{% url 'representatives:committee' mandate.group.abbreviation %}">{{ mandate.group.abbreviation }}</a>)
</p>
{% endif %}
{% endfor %}
<h2 style="clear: both">Mandates</h2>
{% for mandate in representative.active_mandates %}
<div class="mandate">
......@@ -40,7 +32,10 @@
</h3>
<p>
{{ mandate.begin_date }} to {{ mandate.end_date }} <br>
<strong>{{ mandate.group.kind }}</strong> : <em>{{ mandate.group.name }} ({{ mandate.group.abbreviation }})</em> <br>
<a href="{{ mandate|by_mandate_url }}">
<strong>{{ mandate.group.kind }}</strong> : <em>{{ mandate.group.name }} ({{ mandate.group.abbreviation }})</em>
</a>
<br>
Constituency : {{ mandate.constituency.name }} <br>
</p>
</div>
......
from django import template
from django.core.urlresolvers import reverse
register = template.Library()
@register.filter
def by_mandate_url(mandate):
kwargs = {'mandate_kind': mandate.group.kind}
if mandate.group.abbreviation:
kwargs['mandate_abbr'] = mandate.group.abbreviation
else:
kwargs['mandate_name'] = mandate.group.name
return reverse(
'representatives:listby',
kwargs=kwargs
)
......@@ -5,6 +5,11 @@ from memopol_representatives import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
url(r'^committee/(?P<committee>\w{4})$', views.committee, name='committee'),
url(r'^committee/(?P<mandate_abbr>\w{4})$',
views.by_mandate, {'mandate_kind': 'committee'}, name='committee'),
url(r'^listby/(?P<mandate_kind>\w+)/a/(?P<mandate_abbr>.+)$',
views.by_mandate, name='listby'),
url(r'^listby/(?P<mandate_kind>\w+)/n/(?P<mandate_name>.+)$',
views.by_mandate, name='listby'),
url(r'^view/(?P<num>\d+)$', views.view, name='view'),
)
......@@ -6,59 +6,57 @@ from representatives.models import Representative
def index(request):
context = {}
if request.GET.get('search'):
search = request.GET.get('search')
representative_list = Representative.objects.filter(
Q(full_name__icontains=search) |
Q(country__name__icontains=search)
)
queries_without_page = request.GET.copy()
if 'page' in queries_without_page:
del queries_without_page['page']
context['queries'] = queries_without_page
else:
representative_list = Representative.objects.all()
representative_list = _filter_by_search(
request,
Representative.objects.all()
)
paginator = Paginator(representative_list, 50)
return _render_list(request, representative_list)
page = request.GET.get('page')
try:
representatives = paginator.page(page)
except PageNotAnInteger:
representatives = paginator.page(1)
except EmptyPage:
representatives = paginator.page(paginator.num_pages)
context['representatives'] = representatives
return render(
request,
'memopol_representatives/list.html',
context
def by_mandate(request, mandate_kind, mandate_abbr=None, mandate_name=None):
representative_list = Representative.objects.filter(
mandate__group__kind=mandate_kind,
mandate__active=True
)
if mandate_abbr:
representative_list = representative_list.filter(
mandate__group__abbreviation=mandate_abbr,
mandate__active=True
)
if mandate_name:
representative_list = representative_list.filter(
Q(mandate__group__name__icontains=mandate_name),
mandate__active=True
)
# Select distinct representatives and filter by search
representative_list = list(set(
_filter_by_search(request, representative_list)
))
def committee(request, committee):
context = {}
if request.GET.get('search'):
search = request.GET.get('search')
representative_list = Representative.objects.filter(
mandate__group__kind='committee',
mandate__group__abbreviation=committee
).filter(
Q(full_name__icontains=search)
)
queries_without_page = request.GET.copy()
if 'page' in queries_without_page:
del queries_without_page['page']
context['queries'] = queries_without_page
return _render_list(request, representative_list)
def _filter_by_search(request, representative_list):
"""
Return a representative_list filtered by
the representative name provided in search form
"""
search = request.GET.get('search')
if search:
return representative_list.filter(
Q(full_name__icontains=search)
)
else:
representative_list = list(set(Representative.objects.filter(
mandate__group__kind='committee',
mandate__group__abbreviation=committee
).all()))
return representative_list
paginator = Paginator(representative_list, 50)
def _render_list(request, representative_list, num_by_page=50):
"""
Render a paginated list of representatives
"""
paginator = Paginator(representative_list, num_by_page)
page = request.GET.get('page')
try:
representatives = paginator.page(page)
......@@ -67,7 +65,14 @@ def committee(request, committee):
except EmptyPage:
representatives = paginator.page(paginator.num_pages)
context = {}
queries_without_page = request.GET.copy()
if 'page' in queries_without_page:
del queries_without_page['page']
context['queries'] = queries_without_page
context['representatives'] = representatives
context['representative_num'] = paginator.count
return render(
request,
'memopol_representatives/list.html',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment