8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-30 08:32:26 +00:00
re2o/search/views.py

117 lines
3.8 KiB
Python
Raw Normal View History

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
# Copyright © 2017 Lara Kermarec
2017-01-15 23:01:18 +00:00
# Copyright © 2017 Augustin Lemesle
# 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
Augustin lemesle, Gabriel Détraz, Lara Kermarec, Maël Kervella
2017-11-02 19:58:20 +00:00
Gplv2"""
from __future__ import unicode_literals
2016-07-02 22:27:22 +00:00
from django.shortcuts import render
2016-07-08 10:35:53 +00:00
from django.contrib.auth.decorators import login_required
2016-07-02 22:27:22 +00:00
2020-02-18 17:16:08 +00:00
from users.models import User
from cotisations.models import Cotisation
2020-02-18 17:16:08 +00:00
from machines.models import Machine
from search.forms import (
SearchForm,
SearchFormPlus,
CHOICES_USER,
2020-04-19 15:07:29 +00:00
CHOICES_EMAILS,
CHOICES_AFF,
initial_choices,
)
from re2o.acl import can_view_all
2020-02-19 10:06:57 +00:00
from .engine import empty_filters, create_queries, search_single_query
from .engine import apply_filters, finish_results
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"""
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))
aff = params.get("a", initial_choices(CHOICES_AFF))
2020-02-18 11:02:08 +00:00
filters = empty_filters()
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
)
results = apply_filters(filters, request.user, aff)
2020-02-19 10:06:57 +00:00
results = finish_results(
request,
results,
request.GET.get("col"),
request.GET.get("order")
)
results.update({"search_term": query})
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
@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."""
search_form = SearchForm(request.GET or None)
if search_form.is_valid():
return render(
request,
"search/index.html",
get_results(
2020-02-19 10:06:57 +00:00
search_form.cleaned_data.get("q", ""),
request,
search_form.cleaned_data
),
)
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
@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."""
search_form = SearchFormPlus(request.GET or None)
if search_form.is_valid():
return render(
request,
"search/index.html",
get_results(
2020-02-19 10:06:57 +00:00
search_form.cleaned_data.get("q", ""),
request,
search_form.cleaned_data
),
)
return render(request, "search/search.html", {"search_form": search_form})