From ab311e82bc2fc3d7535836013c8f3a1aa186375d Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Sun, 16 Feb 2020 18:35:46 +0000 Subject: [PATCH] Add ability to search by building+room --- search/views.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/search/views.py b/search/views.py index 3eb9deae..b07f079d 100644 --- a/search/views.py +++ b/search/views.py @@ -121,6 +121,12 @@ def search_single_word(word, filters, user, start, end, user_state, aff): ) filter_users = (filter_clubs | Q(name__icontains=word)) + if len(word.split(" ")) >= 2: + # Assume the room is in 1 word, and the building may be in multiple words + building = " ".join(word.split(" ")[:-1]) + room = word.split(" ")[-1] + filter_users |= (Q(room__name__icontains=room) & Q(room__building__name__icontains=building)) + if not User.can_view_all(user)[0]: filter_clubs &= Q(id=user.id) filter_users &= Q(id=user.id) @@ -204,6 +210,13 @@ def search_single_word(word, filters, user, start, end, user_state, aff): filter_rooms = ( Q(details__icontains=word) | Q(name__icontains=word) | Q(port__details=word) ) + + if len(word.split(" ")) >= 2: + # Assume the room is in 1 word, and the building may be in multiple words + building = " ".join(word.split(" ")[:-1]) + room = word.split(" ")[-1] + filter_rooms |= (Q(name__icontains=room) & Q(building__name__icontains=building)) + filters["rooms"] |= filter_rooms # Switch ports @@ -326,6 +339,10 @@ def get_words(query): # If we are between two ", ignore separators words[i] += char continue + if char == "+": + # If we encouter a + outside of ", we replace it with a space + words[i] += " " + continue if char == " " or char == ",": # If we encouter a separator outside of ", we create a new word if words[i] is not "":