Commit 4661f1fa authored by Nicolas Joyard's avatar Nicolas Joyard

Add theme filter in representative detail

parent a381699b
......@@ -7,6 +7,38 @@ from django import http
import unicodecsv as csv
class ThemeSelectionMixin(object):
"""
Mixin for views that allow selecting a theme
"""
def get(self, *args, **kwargs):
self.set_selected_theme()
return super(ThemeSelectionMixin, self).get(*args, **kwargs)
def set_selected_theme(self):
if 'selected_theme' in self.request.GET:
theme = self.request.GET['selected_theme']
self.request.session['selected_theme'] = \
theme if len(theme) else None
elif 'selected_theme' not in self.request.session:
self.request.session['selected_theme'] = None
def get_selected_theme(self):
if 'selected_theme' in self.request.session:
return self.request.session['selected_theme']
else:
return None
def get_context_data(self, **kwargs):
c = super(ThemeSelectionMixin, self).get_context_data(**kwargs)
c['selected_theme'] = self.get_selected_theme()
c['theme_querystring'] = copy(self.request.GET)
if 'selected_theme' in c['theme_querystring']:
del c['theme_querystring']['selected_theme']
return c
class ActiveLegislatureMixin(object):
"""
Mixin for views that can switch between active legislature and all data
......
......@@ -37,6 +37,7 @@ class RepresentativeBaseTest(BaseTest):
"""
Common queries plus:
- 1 for session key
- 1 for chamber abbreviations (helper for chamber websites)
- 1 for the representative
- 1 for the main mandate
......@@ -48,8 +49,9 @@ class RepresentativeBaseTest(BaseTest):
- 1 for address country
- 1 for phone numbers related to addresses
- 1 for other phone numbers
- 2 for themes and theme scores
"""
queries = BaseTest.left_pane_queries + 11
queries = BaseTest.left_pane_queries + 14
@property
def url(self):
......
......@@ -9,9 +9,11 @@ from .representative_mixin import RepresentativeViewMixin
from representatives_positions.views import PositionFormMixin
from core.views import ThemeSelectionMixin
class RepresentativeDetailBase(RepresentativeViewMixin, PositionFormMixin,
generic.DetailView):
ThemeSelectionMixin, generic.DetailView):
queryset = Representative.objects.select_related('representative_score')
......@@ -47,7 +49,8 @@ class RepresentativeDetailBase(RepresentativeViewMixin, PositionFormMixin,
models.Prefetch(
'phone_set',
queryset=Phone.objects.filter(address__isnull=True)
)
),
'theme_scores__theme'
)
return qs
......
......@@ -13,11 +13,15 @@ class RepresentativeDetailPositions(RepresentativeDetailBase):
def get_queryset(self):
qs = super(RepresentativeDetailPositions, self).get_queryset()
positions_qs = Position.objects.filter(published=True)
theme = self.get_selected_theme()
if theme:
positions_qs = positions_qs.filter(themes__slug=theme)
qs = qs.prefetch_related(
models.Prefetch(
'positions',
queryset=Position.objects.filter(published=True)
.order_by('-datetime', 'pk')
queryset=positions_qs.order_by('-datetime', 'pk')
),
'positions__themes',
'positions__position_score'
......
......@@ -13,12 +13,17 @@ class RepresentativeDetailVotes(RepresentativeDetailBase):
def get_queryset(self):
qs = super(RepresentativeDetailVotes, self).get_queryset()
vote_qs = Vote.objects.exclude(proposal__recommendation=None)
theme = self.get_selected_theme()
if theme:
vote_qs = vote_qs.filter(
models.Q(proposal__dossier__themes__slug=theme)
)
qs = qs.prefetch_related(
models.Prefetch(
'votes',
queryset=Vote.objects.exclude(
proposal__recommendation=None
).select_related(
queryset=vote_qs.select_related(
'vote_score',
'proposal__dossier',
'proposal__recommendation'
......
{% load i18n %}
{% load memopol_tags %}
<div class="btn-group" id="theme-menu">
<button class="btn {% if selected_theme %}btn-primary{% else %}btn-default{% endif %} dropdown-toggle"
type="button" id="theme-button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="true">
{% if selected_theme %}
{% for theme_score in theme_scores %}
{% if theme_score.theme.slug == selected_theme %}
{{ theme_score.theme.name }}
{% endif %}
{% endfor %}
{% else %}
{% trans "All themes" %}
{% endif %}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="theme-button">
<li {% if not selected_theme %}class="disabled"{% endif %}>
<a href="?{{ theme_querystring.urlencode }}&selected_theme=">{% trans "All themes" %}</a>
</li>
{% for theme_score in theme_scores %}
<li {% if theme_score.theme.slug == selected_theme %}class="disabled"{% endif %}>
<a href="?{{ theme_querystring.urlencode }}&selected_theme={{ theme_score.theme.slug }}">
{{ theme_score.theme.name }}
{{ theme_score.score|score_badge }}
</a>
</li>
{% endfor %}
</ul>
</div>
......@@ -122,6 +122,13 @@
</div>
</div>
<div class="row">
<div class="col-sm-3 text-center">
{% include "blocks/_themescore_dropdown.html" with theme_scores=representative.theme_scores.all %}
</div>
</div>
<div class="row">
<div class="col-xs-12">
<ul class="nav nav-tabs nav-justified" role="tablist">
......
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