2020-12-28 12:20:46 +00:00
|
|
|
# -*- 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
|
|
|
|
|
|
|
|
from django.db.models import Q, Value, CharField
|
|
|
|
from django.db.models.functions import Concat
|
|
|
|
|
2020-12-31 19:12:36 +00:00
|
|
|
from .models import Room, Dormitory, Building, Switch, PortProfile, Port, SwitchBay
|
2020-12-28 12:20:46 +00:00
|
|
|
|
2021-01-02 22:35:56 +00:00
|
|
|
from re2o.views import AutocompleteViewMixin, AutocompleteUnloggedViewMixin
|
2020-12-28 12:20:46 +00:00
|
|
|
|
|
|
|
|
2021-01-02 22:35:56 +00:00
|
|
|
class RoomAutocomplete(AutocompleteUnloggedViewMixin):
|
2020-12-28 12:20:46 +00:00
|
|
|
obj_type = Room
|
|
|
|
|
2020-12-30 18:22:43 +00:00
|
|
|
# Precision on search to add annotations so search behaves more like users expect it to
|
|
|
|
def filter_results(self):
|
2021-01-01 23:27:31 +00:00
|
|
|
# Suppose we have a dorm named Dorm, a building named B, and rooms from 001 - 999
|
2020-12-28 12:20:46 +00:00
|
|
|
# Comments explain what we try to match
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.annotate(
|
2020-12-31 19:12:36 +00:00
|
|
|
full_name=Concat(
|
|
|
|
"building__name", Value(" "), "name"
|
|
|
|
), # Match when the user searches "B 001"
|
2020-12-28 12:20:46 +00:00
|
|
|
full_name_stuck=Concat("building__name", "name"), # Match "B001"
|
2020-12-31 19:12:36 +00:00
|
|
|
dorm_name=Concat(
|
|
|
|
"building__dormitory__name", Value(" "), "name"
|
|
|
|
), # Match "Dorm 001"
|
|
|
|
dorm_full_name=Concat(
|
|
|
|
"building__dormitory__name",
|
|
|
|
Value(" "),
|
|
|
|
"building__name",
|
|
|
|
Value(" "),
|
|
|
|
"name",
|
|
|
|
), # Match "Dorm B 001"
|
|
|
|
dorm_full_colon_name=Concat(
|
|
|
|
"building__dormitory__name",
|
|
|
|
Value(" : "),
|
|
|
|
"building__name",
|
|
|
|
Value(" "),
|
|
|
|
"name",
|
|
|
|
), # Match "Dorm : B 001" (see Room's full_name property)
|
2020-12-28 12:20:46 +00:00
|
|
|
).all()
|
|
|
|
|
|
|
|
if self.q:
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.filter(
|
2020-12-28 12:20:46 +00:00
|
|
|
Q(full_name__icontains=self.q)
|
|
|
|
| Q(full_name_stuck__icontains=self.q)
|
|
|
|
| Q(dorm_name__icontains=self.q)
|
|
|
|
| Q(dorm_full_name__icontains=self.q)
|
|
|
|
| Q(dorm_full_colon_name__icontains=self.q)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DormitoryAutocomplete(AutocompleteViewMixin):
|
|
|
|
obj_type = Dormitory
|
|
|
|
|
|
|
|
|
|
|
|
class BuildingAutocomplete(AutocompleteViewMixin):
|
|
|
|
obj_type = Building
|
|
|
|
|
2020-12-30 18:22:43 +00:00
|
|
|
def filter_results(self):
|
2020-12-28 12:20:46 +00:00
|
|
|
# We want to be able to filter by dorm so it's easier
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.annotate(
|
2020-12-28 12:20:46 +00:00
|
|
|
full_name=Concat("dormitory__name", Value(" "), "name"),
|
|
|
|
full_name_colon=Concat("dormitory__name", Value(" : "), "name"),
|
|
|
|
).all()
|
|
|
|
|
|
|
|
if self.q:
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.filter(
|
2020-12-31 19:12:36 +00:00
|
|
|
Q(full_name__icontains=self.q) | Q(full_name_colon__icontains=self.q)
|
2020-12-28 12:20:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SwitchAutocomplete(AutocompleteViewMixin):
|
|
|
|
obj_type = Switch
|
|
|
|
|
|
|
|
|
|
|
|
class PortAutocomplete(AutocompleteViewMixin):
|
|
|
|
obj_type = Port
|
|
|
|
|
2020-12-30 18:22:43 +00:00
|
|
|
def filter_results(self):
|
2020-12-28 12:20:46 +00:00
|
|
|
# We want to enter the switch name, not just the port number
|
2021-01-01 23:27:31 +00:00
|
|
|
# Because we're concatenating a CharField and an Integer, we have to specify the output_field
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.annotate(
|
2020-12-31 19:12:36 +00:00
|
|
|
full_name=Concat(
|
|
|
|
"switch__name", Value(" "), "port", output_field=CharField()
|
|
|
|
),
|
2020-12-28 12:20:46 +00:00
|
|
|
full_name_stuck=Concat("switch__name", "port", output_field=CharField()),
|
2020-12-31 19:12:36 +00:00
|
|
|
full_name_dash=Concat(
|
|
|
|
"switch__name", Value(" - "), "port", output_field=CharField()
|
|
|
|
),
|
2020-12-28 12:20:46 +00:00
|
|
|
).all()
|
|
|
|
|
|
|
|
if self.q:
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.filter(
|
2020-12-28 12:20:46 +00:00
|
|
|
Q(full_name__icontains=self.q)
|
|
|
|
| Q(full_name_stuck__icontains=self.q)
|
|
|
|
| Q(full_name_dash__icontains=self.q)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-28 17:26:39 +00:00
|
|
|
class SwitchBayAutocomplete(AutocompleteViewMixin):
|
|
|
|
obj_type = SwitchBay
|
|
|
|
|
2020-12-30 18:22:43 +00:00
|
|
|
def filter_results(self):
|
2021-01-01 23:27:31 +00:00
|
|
|
# See RoomAutocomplete.filter_results
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.annotate(
|
2020-12-31 19:12:36 +00:00
|
|
|
full_name=Concat(
|
|
|
|
"building__name", Value(" "), "name"
|
2021-01-01 23:27:31 +00:00
|
|
|
),
|
2020-12-31 19:12:36 +00:00
|
|
|
dorm_name=Concat(
|
|
|
|
"building__dormitory__name", Value(" "), "name"
|
2021-01-01 23:27:31 +00:00
|
|
|
),
|
2020-12-31 19:12:36 +00:00
|
|
|
dorm_full_name=Concat(
|
|
|
|
"building__dormitory__name",
|
|
|
|
Value(" "),
|
|
|
|
"building__name",
|
|
|
|
Value(" "),
|
|
|
|
"name",
|
2021-01-01 23:27:31 +00:00
|
|
|
),
|
|
|
|
dorm_full_colon_name=Concat(
|
|
|
|
"building__dormitory__name",
|
|
|
|
Value(" : "),
|
|
|
|
"building__name",
|
|
|
|
Value(" "),
|
|
|
|
"name",
|
|
|
|
),
|
2020-12-28 17:26:39 +00:00
|
|
|
).all()
|
|
|
|
|
|
|
|
if self.q:
|
2020-12-30 18:22:43 +00:00
|
|
|
self.query_set = self.query_set.filter(
|
2020-12-28 17:26:39 +00:00
|
|
|
Q(full_name__icontains=self.q)
|
|
|
|
| Q(dorm_name__icontains=self.q)
|
|
|
|
| Q(dorm_full_name__icontains=self.q)
|
2021-01-01 23:27:31 +00:00
|
|
|
| Q(dorm_full_colon_name__icontains=self.q)
|
2020-12-28 17:26:39 +00:00
|
|
|
)
|
|
|
|
|
2020-12-28 12:20:46 +00:00
|
|
|
|
|
|
|
class PortProfileAutocomplete(AutocompleteViewMixin):
|
|
|
|
obj_type = PortProfile
|