8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00

Fix linting issues

This commit is contained in:
Jean-Romain Garnier 2020-02-19 11:06:57 +01:00
parent 870523b439
commit a708fe2dda
4 changed files with 70 additions and 35 deletions

View file

@ -144,7 +144,10 @@ def finish_results(request, results, col, order):
max_result = GeneralOption.get_cached_value("search_display_page") max_result = GeneralOption.get_cached_value("search_display_page")
for name, val in results.items(): for name, val in results.items():
page_arg = name + "_page" 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}) results.update({"max_result": max_result})
@ -162,7 +165,8 @@ def contains_filter(attribute, word, case_sensitive=False):
return Q(**{attr: word}) 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 """ Construct the correct filters to match differents fields of some models
with the given query according to the given filters. with the given query according to the given filters.
The match field are either CharField or IntegerField that will be displayed 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("pseudo", word, case_sensitive)
| contains_filter("email", word, case_sensitive) | contains_filter("email", word, case_sensitive)
| contains_filter("telephone", word, case_sensitive) | contains_filter("telephone", word, case_sensitive)
| contains_filter("room_full_name", word, case_sensitive) # Added through annotate # Added through annotate
| contains_filter("room_full_name_stuck", word, case_sensitive) # 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 # 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]: if not User.can_view_all(user)[0]:
filter_clubs &= Q(id=user.id) 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: if "1" in aff:
filter_machines = ( filter_machines = (
contains_filter("name", word, case_sensitive) 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__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__mac_address", word, case_sensitive)
| contains_filter("interface__ipv4__ipv4", word, case_sensitive) | contains_filter("interface__ipv4__ipv4", word, case_sensitive)
) )
try: try:
_mac_addr = EUI(word, 48) _ = EUI(word, 48)
filter_machines |= Q(interface__mac_address=word) filter_machines |= Q(interface__mac_address=word)
except AddrFormatError: except AddrFormatError:
pass 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): if "5" in aff and Room.can_view_all(user):
filter_rooms = ( filter_rooms = (
contains_filter("details", word, case_sensitive) contains_filter("details", word, case_sensitive)
| contains_filter("full_name", word, case_sensitive) # Added through annotate # Added through annotate
| contains_filter("full_name_stuck", word, case_sensitive) # Added through annotate | contains_filter("full_name", word, case_sensitive)
| contains_filter("full_name_stuck", word, case_sensitive)
| Q(port__details=word) | Q(port__details=word)
) )
filters["rooms"] |= filter_rooms filters["rooms"] |= filter_rooms
@ -278,13 +289,17 @@ def search_single_word(word, filters, user, start, end, user_state, aff, case_se
# Switch ports # Switch ports
if "6" in aff and User.can_view_all(user): if "6" in aff and User.can_view_all(user):
filter_ports = ( filter_ports = (
contains_filter("room_full_name", word, case_sensitive) # Added through annotate contains_filter("machine_interface__domain__name",
| contains_filter("room_full_name_stuck", word, case_sensitive) # Added through annotate word, case_sensitive)
| contains_filter("machine_interface__domain__name", word, case_sensitive) | contains_filter("related__switch__interface__domain__name",
| contains_filter("related__switch__interface__domain__name", word, case_sensitive) word, case_sensitive)
| contains_filter("custom_profile__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) | 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): if is_int(word):
filter_ports |= Q(port=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 = ( filter_switches = (
contains_filter("interface__domain__name", word, case_sensitive) contains_filter("interface__domain__name", word, case_sensitive)
| contains_filter("interface__ipv4__ipv4", 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("stack__name", word, case_sensitive)
| contains_filter("model__reference", word, case_sensitive) | contains_filter("model__reference", word, case_sensitive)
| contains_filter("model__constructor__name", word, case_sensitive) | contains_filter("model__constructor__name", word, case_sensitive)
@ -314,10 +330,10 @@ def apply_filters(filters, user, aff):
the search query. the search query.
""" """
# Results are later filled-in depending on the display filter # Results are later filled-in depending on the display filter
# In some cases, annotations are used to match what is displayed in the results # In some cases, annotations are used to match what is displayed in the
# For example, the displayed room is actually "room__building__name room__name" # results. For example, the displayed room is actually
# So queries wouldn't match what the user expects if we just kept the # "room__building__name room__name", so queries wouldn't match what the
# database's format # user expects if we just kept the database's format
results = { results = {
"users": Adherent.objects.none(), "users": Adherent.objects.none(),
"clubs": Club.objects.none(), "clubs": Club.objects.none(),
@ -333,11 +349,13 @@ def apply_filters(filters, user, aff):
# Users and clubs # Users and clubs
if "0" in aff: if "0" in aff:
results["users"] = Adherent.objects.annotate( 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"), room_full_name_stuck=Concat("room__building__name", "room__name"),
).filter(filters["users"]) ).filter(filters["users"])
results["clubs"] = Club.objects.annotate( 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"), room_full_name_stuck=Concat("room__building__name", "room__name"),
).filter(filters["clubs"]) ).filter(filters["clubs"])
@ -367,7 +385,8 @@ def apply_filters(filters, user, aff):
# Switch ports # Switch ports
if "6" in aff and User.can_view_all(user): if "6" in aff and User.can_view_all(user):
results["ports"] = Port.objects.annotate( 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"), room_full_name_stuck=Concat("room__building__name", "room__name"),
).filter(filters["ports"]) ).filter(filters["ports"])
@ -386,7 +405,8 @@ def search_single_query(query, filters, user, start, end, user_state, aff):
newfilters = empty_filters() newfilters = empty_filters()
for q in query.subqueries: for q in query.subqueries:
# Construct an independent filter for each subquery # 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 # Apply the subfilter
for field in filter_fields(): for field in filter_fields():
@ -399,7 +419,8 @@ def search_single_query(query, filters, user, start, end, user_state, aff):
return filters return filters
# Handle standard queries # 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): def create_queries(query):

View file

@ -63,7 +63,8 @@ class SearchForm(Form):
help_text=( help_text=(
_( _(
'Use « » and «,» to specify distinct words, «"query"» for' '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, max_length=100,
@ -78,7 +79,8 @@ class SearchFormPlus(Form):
help_text=( help_text=(
_( _(
'Use « » and «,» to specify distinct words, «"query"» for' '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, max_length=100,

View file

@ -89,11 +89,13 @@ msgstr "Rechercher"
#: search/forms.py:65 search/forms.py:80 #: search/forms.py:65 search/forms.py:80
msgid "" msgid ""
"Use « » and «,» to specify distinct words, «\"query\"» for" "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."
msgstr "" msgstr ""
"Utilisez « » et «,» pour spécifier différents mots, «\"recherche\"» pour une " "Utilisez « » et «,» pour spécifier différents mots, «\"recherche\"» pour"
"recherche exacte, «\\» pour échapper un caractère et «+» pour combiner des mots clés." " une recherche exacte, «\\» pour échapper un caractère et «+» pour"
" combiner des mots clés."
#: search/forms.py:88 #: search/forms.py:88
msgid "Users filter" msgid "Users filter"

View file

@ -43,7 +43,8 @@ from search.forms import (
) )
from re2o.acl import can_view_all 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): def get_results(query, request, params):
@ -66,7 +67,12 @@ def get_results(query, request, params):
) )
results = apply_filters(filters, request.user, aff) 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}) results.update({"search_term": query})
return results return results
@ -82,7 +88,9 @@ def search(request):
request, request,
"search/index.html", "search/index.html",
get_results( 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}) return render(request, "search/search.html", {"search_form": search_form})
@ -98,7 +106,9 @@ def searchp(request):
request, request,
"search/index.html", "search/index.html",
get_results( 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}) return render(request, "search/search.html", {"search_form": search_form})