From 742d905e83bae6cafa8d2e2171d1c5b0c48e499b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Thu, 2 Nov 2017 00:25:24 +0000 Subject: [PATCH] Ajoute le sort des colonnes sur la recherche --- re2o/templatetags/url_insert_param.py | 21 ++++--- search/forms.py | 21 ++----- search/views.py | 80 +++++++++++++++++++-------- 3 files changed, 73 insertions(+), 49 deletions(-) diff --git a/re2o/templatetags/url_insert_param.py b/re2o/templatetags/url_insert_param.py index 7e37a71b..90f7a0a6 100644 --- a/re2o/templatetags/url_insert_param.py +++ b/re2o/templatetags/url_insert_param.py @@ -36,7 +36,9 @@ def url_insert_param(url="", **kwargs): Return the URL with some specific parameters inserted into the query part. If a URL has already some parameters, those requested will be modified if already exisiting or will be added and the other parameters - will stay unmodified. + will stay unmodified. If parameters with the same name are already in the + URL and a value is specified for this parameter, it will replace all + existing parameters. **Tag name**:: @@ -82,19 +84,22 @@ def url_insert_param(url="", **kwargs): # Get existing parameters in the url params = {} if '?' in url: - url, params = url.split('?', maxsplit=1) - params = { - p[:p.find('=')]: p[p.find('=')+1:] for p in params.split('&') - } + url, parameters = url.split('?', maxsplit=1) + for parameter in parameters.split('&'): + p_name, p_value = parameter.split('=', maxsplit=1) + if p_name not in params: + params[p_name] = [] + params[p_name].append(p_value) # Add the request parameters to the list of parameters for key, value in kwargs.items(): - params[key] = value + params[key] = [value] # Write the url url += '?' - for param, value in params.items(): - url += str(param) + '=' + str(value) + '&' + for param, value_list in params.items(): + for value in value_list: + url += str(param) + '=' + str(value) + '&' # Remove the last '&' (or '?' if no parameters) return url[:-1] diff --git a/search/forms.py b/search/forms.py index c01629f5..23d0acc2 100644 --- a/search/forms.py +++ b/search/forms.py @@ -22,10 +22,8 @@ from __future__ import unicode_literals -from django.db import models from django import forms from django.forms import Form -from django.forms import ModelForm CHOICES_USER = ( ('0', 'Actifs'), @@ -43,22 +41,19 @@ CHOICES_AFF = ( ('5', 'Ports'), ) + def initial_choices(c): return [i[0] for i in c] class SearchForm(Form): - q = forms.CharField(label = 'Search', max_length = 100) - - def clean(self): - cleaned_data = super(SearchForm, self).clean() - q = cleaned_data.get('q', '') + q = forms.CharField(label='Search', max_length=100) class SearchFormPlus(Form): q = forms.CharField( - label = 'Search', - max_length = 100, + label='Search', + max_length=100, required=False ) u = forms.MultipleChoiceField( @@ -87,11 +82,3 @@ class SearchFormPlus(Form): input_formats=['%d/%m/%Y'], label="Date de fin" ) - - def clean(self): - cleaned_data = super(SearchFormPlus, self).clean() - q = cleaned_data.get('q') - u = cleaned_data.get('u') - a = cleaned_data.get('a') - s = cleaned_data.get('s') - e = cleaned_data.get('e') diff --git a/search/views.py b/search/views.py index c1e06b98..5846d946 100644 --- a/search/views.py +++ b/search/views.py @@ -45,6 +45,7 @@ from search.forms import ( CHOICES_AFF, initial_choices ) +from re2o.utils import SortTable def get_results(query, request, filters={}): start = filters.get('s', None) @@ -96,9 +97,13 @@ def get_results(query, request, filters={}): ) & user_state_filter if not request.user.has_perms(('cableur',)): filter_user_list &= Q(id=request.user.id) - results['users_list'] = User.objects.filter( - filter_user_list - ).order_by('state', 'surname').distinct()[:max_result] + results['users_list'] = User.objects.filter(filter_user_list) + results['users_list'] = SortTable.sort( + results['users_list'], + request.GET.get('col'), + request.GET.get('order'), + SortTable.USERS_INDEX + ) # Machines if '1' in aff: @@ -119,20 +124,28 @@ def get_results(query, request, filters={}): ) if not request.user.has_perms(('cableur',)): filter_machine_list &= Q(machine__user__id=request.user.id) - results['machines_list'] = Machine.objects.filter( - filter_machine_list - ).order_by('name').distinct()[:max_result] + results['machines_list'] = Machine.objects.filter(filter_machine_list) + results['machines_list'] = SortTable.sort( + results['machines_list'], + request.GET.get('col'), + request.GET.get('order'), + SortTable.MACHINES_INDEX + ) # Factures if '2' in aff: - date_filter = Q() + filter_facture_list = users_filter if start != None: - date_filter &= Q(date__gte=start) + filter_facture_list &= Q(date__gte=start) if end != None: - date_filter &= Q(date__lte=end) - results['facture_list'] = Facture.objects.filter( - users_filter & date_filter - ).order_by('date').distinct()[:max_result] + filter_facture_list &= Q(date__lte=end) + results['facture_list'] = Facture.objects.filter(filter_facture_list) + results['facture_list'] = SortTable.sort( + results['facture_list'], + request.GET.get('col'), + request.GET.get('order'), + SortTable.COTISATIONS_INDEX + ) # Bans if '3' in aff: @@ -153,9 +166,13 @@ def get_results(query, request, filters={}): ) | ( Q(date_start__gte=end) & Q(date_end__lte=end) ) - results['ban_list'] = Ban.objects.filter( - users_filter & date_filter - ).order_by('date_end').distinct()[:max_result] + results['ban_list'] = Ban.objects.filter(users_filter & date_filter) + results['ban_list'] = SortTable.sort( + results['ban_list'], + request.GET.get('col'), + request.GET.get('order'), + SortTable.USERS_INDEX_BAN + ) # Whitelists if '4' in aff: @@ -176,21 +193,36 @@ def get_results(query, request, filters={}): ) | ( Q(date_start__gte=end) & Q(date_end__lte=end) ) - results['white_list'] = Whitelist.objects.filter( - users_filter & date_filter - ).order_by('date_end').distinct()[:max_result] + results['white_list'] = Whitelist.objects.filter(users_filter & date_filter) + results['white_list'] = SortTable.sort( + results['white_list'], + request.GET.get('col'), + request.GET.get('order'), + SortTable.USERS_INDEX_WHITE + ) # Switch ports if '5' in aff and request.user.has_perms(('cableur',)): - results['port_list'] = Port.objects.filter( - details__icontains=query - ).order_by('switch', 'port').distinct()[:max_result] + results['port_list'] = Port.objects.filter(details__icontains=query) + results['port_list'] = SortTable.sort( + results['port_list'], + request.GET.get('col'), + request.GET.get('order'), + SortTable.TOPOLOGIE_INDEX_PORT + ) # Switches if '6' in aff and request.user.has_perms(('cableur')): - results['switch_list'] = Switch.objects.filter( - details__icontains=query - ).order_by('stack', 'stack_member_id').distinct()[:max_result] + results['switch_list'] = Switch.objects.filter(details__icontains=query) + results['switch_list'] = SortTable.sort( + results['switch_list'], + request.GET.get('col'), + request.GET.get('order'), + SortTable.TOPOLOGIE_INDEX + ) + + for r in results.keys(): + results[r] = results[r].distinct()[:max_result] results.update({'max_result': max_result})