From f139b5b2c4c1627fdbce99c6541cfc7ecde46eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sun, 22 Oct 2017 01:37:27 +0000 Subject: [PATCH] Sort : support des order_by sur plusieurs fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit et delete de deux order_by oublié dans les views --- re2o/utils.py | 126 ++++++++++++++++++++++----------------------- topologie/views.py | 2 +- users/views.py | 2 +- 3 files changed, 65 insertions(+), 65 deletions(-) diff --git a/re2o/utils.py b/re2o/utils.py index adf51ff0..5c95c47a 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -145,96 +145,96 @@ class SortTable: to the column and order requested. It's used with a dict of possible values and associated model_fields """ - # All the possible criteria possible + # All the possible possible values # 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. + # The syntax to describe the sort to apply is a dict where the keys are + # the url value and the values are a list of model field name to use to + # order the request. They are applied in the order they are given. + # A 'default' might be provided to specify what to do if the requested col + # doesn't match any keys. USERS_INDEX = { - 'name': 'name', - 'surname': 'surname', - 'pseudo': 'pseudo', - 'room': 'room', - 'default': 'pseudo' + 'name': ['name'], + 'surname': ['surname'], + 'pseudo': ['pseudo'], + 'room': ['room'], + 'default': ['state', 'pseudo'] } USERS_INDEX_BAN = { - 'user': 'user__pseudo', - 'start': 'date_start', - 'end': 'date_end', - 'default': 'date_end' + 'user': ['user__pseudo'], + 'start': ['date_start'], + 'end': ['date_end'], + 'default': ['-date_end'] } USERS_INDEX_WHITE = { - 'user': 'user__pseudo', - 'start': 'date_start', - 'end': 'date_end', - 'default': 'date_end' + 'user': ['user__pseudo'], + 'start': ['date_start'], + 'end': ['date_end'], + 'default': ['-date_end'] } MACHINES_INDEX = { - 'name': 'name', - 'default': 'pk' + 'name': ['name'], + 'default': ['pk'] } COTISATIONS_INDEX = { - 'user': 'user__pseudo', - 'paiement': 'paiement__moyen', - 'date': 'date', - 'default': 'date' + 'user': ['user__pseudo'], + 'paiement': ['paiement__moyen'], + 'date': ['date'], + 'default': ['-date'] } COTISATIONS_CONTROL = { - 'name': 'user__name', - 'surname': 'user__surname', - 'paiement': 'paiement', - 'date': 'date', - 'valid': 'valid', - 'control': 'control', - 'default': 'date' + 'name': ['user__name'], + 'surname': ['user__surname'], + 'paiement': ['paiement'], + 'date': ['date'], + 'valid': ['valid'], + 'control': ['control'], + 'default': ['-date'] } TOPOLOGIE_INDEX = { - 'dns': 'switch_interface__domain__name', - 'ip': 'switch_interface__ipv4__ipv4', - 'loc': 'location', - 'ports': 'number', - 'stack': 'stack__name', - 'default': 'switch_interface__domain__name' + 'dns': ['switch_interface__domain__name'], + 'ip': ['switch_interface__ipv4__ipv4'], + 'loc': ['location'], + 'ports': ['number'], + 'stack': ['stack__name'], + 'default': ['location', 'stack', 'stack_member_id'] } TOPOLOGIE_INDEX_PORT = { - 'port': 'port', - 'room': 'room__name', - 'interface': 'machine_interface__domain__name', - 'related': 'related__switch__name', - 'radius': 'radius', - 'vlan': 'vlan_force__name', - 'default': 'port' + 'port': ['port'], + 'room': ['room__name'], + 'interface': ['machine_interface__domain__name'], + 'related': ['related__switch__name'], + 'radius': ['radius'], + 'vlan': ['vlan_force__name'], + 'default': ['port'] } TOPOLOGIE_INDEX_ROOM = { - 'name': 'name', - 'default': 'name' + 'name': ['name'], + 'default': ['name'] } TOPOLOGIE_INDEX_STACK = { - 'name': 'name', - 'id': 'stack_id', - 'default': 'stack_id' + 'name': ['name'], + 'id': ['stack_id'], + 'default': ['stack_id'], } LOGS_INDEX = { - 'date': 'revision__date_created', - 'default': 'revision__date_created' + 'date': ['revision__date_created'], + 'default': ['-revision__date_created'], } LOGS_STATS_LOGS = { - 'author': 'user__name', - 'date': 'date_created', - 'default': 'date_created' + 'author': ['user__name'], + 'date': ['date_created'], + 'default': ['-date_created'] } @staticmethod - def sort(request, col, order, criterion): + def sort(request, col, order, values): """ 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 + fields = values.get(col, None) + if not fields: + fields = values.get('default', []) + request = request.order_by(*fields) + if order == 'desc': + return request.reverse() else: - if order == 'desc': - return request.order_by(model_field).reverse() - else: - return request.order_by(model_field) + return request diff --git a/topologie/views.py b/topologie/views.py index e4f928be..dec681b6 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -157,7 +157,7 @@ def index_port(request, switch_id): @permission_required('cableur') def index_room(request): """ Affichage de l'ensemble des chambres""" - room_list = Room.objects.order_by('name') + room_list = Room.objects room_list = SortTable.sort( room_list, request.GET.get('col'), diff --git a/users/views.py b/users/views.py index 84b9e26d..316c85ea 100644 --- a/users/views.py +++ b/users/views.py @@ -575,7 +575,7 @@ def index(request): """ Affiche l'ensemble des users, need droit cableur """ 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 = User.objects.select_related('room') users_list = SortTable.sort( users_list, request.GET.get('col'),