mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-30 08:32:26 +00:00
Sort : 1er example de sort sur la col prénom de users/index
This commit is contained in:
parent
e404f59b63
commit
f88c65c388
4 changed files with 78 additions and 2 deletions
|
@ -139,3 +139,36 @@ def all_active_interfaces_count():
|
||||||
def all_active_assigned_interfaces_count():
|
def all_active_assigned_interfaces_count():
|
||||||
""" Version light seulement pour compter"""
|
""" Version light seulement pour compter"""
|
||||||
return all_active_interfaces_count().filter(ipv4__isnull=False)
|
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)
|
||||||
|
|
37
templates/buttons/sort.html
Normal file
37
templates/buttons/sort.html
Normal 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 }}
|
||||||
|
<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 %}
|
|
@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
<table class="table table-striped">
|
<table class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Prénom</th>
|
<th>{% include "buttons/sort.html" with col="prenom" text="Prénom" %}</th>
|
||||||
<th>Nom</th>
|
<th>Nom</th>
|
||||||
<th>Pseudo</th>
|
<th>Pseudo</th>
|
||||||
<th>Chambre</th>
|
<th>Chambre</th>
|
||||||
|
|
|
@ -65,7 +65,7 @@ from machines.models import Machine
|
||||||
from preferences.models import OptionalUser, GeneralOption
|
from preferences.models import OptionalUser, GeneralOption
|
||||||
|
|
||||||
from re2o.views import form
|
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):
|
def password_change_action(u_form, user, request, req=False):
|
||||||
""" Fonction qui effectue le changeemnt de mdp bdd"""
|
""" Fonction qui effectue le changeemnt de mdp bdd"""
|
||||||
|
@ -576,6 +576,12 @@ def index(request):
|
||||||
options, _created = GeneralOption.objects.get_or_create()
|
options, _created = GeneralOption.objects.get_or_create()
|
||||||
pagination_number = options.pagination_number
|
pagination_number = options.pagination_number
|
||||||
users_list = User.objects.select_related('room').order_by('state', 'name')
|
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)
|
paginator = Paginator(users_list, pagination_number)
|
||||||
page = request.GET.get('page')
|
page = request.GET.get('page')
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in a new issue