2020-11-23 16:06:37 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
|
2017-01-15 23:01:18 +00:00
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2017 Gabriel Détraz
|
2019-09-29 14:02:28 +00:00
|
|
|
# Copyright © 2017 Lara Kermarec
|
2017-01-15 23:01:18 +00:00
|
|
|
# Copyright © 2017 Augustin Lemesle
|
2019-12-06 10:29:10 +00:00
|
|
|
# Copyright © 2019 Jean-Romain Garnier
|
2017-01-15 23:01:18 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2017-11-02 19:58:20 +00:00
|
|
|
"""The views for the search app, responsible for finding the matches
|
2019-09-29 14:02:28 +00:00
|
|
|
Augustin lemesle, Gabriel Détraz, Lara Kermarec, Maël Kervella
|
2017-11-02 19:58:20 +00:00
|
|
|
Gplv2"""
|
|
|
|
|
2017-09-10 23:29:24 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-02-10 10:06:09 +00:00
|
|
|
from django.shortcuts import render
|
2016-07-02 22:27:22 +00:00
|
|
|
|
2018-08-10 23:53:46 +00:00
|
|
|
from cotisations.models import Cotisation
|
2020-02-18 17:16:08 +00:00
|
|
|
from machines.models import Machine
|
2018-08-10 23:53:46 +00:00
|
|
|
from re2o.acl import can_view_all
|
2021-02-10 10:06:09 +00:00
|
|
|
from search.forms import (CHOICES_AFF, CHOICES_EMAILS, CHOICES_USER,
|
|
|
|
SearchForm, SearchFormPlus, initial_choices)
|
|
|
|
from users.models import User
|
2016-10-12 20:58:41 +00:00
|
|
|
|
2021-02-10 10:06:09 +00:00
|
|
|
from .engine import (apply_filters, create_queries, empty_filters,
|
|
|
|
finish_results, search_single_query)
|
2017-11-07 00:46:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_results(query, request, params):
|
|
|
|
"""The main function of the search procedure. It gather the filters for
|
|
|
|
each of the different words of the query and concatenate them into a
|
|
|
|
single filter. Then it calls 'finish_results' and return the queryset of
|
|
|
|
objects to display as results"""
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
start = params.get("s", None)
|
|
|
|
end = params.get("e", None)
|
|
|
|
user_state = params.get("u", initial_choices(CHOICES_USER))
|
2020-04-19 15:07:29 +00:00
|
|
|
email_state = params.get("m", initial_choices(CHOICES_EMAILS))
|
2019-11-04 16:55:03 +00:00
|
|
|
aff = params.get("a", initial_choices(CHOICES_AFF))
|
2017-11-07 00:46:45 +00:00
|
|
|
|
2020-02-18 11:02:08 +00:00
|
|
|
filters = empty_filters()
|
2017-11-07 00:46:45 +00:00
|
|
|
|
2020-02-18 11:02:08 +00:00
|
|
|
queries = create_queries(query)
|
2020-02-18 13:29:47 +00:00
|
|
|
for q in queries:
|
2020-02-18 11:02:08 +00:00
|
|
|
filters = search_single_query(
|
2020-04-19 15:07:29 +00:00
|
|
|
q, filters, request.user, start, end, user_state, email_state, aff
|
2017-11-02 00:25:24 +00:00
|
|
|
)
|
|
|
|
|
2019-12-06 10:29:10 +00:00
|
|
|
results = apply_filters(filters, request.user, aff)
|
2020-02-19 10:06:57 +00:00
|
|
|
results = finish_results(
|
2021-02-10 10:06:09 +00:00
|
|
|
request, results, request.GET.get("col"), request.GET.get("order")
|
2020-02-19 10:06:57 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
results.update({"search_term": query})
|
2016-11-01 14:29:12 +00:00
|
|
|
|
2017-11-01 23:19:41 +00:00
|
|
|
return results
|
2016-07-04 21:56:51 +00:00
|
|
|
|
2017-11-02 19:58:20 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2018-08-11 00:54:50 +00:00
|
|
|
@can_view_all(User, Machine, Cotisation)
|
2016-07-04 21:56:51 +00:00
|
|
|
def search(request):
|
2020-04-28 19:47:39 +00:00
|
|
|
"""View used to display the simple search page."""
|
2017-11-01 16:01:10 +00:00
|
|
|
search_form = SearchForm(request.GET or None)
|
|
|
|
if search_form.is_valid():
|
2017-11-01 23:19:41 +00:00
|
|
|
return render(
|
|
|
|
request,
|
2019-11-04 16:55:03 +00:00
|
|
|
"search/index.html",
|
2017-11-01 23:19:41 +00:00
|
|
|
get_results(
|
2021-02-10 10:06:09 +00:00
|
|
|
search_form.cleaned_data.get("q", ""), request, search_form.cleaned_data
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2017-11-01 23:19:41 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
return render(request, "search/search.html", {"search_form": search_form})
|
2017-11-02 19:58:20 +00:00
|
|
|
|
2016-07-04 21:56:51 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2018-08-11 00:54:50 +00:00
|
|
|
@can_view_all(User, Machine, Cotisation)
|
2016-07-04 21:56:51 +00:00
|
|
|
def searchp(request):
|
2020-04-28 19:47:39 +00:00
|
|
|
"""View used to display the advanced search page."""
|
2017-11-01 16:01:10 +00:00
|
|
|
search_form = SearchFormPlus(request.GET or None)
|
|
|
|
if search_form.is_valid():
|
2017-11-01 23:19:41 +00:00
|
|
|
return render(
|
|
|
|
request,
|
2019-11-04 16:55:03 +00:00
|
|
|
"search/index.html",
|
2017-11-01 23:19:41 +00:00
|
|
|
get_results(
|
2021-02-10 10:06:09 +00:00
|
|
|
search_form.cleaned_data.get("q", ""), request, search_form.cleaned_data
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2017-11-01 23:19:41 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
return render(request, "search/search.html", {"search_form": search_form})
|