From 7817dd47a5f646cbd060d2ef4d56801f2825588e Mon Sep 17 00:00:00 2001 From: Jean-Roman Garnier Date: Fri, 6 Dec 2019 10:29:10 +0000 Subject: [PATCH 1/2] Fix display filter in advance search not being taken into account --- search/views.py | 67 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/search/views.py b/search/views.py index bcfdaeee..d6f7feeb 100644 --- a/search/views.py +++ b/search/views.py @@ -230,6 +230,60 @@ def search_single_word(word, filters, user, start, end, user_state, aff): return filters +def apply_filters(filters, user, aff): + """ Apply the filters constructed by search_single_word. + It also takes into account the visual filters defined during + the search query. + """ + # Results are later filled-in depending on the display filter + results = { + "users": Adherent.objects.none(), + "clubs": Club.objects.none(), + "machines": Machine.objects.none(), + "factures": Facture.objects.none(), + "bans": Ban.objects.none(), + "whitelists": Whitelist.objects.none(), + "rooms": Room.objects.none(), + "ports": Port.objects.none(), + "switches": Switch.objects.none(), + } + + # Users and clubs + if "0" in aff: + results["users"] = Adherent.objects.filter(filters["users"]) + results["clubs"] = Club.objects.filter(filters["clubs"]) + + # Machines + if "1" in aff: + results["machines"] = Machine.objects.filter(filters["machines"]) + + # Factures + if "2" in aff: + results["factures"] = Facture.objects.filter(filters["factures"]) + + # Bans + if "3" in aff: + results["bans"] = Ban.objects.filter(filters["bans"]) + + # Whitelists + if "4" in aff: + results["whitelists"] = Whitelist.objects.filter(filters["whitelists"]) + + # Rooms + if "5" in aff and Room.can_view_all(user): + results["rooms"] = Room.objects.filter(filters["rooms"]) + + # Switch ports + if "6" in aff and User.can_view_all(user): + results["ports"] = Port.objects.filter(filters["ports"]) + + # Switches + if "7" in aff and Switch.can_view_all(user): + results["switches"] = Switch.objects.filter(filters["switches"]) + + return results + + def get_words(query): """Function used to split the uery in different words to look for. The rules are simple : @@ -304,18 +358,7 @@ def get_results(query, request, params): word, filters, request.user, start, end, user_state, aff ) - results = { - "users": Adherent.objects.filter(filters["users"]), - "clubs": Club.objects.filter(filters["clubs"]), - "machines": Machine.objects.filter(filters["machines"]), - "factures": Facture.objects.filter(filters["factures"]), - "bans": Ban.objects.filter(filters["bans"]), - "whitelists": Whitelist.objects.filter(filters["whitelists"]), - "rooms": Room.objects.filter(filters["rooms"]), - "ports": Port.objects.filter(filters["ports"]), - "switches": Switch.objects.filter(filters["switches"]), - } - + results = apply_filters(filters, request.user, aff) results = finish_results(results, request.GET.get("col"), request.GET.get("order")) results.update({"search_term": query}) From 7f4c7962499ac3a2c0fc17b15e093f4eb5bb54e4 Mon Sep 17 00:00:00 2001 From: Jean-Roman Garnier Date: Fri, 6 Dec 2019 13:24:09 +0000 Subject: [PATCH 2/2] Tweak filtering for users and clubs in search to better handle user-defined filters --- search/views.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/search/views.py b/search/views.py index d6f7feeb..c95d131f 100644 --- a/search/views.py +++ b/search/views.py @@ -5,6 +5,7 @@ # Copyright © 2017 Gabriel Détraz # Copyright © 2017 Lara Kermarec # Copyright © 2017 Augustin Lemesle +# Copyright © 2019 Jean-Romain Garnier # # 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 @@ -109,17 +110,22 @@ def search_single_word(word, filters, user, start, end, user_state, aff): # Users if "0" in aff: - filter_users = ( + filter_clubs = ( Q(surname__icontains=word) | Q(pseudo__icontains=word) | Q(room__name__icontains=word) | Q(email__icontains=word) | Q(telephone__icontains=word) - ) & Q(state__in=user_state) + ) + filter_users = (filter_clubs | Q(name__icontains=word)) + if not User.can_view_all(user)[0]: + filter_clubs &= Q(id=user.id) filter_users &= Q(id=user.id) - filter_clubs = filter_users - filter_users |= Q(name__icontains=word) + + filter_clubs &= Q(state__in=user_state) + filter_users &= Q(state__in=user_state) + filters["users"] |= filter_users filters["clubs"] |= filter_clubs