Commit f88c65c3 authored by Maël Kervella's avatar Maël Kervella
Browse files

Sort : 1er example de sort sur la col prénom de users/index

parent e404f59b
......@@ -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.
'prenom' : 'name',
'nom' : 'surname',
'pseudo' : 'pseudo',
'chamber' : 'room',
'default' : 'pseudo'
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
if order == 'desc':
return request.order_by(model_field).reverse()
return request.order_by(model_field)
{% 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
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 %}
<div style="display: flex; padding: 0;">
{{ text }} &nbsp;
<div style="display: grid; font-size: 9px; line-height: 1;">
<a role="button" href="?col={{ col }}" title="{{ desc|default:"Tri croissant" }}">
<span class="glyphicon glyphicon-triangle-top"></span>
<a role="button" href="?col={{ col }}&order=desc" title="{{ desc|default:"Tri décroissant" }}">
<span class="glyphicon glyphicon-triangle-bottom"></span>
{% endspaceless %}
......@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<table class="table table-striped">
<th>{% include "buttons/sort.html" with col="prenom" text="Prénom" %}</th>
......@@ -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(
paginator = Paginator(users_list, pagination_number)
page = request.GET.get('page')
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