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

Rework sort UX

parent c160c418
...@@ -53,70 +53,66 @@ class ActiveLegislatureMixin(object): ...@@ -53,70 +53,66 @@ class ActiveLegislatureMixin(object):
class SortMixin(object): class SortMixin(object):
""" """
Mixin for views that allow sorting. Mixin for views that allow sorting.
The sort_fields attribute should be defined to a {field: label} dict The sort_modes attribute should be defined to a dict as such:
containing all fields usable for sorting. {
The sort_default and sort_default_dir attributes should contain the default 'mode1': {
sorting settings. 'order': 42,
'label': 'mode label',
'fields': ['-field1', 'field2', ...]
},
...
}
The sort_default attribute should contain the default sorting mode.
""" """
sort_fields = {} sort_modes = {}
sort_default_field = None sort_default = None
sort_default_dir = 'asc'
sort_session_prefix = '' sort_session_prefix = ''
def get(self, *args, **kwargs): def get(self, *args, **kwargs):
self.set_sorting() self.set_sorting()
return super(SortMixin, self).get(*args, **kwargs) return super(SortMixin, self).get(*args, **kwargs)
def _session_get(self, key): def _session_get_sort(self):
k = '%s_%s' % (self.sort_session_prefix, key) k = '%s_sort' % self.sort_session_prefix
return self.request.session[k] return self.request.session[k]
def _session_set(self, key, value): def _session_set_sort(self, value):
k = '%s_%s' % (self.sort_session_prefix, key) k = '%s_sort' % self.sort_session_prefix
self.request.session[k] = value self.request.session[k] = value
def _session_exists(self, key): def _session_sort_exists(self):
k = '%s_%s' % (self.sort_session_prefix, key) k = '%s_sort' % self.sort_session_prefix
return k in self.request.session return k in self.request.session
def set_sorting(self): def set_sorting(self):
if 'sort_by' in self.request.GET: if 'sort' in self.request.GET:
self._session_set('sort_by', self.request.GET['sort_by']) self._session_set_sort(self.request.GET['sort'])
elif not self._session_exists('sort_by'): elif not self._session_sort_exists():
self._session_set('sort_by', self.sort_default_field) self._session_set_sort(self.sort_default)
if self._session_get('sort_by') not in self.sort_fields: if self._session_get_sort() not in self.sort_modes:
self._session_set('sort_by', self.sort_default_field) self._session_set_sort(self.sort_default)
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)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
c = super(SortMixin, self).get_context_data(**kwargs) c = super(SortMixin, self).get_context_data(**kwargs)
c['sort_by_querystring'] = copy(self.request.GET) c['sort_querystring'] = copy(self.request.GET)
if 'sort_by' in c['sort_by_querystring']: if 'sort' in c['sort_querystring']:
del c['sort_by_querystring']['sort_by'] del c['sort_querystring']['sort']
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'] = { c['sort'] = {
'fields': self.sort_fields, 'modes': [{'id': k, 'label': v['label'], 'order': v['order']}
'field': self._session_get('sort_by'), for k, v in self.sort_modes.iteritems()],
'dir': self._session_get('sort_dir'), 'mode': self._session_get_sort()
} }
return c return c
def get_queryset(self): def get_queryset(self):
qs = super(SortMixin, self).get_queryset() qs = super(SortMixin, self).get_queryset()
if self._session_get('sort_by'): if self._session_get_sort() in self.sort_modes:
qs = qs.order_by('%s%s' % ( mode = self.sort_modes[self._session_get_sort()]
'-' if self._session_get('sort_dir') == 'desc' else '', qs = qs.order_by(*mode['fields'])
self._session_get('sort_by')))
return qs 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> <li>
<a href="?&amp;sort_by=nb_proposals">proposals</a> <a href="?&amp;sort=title-asc">Title A-Z</a>
</li> </li>
--- ---
<li class="disabled"> <li>
<a href="?&amp;sort_by=nb_recommendations">recommendations</a> <a href="?&amp;sort=title-desc">Title Z-A</a>
</li> </li>
--- ---
<li> <li class="disabled">
<a href="?&amp;sort_by=title">title</a> <a href="?&amp;sort=recommendations">Most recommendations</a>
</li> </li>
--- ---
<li> <li>
<a href="?&amp;sort_dir=asc">ascending</a> <a href="?&amp;sort=proposals">Most proposals</a>
</li> </li>
--- \ No newline at end of file
<li class="disabled">
<a href="?&amp;sort_dir=desc">descending</a>
</li>
\ No newline at end of file
<li> <li class="disabled">
<a href="?&amp;sort_by=score__score">score</a> <a href="?&amp;sort=name-asc">Name A-Z</a>
</li> </li>
--- ---
<li class="disabled"> <li>
<a href="?&amp;sort_by=last_name">name</a> <a href="?&amp;sort=name-desc">Name Z-A</a>
</li> </li>
--- ---
<li class="disabled"> <li>
<a href="?&amp;sort_dir=asc">ascending</a> <a href="?&amp;sort=score-desc">Worst score</a>
</li> </li>
--- ---
<li> <li>
<a href="?&amp;sort_dir=desc">descending</a> <a href="?&amp;sort=score-asc">Best score</a>
</li> </li>
\ No newline at end of file \ 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