Commit 93fac5d4 authored by Nicolas Joyard's avatar Nicolas Joyard
Browse files

Rework sort UX

parent c160c418
......@@ -53,70 +53,66 @@ class ActiveLegislatureMixin(object):
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.
The sort_modes attribute should be defined to a dict as such:
{
'mode1': {
'order': 42,
'label': 'mode label',
'fields': ['-field1', 'field2', ...]
},
...
}
The sort_default attribute should contain the default sorting mode.
"""
sort_fields = {}
sort_default_field = None
sort_default_dir = 'asc'
sort_modes = {}
sort_default = None
sort_session_prefix = ''
def get(self, *args, **kwargs):
self.set_sorting()
return super(SortMixin, self).get(*args, **kwargs)
def _session_get(self, key):
k = '%s_%s' % (self.sort_session_prefix, key)
def _session_get_sort(self):
k = '%s_sort' % self.sort_session_prefix
return self.request.session[k]
def _session_set(self, key, value):
k = '%s_%s' % (self.sort_session_prefix, key)
def _session_set_sort(self, value):
k = '%s_sort' % self.sort_session_prefix
self.request.session[k] = value
def _session_exists(self, key):
k = '%s_%s' % (self.sort_session_prefix, key)
def _session_sort_exists(self):
k = '%s_sort' % self.sort_session_prefix
return k in self.request.session
def set_sorting(self):
if 'sort_by' in self.request.GET:
self._session_set('sort_by', self.request.GET['sort_by'])
elif not self._session_exists('sort_by'):
self._session_set('sort_by', self.sort_default_field)
if 'sort' in self.request.GET:
self._session_set_sort(self.request.GET['sort'])
elif not self._session_sort_exists():
self._session_set_sort(self.sort_default)
if self._session_get('sort_by') not in self.sort_fields:
self._session_set('sort_by', self.sort_default_field)
if 'sort_dir' in self.request.GET:
self._session_set('sort_dir', self.request.GET['sort_dir'])
elif not self._session_exists('sort_dir'):
self._session_set('sort_dir', self.sort_default_dir)
if self._session_get_sort() not in self.sort_modes:
self._session_set_sort(self.sort_default)
def get_context_data(self, **kwargs):
c = super(SortMixin, self).get_context_data(**kwargs)
c['sort_by_querystring'] = copy(self.request.GET)
if 'sort_by' in c['sort_by_querystring']:
del c['sort_by_querystring']['sort_by']
c['sort_dir_querystring'] = copy(self.request.GET)
if 'sort_dir' in c['sort_dir_querystring']:
del c['sort_dir_querystring']['sort_dir']
c['sort_querystring'] = copy(self.request.GET)
if 'sort' in c['sort_querystring']:
del c['sort_querystring']['sort']
c['sort'] = {
'fields': self.sort_fields,
'field': self._session_get('sort_by'),
'dir': self._session_get('sort_dir'),
'modes': [{'id': k, 'label': v['label'], 'order': v['order']}
for k, v in self.sort_modes.iteritems()],
'mode': self._session_get_sort()
}
return c
def get_queryset(self):
qs = super(SortMixin, self).get_queryset()
if self._session_get('sort_by'):
qs = qs.order_by('%s%s' % (
'-' if self._session_get('sort_dir') == 'desc' else '',
self._session_get('sort_by')))
if self._session_get_sort() in self.sort_modes:
mode = self.sort_modes[self._session_get_sort()]
qs = qs.order_by(*mode['fields'])
return qs
......
<h4 class="text-center">Resolution on the transparency and state of play of the Anti-Counterfeiting Trade Agreement ACTA negotiations</h4>
---
<h4 class="text-center">Classification, packaging and labelling of dangerous preparations</h4>
---
<h4 class="text-center">Health and safety at work: exposure of workers to electromagnetic fields; transposition of Directive</h4>
---
<h4 class="text-center">Court of Auditors: appointment of a member, Baudilio Tomé Muguruza - ES</h4>
---
<h4 class="text-center">Declaration on the lack of a transparent process for the Anti-Counterfeiting Trade Agreement (ACTA) and potentially objectionable content</h4>
---
<h4 class="text-center">Personal data protection: processing of data for the purposes of prevention, investigation, detection or prosecution of criminal offences or execution of criminal penalties, and free movement of data</h4>
---
<h4 class="text-center">Pouvoirs publics : application de la loi relative à l’état d’urgence</h4>
---
<h4 class="text-center">Pouvoirs publics : prorogation de l'état d'urgence</h4>
---
<h4 class="text-center">Pouvoirs publics : prorogation de l'application de la loi relative à l'état d'urgence</h4>
---
<h4 class="text-center">Pouvoirs publics : prorogation de l'application de la loi sur l'état d'urgence</h4>
---
<h4 class="text-center">Resolution on the Anti-Counterfeiting Trade Agreement (ACTA)</h4>
\ No newline at end of file
<h4 class="text-center">Personal data protection: processing of data for the purposes of prevention, investigation, detection or prosecution of criminal offences or execution of criminal penalties, and free movement of data</h4>
---
<h4 class="text-center">Classification, packaging and labelling of dangerous preparations</h4>
---
<h4 class="text-center">Health and safety at work: exposure of workers to electromagnetic fields; transposition of Directive</h4>
---
<h4 class="text-center">Court of Auditors: appointment of a member, Baudilio Tomé Muguruza - ES</h4>
---
<h4 class="text-center">Resolution on the transparency and state of play of the Anti-Counterfeiting Trade Agreement ACTA negotiations</h4>
---
<h4 class="text-center">Declaration on the lack of a transparent process for the Anti-Counterfeiting Trade Agreement (ACTA) and potentially objectionable content</h4>
---
<h4 class="text-center">Pouvoirs publics : application de la loi relative à l’état d’urgence</h4>
---
<h4 class="text-center">Pouvoirs publics : prorogation de l'application de la loi relative à l'état d'urgence</h4>
---
<h4 class="text-center">Pouvoirs publics : prorogation de l'état d'urgence</h4>
---
<h4 class="text-center">Pouvoirs publics : prorogation de l'application de la loi sur l'état d'urgence</h4>
---
<h4 class="text-center">Resolution on the Anti-Counterfeiting Trade Agreement (ACTA)</h4>
\ No newline at end of file
<li>
<a href="?&amp;sort_by=nb_proposals">proposals</a>
</li>
---
<li class="disabled">
<a href="?&amp;sort_by=nb_recommendations">recommendations</a>
<a href="?&amp;sort=title-asc">Title A-Z</a>
</li>
---
<li>
<a href="?&amp;sort_by=title">title</a>
<a href="?&amp;sort=title-desc">Title Z-A</a>
</li>
---
<li>
<a href="?&amp;sort_dir=asc">ascending</a>
<li class="disabled">
<a href="?&amp;sort=recommendations">Most recommendations</a>
</li>
---
<li class="disabled">
<a href="?&amp;sort_dir=desc">descending</a>
<li>
<a href="?&amp;sort=proposals">Most proposals</a>
</li>
\ No newline at end of file
<li>
<a href="?&amp;sort_by=score__score">score</a>
<li class="disabled">
<a href="?&amp;sort=name-asc">Name A-Z</a>
</li>
---
<li class="disabled">
<a href="?&amp;sort_by=last_name">name</a>
<li>
<a href="?&amp;sort=name-desc">Name Z-A</a>
</li>
---
<li class="disabled">
<a href="?&amp;sort_dir=asc">ascending</a>
<li>
<a href="?&amp;sort=score-desc">Worst score</a>
</li>
---
<li>
<a href="?&amp;sort_dir=desc">descending</a>
<a href="?&amp;sort=score-asc">Best score</a>
</li>
\ No newline at end of file
Supports Markdown
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