Commit db2608a6 authored by Nicolas Joyard's avatar Nicolas Joyard
Browse files

Add sorting to rep list and dossier list

parent 1dde5a4c
Display :
- if active_only
current legislature only /
%a{'href': '?={queries.urlencode}&active_only=0'}
all data
- else
%a{'href': '?={queries.urlencode}&active_only=1'}
current legislature only
="/ all data"
\ No newline at end of file
.active-block
Display :
- if active_only
current legislature only /
%a{'href': '?={queries.urlencode}&active_only=0'}
all data
- else
%a{'href': '?={queries.urlencode}&active_only=1'}
current legislature only
="/ all data"
\ No newline at end of file
%span.grid-list-selector
%a{:href => '?={queries.urlencode}&display=grid'}
%a{:href => '?={queries.urlencode}&display=grid'}<
<i class="fa fa-th"></i>
%a{:href => '?={queries.urlencode}&display=list'}
%a{:href => '?={queries.urlencode}&display=list'}<
<i class="fa fa-th-list"></i>
......@@ -36,21 +36,24 @@
<i aria-hidden="true" class="fa fa-chevron-circle-right"></i>
%div.count
Number of results : {{ paginator.count }}
.count-block
Number of results : {{ paginator.count }}
- if active_only != None
%br
- include 'core/blocks/active-filter.html'
%br
Number of displayed results :
{{ paginator.per_page }}
(
- for limit in pagination_limits
%a{'href': '?={queries.urlencode}&paginate_by={{ limit }}'}
{{ limit }}
- if not forloop.last
\/
)
- if grid_list
- include 'core/blocks/grid-list.html'
- if sort != None
- include 'core/blocks/sorting.html'
.page-size-block
Number of displayed results :
{{ paginator.per_page }}
(
- for limit in pagination_limits
%a{'href': '?={queries.urlencode}&paginate_by={{ limit }}'}<
{{ limit }}
- if not forloop.last
\/
)
- if grid_list
- include 'core/blocks/grid-list.html'
- if sort.fields|length > 0
.sorting-block
Order by :
- for field, label in sort.fields.items
- if sort.field == field
{{ label }}
- else
%a{'href': '?={queries.urlencode}&sort_by=={field}'}<
{{ label }}
- if not forloop.last
=" | "
&ndash;
- if sort.dir == 'asc'
ascending |
%a{'href': '?={queries.urlencode}&sort_dir=desc'}<
descending
- else
%a{'href': '?={queries.urlencode}&sort_dir=asc'}<
ascending
="| descending"
......@@ -50,6 +50,61 @@ class ActiveLegislatureMixin(object):
return c
class SortMixin(object):
"""
Mixin for views that allow sorting.
The sort_fields attribute should be defined to a {field: label} dict
containing all fields usable for sorting.
The sort_default and sort_default_dir attributes should contain the default
sorting settings.
"""
sort_fields = {}
sort_default_field = None
sort_default_dir = 'asc'
def get(self, *args, **kwargs):
self.set_sorting()
return super(SortMixin, self).get(*args, **kwargs)
def set_sorting(self):
if 'sort_by' in self.request.GET:
self.request.session['sort_by'] = self.request.GET['sort_by']
elif 'sort_by' not in self.request.session:
self.request.session['sort_by'] = self.sort_default_field
if self.request.session['sort_by'] not in self.sort_fields:
self.request.session['sort_by'] = self.sort_default_field
if 'sort_dir' in self.request.GET:
self.request.session['sort_dir'] = self.request.GET['sort_dir']
elif 'sort_dir' not in self.request.session:
self.request.session['sort_dir'] = self.sort_default_dir
def get_context_data(self, **kwargs):
c = super(SortMixin, self).get_context_data(**kwargs)
c['queries'] = copy(self.request.GET)
if 'sort_by' in c['queries']:
del c['queries']['sort_by']
if 'sort_dir' in c['queries']:
del c['queries']['sort_dir']
c['sort'] = {
'fields': self.sort_fields,
'field': self.request.session['sort_by'],
'dir': self.request.session['sort_dir'],
}
return c
def get_queryset(self):
qs = super(SortMixin, self).get_queryset()
if self.request.session['sort_by']:
qs = qs.order_by('%s%s' % (
'-' if self.request.session['sort_dir'] == 'desc' else '',
self.request.session['sort_by']))
return qs
class PaginationMixin(object):
pagination_limits = (12, 24, 48, 96)
......
# coding: utf-8
from core.views import PaginationMixin
from core.views import PaginationMixin, SortMixin
from django.db.models import Count
from django.views import generic
......@@ -10,7 +10,7 @@ from representatives_votes.models import Dossier
from ..filters import DossierFilter
class DossierList(PaginationMixin, generic.ListView):
class DossierList(PaginationMixin, SortMixin, generic.ListView):
current_filter = None
queryset = Dossier.objects.prefetch_related(
......@@ -21,7 +21,15 @@ class DossierList(PaginationMixin, generic.ListView):
).annotate(
nb_proposals=Count('proposals'),
nb_recomm=Count('proposals__recommendation')
).order_by('-nb_recomm', '-reference')
)
sort_fields = {
'title': 'title',
'reference': 'reference',
'nb_recomm': 'recommendations',
'nb_proposals': 'proposals',
}
sort_default_field = 'nb_recomm'
sort_default_dir = 'desc'
def dossier_filter(self, qs):
f = DossierFilter(self.request.GET, queryset=qs)
......
# coding: utf-8
from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin, \
ActiveLegislatureMixin
ActiveLegislatureMixin, SortMixin
from django.views import generic
......@@ -13,11 +13,16 @@ from .representative_mixin import RepresentativeViewMixin
class RepresentativeList(CSVDownloadMixin, GridListMixin, PaginationMixin,
RepresentativeViewMixin, ActiveLegislatureMixin,
generic.ListView):
SortMixin, generic.ListView):
csv_name = 'representatives'
queryset = Representative.objects.select_related('score')
current_filter = None
sort_fields = {
'last_name': 'name',
'score__score': 'score',
}
sort_default_field = 'last_name'
def get_context_data(self, **kwargs):
c = super(RepresentativeList, self).get_context_data(**kwargs)
......
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