8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-04 18:42:25 +00:00
re2o/users/views_autocomplete.py

99 lines
3.3 KiB
Python
Raw Permalink Normal View History

# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017-2020 Gabriel Détraz
# Copyright © 2017-2020 Jean-Romain Garnier
#
# 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.
# App de gestion des users pour re2o
# Lara Kermarec, Gabriel Détraz, Lemesle Augustin
# Gplv2
"""
Django views autocomplete view
Here are defined the autocomplete class based view.
"""
from __future__ import unicode_literals
2021-02-10 10:06:09 +00:00
from django.db.models import CharField, Q, Value
from django.db.models.functions import Concat
2021-02-10 10:06:09 +00:00
from re2o.views import AutocompleteLoggedOutViewMixin, AutocompleteViewMixin
2021-02-10 10:06:09 +00:00
from .models import Adherent, Club, ListShell, School, User
2020-12-28 15:18:11 +00:00
2020-12-31 19:12:36 +00:00
2021-01-02 22:42:13 +00:00
class SchoolAutocomplete(AutocompleteLoggedOutViewMixin):
obj_type = School
2020-12-31 19:12:36 +00:00
class UserAutocomplete(AutocompleteViewMixin):
obj_type = User
# Precision on search to add annotations so search behaves more like users expect it to
def filter_results(self):
2020-12-28 15:18:11 +00:00
# Comments explain what we try to match
self.query_set = self.query_set.annotate(
2020-12-31 19:12:36 +00:00
full_name=Concat(
"adherent__name", Value(" "), "surname"
), # Match when the user searches "Toto Passoir"
full_name_reverse=Concat(
"surname", Value(" "), "adherent__name"
), # Match when the user searches "Passoir Toto"
2020-12-28 15:18:11 +00:00
).all()
if self.q:
self.query_set = self.query_set.filter(
2020-12-28 15:18:11 +00:00
Q(pseudo__icontains=self.q)
| Q(full_name__icontains=self.q)
| Q(full_name_reverse__icontains=self.q)
)
2020-12-31 19:12:36 +00:00
class AdherentAutocomplete(AutocompleteViewMixin):
obj_type = Adherent
2020-12-31 14:31:49 +00:00
# Precision on search to add annotations so search behaves more like users expect it to
def filter_results(self):
# Comments explain what we try to match
self.query_set = self.query_set.annotate(
2020-12-31 19:12:36 +00:00
full_name=Concat(
"name", Value(" "), "surname"
), # Match when the user searches "Toto Passoir"
full_name_reverse=Concat(
"surname", Value(" "), "name"
), # Match when the user searches "Passoir Toto"
2020-12-31 14:31:49 +00:00
).all()
if self.q:
self.query_set = self.query_set.filter(
Q(pseudo__icontains=self.q)
| Q(full_name__icontains=self.q)
| Q(full_name_reverse__icontains=self.q)
)
2020-12-31 19:12:36 +00:00
class ClubAutocomplete(AutocompleteViewMixin):
obj_type = Club
2020-12-31 19:12:36 +00:00
class ShellAutocomplete(AutocompleteViewMixin):
obj_type = ListShell
query_filter = "shell__icontains"