Commit 6febaf6d authored by luxcem's avatar luxcem

adds urls and view for filtering representatives by mandates

parent ea8f7449
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
- block content - block content
- include 'memopol_representatives/search.html' - include 'memopol_representatives/search.html'
%p
Number of representatives: {{ representative_num }}
%table %table
- for representative in representatives - for representative in representatives
%tr %tr
......
<form action="{% url 'representatives:index' %}" method="get"> <form action="" method="get">
{% csrf_token %} {% csrf_token %}
<label for="search">Search : </label> <label for="search">Search : </label>
<input id="search" type="text" name="search"> <input id="search" type="text" name="search">
......
{% extends 'base.html' %} {% extends 'base.html' %}
{% load by_mandate_url %}
{% block content %} {% block content %}
...@@ -9,26 +10,17 @@ ...@@ -9,26 +10,17 @@
</p> </p>
<h1>{{ representative.full_name }}</h1> <h1>{{ representative.full_name }}</h1>
<p>
{% for mandate in representative.mandate_set.all %} <strong>
{% if mandate.group.kind == "group" and mandate.active %} <a href="{{ representative.current_group|by_mandate_url }}">
<h3>{{ mandate.role }} of {{ mandate.group.name }}</h3> {{ representative.current_group.role }} of {{ representative.current_group.group.name }}
{% endif %} </a>
{% endfor %} </strong>
</p>
<p>Born in {{ representative.birth_place }} the {{ representative.birth_date }} ({{ representative.get_gender_display }})</p> <p>Born in {{ representative.birth_place }} the {{ representative.birth_date }} ({{ representative.get_gender_display }})</p>
<p>{{ representative.country.name }}</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> <h2 style="clear: both">Mandates</h2>
{% for mandate in representative.active_mandates %} {% for mandate in representative.active_mandates %}
<div class="mandate"> <div class="mandate">
...@@ -40,7 +32,10 @@ ...@@ -40,7 +32,10 @@
</h3> </h3>
<p> <p>
{{ mandate.begin_date }} to {{ mandate.end_date }} <br> {{ 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> Constituency : {{ mandate.constituency.name }} <br>
</p> </p>
</div> </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 ...@@ -5,6 +5,11 @@ from memopol_representatives import views
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(r'^$', views.index, name='index'), 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'), url(r'^view/(?P<num>\d+)$', views.view, name='view'),
) )
...@@ -6,59 +6,57 @@ from representatives.models import Representative ...@@ -6,59 +6,57 @@ from representatives.models import Representative
def index(request): def index(request):
context = {} representative_list = _filter_by_search(
if request.GET.get('search'): request,
search = request.GET.get('search') Representative.objects.all()
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()
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 def by_mandate(request, mandate_kind, mandate_abbr=None, mandate_name=None):
return render( representative_list = Representative.objects.filter(
request, mandate__group__kind=mandate_kind,
'memopol_representatives/list.html', mandate__active=True
context )
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): return _render_list(request, representative_list)
context = {}
if request.GET.get('search'):
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') search = request.GET.get('search')
representative_list = Representative.objects.filter( if search:
mandate__group__kind='committee', return representative_list.filter(
mandate__group__abbreviation=committee
).filter(
Q(full_name__icontains=search) 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
else: else:
representative_list = list(set(Representative.objects.filter( return representative_list
mandate__group__kind='committee',
mandate__group__abbreviation=committee
).all()))
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') page = request.GET.get('page')
try: try:
representatives = paginator.page(page) representatives = paginator.page(page)
...@@ -67,7 +65,14 @@ def committee(request, committee): ...@@ -67,7 +65,14 @@ def committee(request, committee):
except EmptyPage: except EmptyPage:
representatives = paginator.page(paginator.num_pages) 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['representatives'] = representatives
context['representative_num'] = paginator.count
return render( return render(
request, request,
'memopol_representatives/list.html', '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