From 9f74655cbd39b9f3419359810c4e9f1d85c6f494 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Wed, 19 Feb 2020 11:06:57 +0100 Subject: [PATCH] Fix linting issues --- search/engine.py | 71 +++++++++++++++++--------- search/forms.py | 6 ++- search/locale/fr/LC_MESSAGES/django.po | 10 ++-- search/views.py | 18 +++++-- 4 files changed, 70 insertions(+), 35 deletions(-) diff --git a/search/engine.py b/search/engine.py index 62ca5e53..3dfd0680 100644 --- a/search/engine.py +++ b/search/engine.py @@ -144,7 +144,10 @@ def finish_results(request, results, col, order): max_result = GeneralOption.get_cached_value("search_display_page") for name, val in results.items(): page_arg = name + "_page" - results[name] = re2o_paginator(request, val.distinct(), max_result, page_arg=page_arg) + results[name] = re2o_paginator(request, + val.distinct(), + max_result, + page_arg=page_arg) results.update({"max_result": max_result}) @@ -162,7 +165,8 @@ def contains_filter(attribute, word, case_sensitive=False): return Q(**{attr: word}) -def search_single_word(word, filters, user, start, end, user_state, aff, case_sensitive=False): +def search_single_word(word, filters, user, start, end, + user_state, aff, case_sensitive=False): """ Construct the correct filters to match differents fields of some models with the given query according to the given filters. The match field are either CharField or IntegerField that will be displayed @@ -177,12 +181,16 @@ def search_single_word(word, filters, user, start, end, user_state, aff, case_se | contains_filter("pseudo", word, case_sensitive) | contains_filter("email", word, case_sensitive) | contains_filter("telephone", word, case_sensitive) - | contains_filter("room_full_name", word, case_sensitive) # Added through annotate - | contains_filter("room_full_name_stuck", word, case_sensitive) # Added through annotate + # Added through annotate + | contains_filter("room_full_name", word, case_sensitive) + | contains_filter("room_full_name_stuck", word, case_sensitive) ) # Users have a name whereas clubs only have a surname - filter_users = (filter_clubs | contains_filter("name", word, case_sensitive)) + filter_users = ( + filter_clubs + | contains_filter("name", word, case_sensitive) + ) if not User.can_view_all(user)[0]: filter_clubs &= Q(id=user.id) @@ -198,14 +206,16 @@ def search_single_word(word, filters, user, start, end, user_state, aff, case_se if "1" in aff: filter_machines = ( contains_filter("name", word, case_sensitive) - | contains_filter("user__pseudo", word, case_sensitive) & Q(user__state__in=user_state) + | (contains_filter("user__pseudo", word, case_sensitive) + & Q(user__state__in=user_state)) | contains_filter("interface__domain__name", word, case_sensitive) - | contains_filter("interface__domain__related_domain__name", word, case_sensitive) + | contains_filter("interface__domain__related_domain__name", + word, case_sensitive) | contains_filter("interface__mac_address", word, case_sensitive) | contains_filter("interface__ipv4__ipv4", word, case_sensitive) ) try: - _mac_addr = EUI(word, 48) + _ = EUI(word, 48) filter_machines |= Q(interface__mac_address=word) except AddrFormatError: pass @@ -269,8 +279,9 @@ def search_single_word(word, filters, user, start, end, user_state, aff, case_se if "5" in aff and Room.can_view_all(user): filter_rooms = ( contains_filter("details", word, case_sensitive) - | contains_filter("full_name", word, case_sensitive) # Added through annotate - | contains_filter("full_name_stuck", word, case_sensitive) # Added through annotate + # Added through annotate + | contains_filter("full_name", word, case_sensitive) + | contains_filter("full_name_stuck", word, case_sensitive) | Q(port__details=word) ) filters["rooms"] |= filter_rooms @@ -278,13 +289,17 @@ def search_single_word(word, filters, user, start, end, user_state, aff, case_se # Switch ports if "6" in aff and User.can_view_all(user): filter_ports = ( - contains_filter("room_full_name", word, case_sensitive) # Added through annotate - | contains_filter("room_full_name_stuck", word, case_sensitive) # Added through annotate - | contains_filter("machine_interface__domain__name", word, case_sensitive) - | contains_filter("related__switch__interface__domain__name", word, case_sensitive) + contains_filter("machine_interface__domain__name", + word, case_sensitive) + | contains_filter("related__switch__interface__domain__name", + word, case_sensitive) | contains_filter("custom_profile__name", word, case_sensitive) - | contains_filter("custom_profile__profil_default", word, case_sensitive) + | contains_filter("custom_profile__profil_default", + word, case_sensitive) | contains_filter("details", word, case_sensitive) + # Added through annotate + | contains_filter("room_full_name", word, case_sensitive) + | contains_filter("room_full_name_stuck", word, case_sensitive) ) if is_int(word): filter_ports |= Q(port=word) @@ -295,7 +310,8 @@ def search_single_word(word, filters, user, start, end, user_state, aff, case_se filter_switches = ( contains_filter("interface__domain__name", word, case_sensitive) | contains_filter("interface__ipv4__ipv4", word, case_sensitive) - | contains_filter("switchbay__building__name", word, case_sensitive) + | contains_filter("switchbay__building__name", + word, case_sensitive) | contains_filter("stack__name", word, case_sensitive) | contains_filter("model__reference", word, case_sensitive) | contains_filter("model__constructor__name", word, case_sensitive) @@ -314,10 +330,10 @@ def apply_filters(filters, user, aff): the search query. """ # Results are later filled-in depending on the display filter - # In some cases, annotations are used to match what is displayed in the results - # For example, the displayed room is actually "room__building__name room__name" - # So queries wouldn't match what the user expects if we just kept the - # database's format + # In some cases, annotations are used to match what is displayed in the + # results. For example, the displayed room is actually + # "room__building__name room__name", so queries wouldn't match what the + # user expects if we just kept the database's format results = { "users": Adherent.objects.none(), "clubs": Club.objects.none(), @@ -333,11 +349,13 @@ def apply_filters(filters, user, aff): # Users and clubs if "0" in aff: results["users"] = Adherent.objects.annotate( - room_full_name=Concat("room__building__name", Value(" "), "room__name"), + room_full_name=Concat("room__building__name", + Value(" "), "room__name"), room_full_name_stuck=Concat("room__building__name", "room__name"), ).filter(filters["users"]) results["clubs"] = Club.objects.annotate( - room_full_name=Concat("room__building__name", Value(" "), "room__name"), + room_full_name=Concat("room__building__name", + Value(" "), "room__name"), room_full_name_stuck=Concat("room__building__name", "room__name"), ).filter(filters["clubs"]) @@ -367,7 +385,8 @@ def apply_filters(filters, user, aff): # Switch ports if "6" in aff and User.can_view_all(user): results["ports"] = Port.objects.annotate( - room_full_name=Concat("room__building__name", Value(" "), "room__name"), + room_full_name=Concat("room__building__name", + Value(" "), "room__name"), room_full_name_stuck=Concat("room__building__name", "room__name"), ).filter(filters["ports"]) @@ -386,7 +405,8 @@ def search_single_query(query, filters, user, start, end, user_state, aff): newfilters = empty_filters() for q in query.subqueries: # Construct an independent filter for each subquery - subfilters = search_single_query(q, empty_filters(), user, start, end, user_state, aff) + subfilters = search_single_query(q, empty_filters(), user, + start, end, user_state, aff) # Apply the subfilter for field in filter_fields(): @@ -399,7 +419,8 @@ def search_single_query(query, filters, user, start, end, user_state, aff): return filters # Handle standard queries - return search_single_word(query.text, filters, user, start, end, user_state, aff, query.case_sensitive) + return search_single_word(query.text, filters, user, start, end, + user_state, aff, query.case_sensitive) def create_queries(query): diff --git a/search/forms.py b/search/forms.py index 1a29ce2d..f6e90cd7 100644 --- a/search/forms.py +++ b/search/forms.py @@ -63,7 +63,8 @@ class SearchForm(Form): help_text=( _( 'Use « » and «,» to specify distinct words, «"query"» for' - " an exact search, «\\» to escape a character and «+» to combine keywords." + " search, «\\» to escape a character and «+» to" + " combine keywords." ) ), max_length=100, @@ -78,7 +79,8 @@ class SearchFormPlus(Form): help_text=( _( 'Use « » and «,» to specify distinct words, «"query"» for' - " an exact search, «\\» to escape a character and «+» to combine keywords." + " an exact search, «\\» to escape a character and «+» to" + " combine keywords." ) ), max_length=100, diff --git a/search/locale/fr/LC_MESSAGES/django.po b/search/locale/fr/LC_MESSAGES/django.po index 356c1ae4..5afe48c4 100644 --- a/search/locale/fr/LC_MESSAGES/django.po +++ b/search/locale/fr/LC_MESSAGES/django.po @@ -89,11 +89,13 @@ msgstr "Rechercher" #: search/forms.py:65 search/forms.py:80 msgid "" -"Use « » and «,» to specify distinct words, «\"query\"» for" -" an exact search, «\\» to escape a character and «+» to combine keywords." +"Use « » and «,» to specify distinct words, «"query"» for" +" search, «\\» to escape a character and «+» to" +" combine keywords." msgstr "" -"Utilisez « » et «,» pour spécifier différents mots, «\"recherche\"» pour une " -"recherche exacte, «\\» pour échapper un caractère et «+» pour combiner des mots clés." +"Utilisez « » et «,» pour spécifier différents mots, «\"recherche\"» pour" +" une recherche exacte, «\\» pour échapper un caractère et «+» pour" +" combiner des mots clés." #: search/forms.py:88 msgid "Users filter" diff --git a/search/views.py b/search/views.py index 60d0a92f..a994240f 100644 --- a/search/views.py +++ b/search/views.py @@ -43,7 +43,8 @@ from search.forms import ( ) from re2o.acl import can_view_all -from .engine import * +from .engine import empty_filters, create_queries, search_single_query +from .engine import apply_filters, finish_results def get_results(query, request, params): @@ -66,7 +67,12 @@ def get_results(query, request, params): ) results = apply_filters(filters, request.user, aff) - results = finish_results(request, results, request.GET.get("col"), request.GET.get("order")) + results = finish_results( + request, + results, + request.GET.get("col"), + request.GET.get("order") + ) results.update({"search_term": query}) return results @@ -82,7 +88,9 @@ def search(request): request, "search/index.html", get_results( - search_form.cleaned_data.get("q", ""), request, search_form.cleaned_data + search_form.cleaned_data.get("q", ""), + request, + search_form.cleaned_data ), ) return render(request, "search/search.html", {"search_form": search_form}) @@ -98,7 +106,9 @@ def searchp(request): request, "search/index.html", get_results( - search_form.cleaned_data.get("q", ""), request, search_form.cleaned_data + search_form.cleaned_data.get("q", ""), + request, + search_form.cleaned_data ), ) return render(request, "search/search.html", {"search_form": search_form})