8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-16 00:13:12 +00:00

Merge branch 'fix_advanced_search_filters' into 'dev'

Fix advanced search filters

See merge request federez/re2o!475
This commit is contained in:
chirac 2019-12-07 14:25:21 +01:00
commit c3fc672f14

View file

@ -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
@ -111,17 +112,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
@ -232,6 +238,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 :
@ -306,18 +366,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(request, results, request.GET.get("col"), request.GET.get("order"))
results.update({"search_term": query})