8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-28 09:53:47 +00:00

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

This commit is contained in:
Maël Kervella 2017-10-21 15:47:42 +00:00
parent e404f59b63
commit f88c65c388
4 changed files with 78 additions and 2 deletions

View file

@ -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)

View file

@ -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 %}
<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>
<a role="button" href="?col={{ col }}&order=desc" title="{{ desc|default:"Tri décroissant" }}">
<span class="glyphicon glyphicon-triangle-bottom"></span>
</a>
</div>
</div>
{% endspaceless %}

View file

@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<table class="table table-striped">
<thead>
<tr>
<th>Prénom</th>
<th>{% include "buttons/sort.html" with col="prenom" text="Prénom" %}</th>
<th>Nom</th>
<th>Pseudo</th>
<th>Chambre</th>

View file

@ -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: