From 06c2e81392bda23d92b5c4f94b1c12dba5eecc24 Mon Sep 17 00:00:00 2001 From: bootandy Date: Sun, 7 Jul 2013 15:32:44 +0200 Subject: [PATCH] allow sort by field --- djadmin2/static/themes/bootstrap/css/base.css | 9 +++++++ .../templates/djadmin2/bootstrap/base.html | 1 + .../djadmin2/bootstrap/model_list.html | 21 ++++++++++++---- djadmin2/views.py | 25 +++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 djadmin2/static/themes/bootstrap/css/base.css diff --git a/djadmin2/static/themes/bootstrap/css/base.css b/djadmin2/static/themes/bootstrap/css/base.css new file mode 100644 index 0000000..773ef1b --- /dev/null +++ b/djadmin2/static/themes/bootstrap/css/base.css @@ -0,0 +1,9 @@ +.sort_link { + display: block; + cursor: pointer; + color: black; +} + +.sort_link:hover { + color: black; +} diff --git a/djadmin2/templates/djadmin2/bootstrap/base.html b/djadmin2/templates/djadmin2/bootstrap/base.html index 46f3ded..9def467 100644 --- a/djadmin2/templates/djadmin2/bootstrap/base.html +++ b/djadmin2/templates/djadmin2/bootstrap/base.html @@ -9,6 +9,7 @@ {% block css %} + {% endblock css %} diff --git a/djadmin2/templates/djadmin2/bootstrap/model_list.html b/djadmin2/templates/djadmin2/bootstrap/model_list.html index 32bfc42..75b857a 100644 --- a/djadmin2/templates/djadmin2/bootstrap/model_list.html +++ b/djadmin2/templates/djadmin2/bootstrap/model_list.html @@ -70,11 +70,22 @@ {% for attr in view.model_admin.list_display %} - {% if forloop.first and attr == "__str__" %} - {{ model_name|capfirst }} - {% else %} - {{ model|model_attr_verbose_name:attr|capfirst }} - {% endif%} + + {# comment if we sorted on this field last time invert the sort #} + {% if sort_term == attr %} + + {% else %} + + {% endif %} + + {% if forloop.first and attr == "__str__" %} + {{ model_name|capfirst }} + {% else %} + {{ model|model_attr_verbose_name:attr|capfirst }} + {% endif %} + + + {% endfor %} diff --git a/djadmin2/views.py b/djadmin2/views.py index b869805..1617d05 100644 --- a/djadmin2/views.py +++ b/djadmin2/views.py @@ -15,6 +15,7 @@ from django.http import HttpResponseRedirect from django.utils.encoding import force_text from django.utils.text import capfirst from django.views import generic +from django.db.models.fields import FieldDoesNotExist import extra_views @@ -134,11 +135,34 @@ class ModelListView(AdminModel2Mixin, generic.ListView): if self.model_admin.list_filter: queryset = self.build_list_filter(queryset).qs + queryset = self._modify_queryset_for_sort(queryset) + if search_use_distinct: return queryset.distinct() else: return queryset + def _modify_queryset_for_sort(self, queryset): + # If we are sorting AND the field exists on the model + sort_by = self.request.GET.get('sort', None) + if sort_by: + # Special case when we are not explicityly displaying fields + if sort_by == '-__str__': + queryset = queryset[::-1] + try: + # If we sort on '-' remove it before looking for that field + field_exists = sort_by + if field_exists[0] == '-': + field_exists = field_exists[1:] + + options = utils.model_options(self.model) + options.get_field(field_exists) + queryset = queryset.order_by(sort_by) + except FieldDoesNotExist: + # If the field does not exist then we dont sort on it + pass + return queryset + def build_list_filter(self, queryset=None): if not hasattr(self, '_list_filter'): if queryset is None: @@ -157,6 +181,7 @@ class ModelListView(AdminModel2Mixin, generic.ListView): context['search_fields'] = self.get_search_fields() context['search_term'] = self.request.GET.get('q', '') context['list_filter'] = self.build_list_filter() + context['sort_term'] = self.request.GET.get('sort', '') return context def get_success_url(self):