From 6febaf6d96745b438188064cf029f9afcc4a9748 Mon Sep 17 00:00:00 2001
From: luxcem
Date: Tue, 17 Mar 2015 14:34:50 +0100
Subject: [PATCH] adds urls and view for filtering representatives by mandates
---
.../memopol_representatives/list.haml | 3 +
.../memopol_representatives/search.html | 2 +-
.../memopol_representatives/view.html | 29 +++---
.../templatetags/__init__.py | 0
.../templatetags/by_mandate_url.py | 19 ++++
memopol_representatives/urls.py | 7 +-
memopol_representatives/views.py | 95 ++++++++++---------
7 files changed, 91 insertions(+), 64 deletions(-)
create mode 100644 memopol_representatives/templatetags/__init__.py
create mode 100644 memopol_representatives/templatetags/by_mandate_url.py
diff --git a/memopol_representatives/templates/memopol_representatives/list.haml b/memopol_representatives/templates/memopol_representatives/list.haml
index b806135..b32776f 100644
--- a/memopol_representatives/templates/memopol_representatives/list.haml
+++ b/memopol_representatives/templates/memopol_representatives/list.haml
@@ -3,6 +3,9 @@
- block content
- include 'memopol_representatives/search.html'
+ %p
+ Number of representatives: {{ representative_num }}
+
%table
- for representative in representatives
%tr
diff --git a/memopol_representatives/templates/memopol_representatives/search.html b/memopol_representatives/templates/memopol_representatives/search.html
index bd2b291..86a9dfe 100644
--- a/memopol_representatives/templates/memopol_representatives/search.html
+++ b/memopol_representatives/templates/memopol_representatives/search.html
@@ -1,4 +1,4 @@
-
{{ representative.full_name }}
-
-{% for mandate in representative.mandate_set.all %}
-{% if mandate.group.kind == "group" and mandate.active %}
-{{ mandate.role }} of {{ mandate.group.name }}
-{% endif %}
-{% endfor %}
+
+
+
+ {{ representative.current_group.role }} of {{ representative.current_group.group.name }}
+
+
+
Born in {{ representative.birth_place }} the {{ representative.birth_date }} ({{ representative.get_gender_display }})
{{ representative.country.name }}
-Committees
-{% for mandate in representative.mandate_set.all %}
- {% if mandate.group.kind == "committee" and mandate.active %}
-
- {{ mandate.role }} of {{ mandate.group.name }}
- ({{ mandate.group.abbreviation }})
-
- {% endif %}
-{% endfor %}
-
Mandates
{% for mandate in representative.active_mandates %}
diff --git a/memopol_representatives/templatetags/__init__.py b/memopol_representatives/templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/memopol_representatives/templatetags/by_mandate_url.py b/memopol_representatives/templatetags/by_mandate_url.py
new file mode 100644
index 0000000..9159646
--- /dev/null
+++ b/memopol_representatives/templatetags/by_mandate_url.py
@@ -0,0 +1,19 @@
+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
+ )
diff --git a/memopol_representatives/urls.py b/memopol_representatives/urls.py
index 82933f6..d5473da 100644
--- a/memopol_representatives/urls.py
+++ b/memopol_representatives/urls.py
@@ -5,6 +5,11 @@ from memopol_representatives import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
- url(r'^committee/(?P\w{4})$', views.committee, name='committee'),
+ url(r'^committee/(?P\w{4})$',
+ views.by_mandate, {'mandate_kind': 'committee'}, name='committee'),
+ url(r'^listby/(?P\w+)/a/(?P.+)$',
+ views.by_mandate, name='listby'),
+ url(r'^listby/(?P\w+)/n/(?P.+)$',
+ views.by_mandate, name='listby'),
url(r'^view/(?P\d+)$', views.view, name='view'),
)
diff --git a/memopol_representatives/views.py b/memopol_representatives/views.py
index 2ca53a7..c6e833f 100644
--- a/memopol_representatives/views.py
+++ b/memopol_representatives/views.py
@@ -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',
--
GitLab