From f097e7258aefe6d1a129c23048501cc157e71b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 21 Oct 2017 15:47:42 +0000 Subject: [PATCH] =?UTF-8?q?Sort=20:=201er=20example=20de=20sort=20sur=20la?= =?UTF-8?q?=20col=20pr=C3=A9nom=20de=20users/index?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- re2o/utils.py | 33 +++++++++++++++++++++++++ templates/buttons/sort.html | 37 ++++++++++++++++++++++++++++ users/templates/users/aff_users.html | 2 +- users/views.py | 8 +++++- 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 templates/buttons/sort.html diff --git a/re2o/utils.py b/re2o/utils.py index 8abee181..f6f8cb30 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -139,3 +139,36 @@ def all_active_interfaces_count(): def all_active_assigned_interfaces_count(): """ Version light seulement pour compter""" return all_active_interfaces_count().filter(ipv4__isnull=False) + +class SortTable: + """ Class gathering uselful stuff to sort the colums of a table, according + to the column and order requested. It's used with a dict of possible + values and associated model_fields """ + + # All the possible criteria possible + # The naming convention is based on the URL or the views function + # The syntax is the url value as a key and the associated model field name + # to use as order field in the request. A 'default' might be provided to + # specify what to do if the requested col doesn't match any keys. + USERS_INDEX = { + 'prenom' : 'name', + 'nom' : 'surname', + 'pseudo' : 'pseudo', + 'chamber' : 'room', + 'default' : 'pseudo' + } + + @staticmethod + def sort(request, col, order, criterion): + """ Check if the given values are possible and add .order_by() and + a .reverse() as specified according to those values """ + model_field = criterion.get(col, None) + if not model_field: + model_field = criterion.get('default', None) + if not model_field: + return request + else: + if order == 'desc': + return request.order_by(model_field).reverse() + else: + return request.order_by(model_field) diff --git a/templates/buttons/sort.html b/templates/buttons/sort.html new file mode 100644 index 00000000..48214913 --- /dev/null +++ b/templates/buttons/sort.html @@ -0,0 +1,37 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% spaceless %} +
+ {{ text }}   +
+ + + + + + +
+
+{% endspaceless %} diff --git a/users/templates/users/aff_users.html b/users/templates/users/aff_users.html index b4ded1f2..aa47f293 100644 --- a/users/templates/users/aff_users.html +++ b/users/templates/users/aff_users.html @@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., - + diff --git a/users/views.py b/users/views.py index 5b0b3910..17c9c872 100644 --- a/users/views.py +++ b/users/views.py @@ -65,7 +65,7 @@ from machines.models import Machine from preferences.models import OptionalUser, GeneralOption from re2o.views import form -from re2o.utils import all_has_access +from re2o.utils import all_has_access, SortTable def password_change_action(u_form, user, request, req=False): """ Fonction qui effectue le changeemnt de mdp bdd""" @@ -576,6 +576,12 @@ def index(request): options, _created = GeneralOption.objects.get_or_create() pagination_number = options.pagination_number users_list = User.objects.select_related('room').order_by('state', 'name') + users_list = SortTable.sort( + users_list, + request.GET.get('col'), + request.GET.get('order'), + SortTable.USERS_INDEX + ) paginator = Paginator(users_list, pagination_number) page = request.GET.get('page') try:
Prénom{% include "buttons/sort.html" with col="prenom" text="Prénom" %} Nom Pseudo Chambre