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

Fix linting issues

This commit is contained in:
Jean-Romain Garnier 2020-02-19 11:06:57 +01:00 committed by Gabriel Detraz
parent 9cc017a0ea
commit 9f74655cbd
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")
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):

View file

@ -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,

View file

@ -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"

View file

@ -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})