8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-24 04:13:12 +00:00
re2o/multi_op/views.py

207 lines
6.8 KiB
Python
Raw Permalink Normal View History

2019-09-19 22:09:49 +00:00
# -*- mode: python; coding: utf-8 -*-
2020-11-23 16:06:37 +00:00
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
2019-09-19 22:09:49 +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 Goulven Kermarec
# Copyright © 2017 Augustin Lemesle
#
# 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
# Goulven Kermarec, Gabriel Détraz, Lemesle Augustin
# Gplv2
from django.contrib import messages
from django.contrib.auth.decorators import login_required
2021-02-10 10:06:09 +00:00
from django.db.models import Q
from django.forms import modelformset_factory
from django.shortcuts import redirect, render
2019-09-19 22:09:49 +00:00
from django.template.loader import render_to_string
from django.urls import reverse
2021-02-10 10:06:09 +00:00
from django.utils.translation import ugettext as _
from django.views.decorators.cache import cache_page
2019-09-19 22:09:49 +00:00
2021-02-10 10:06:09 +00:00
from preferences.models import AssoOption, GeneralOption
from re2o.acl import can_create, can_edit, can_view, can_view_all
from re2o.base import SortTable, re2o_paginator
from re2o.utils import all_adherent, all_has_access
from re2o.views import form
from topologie.models import Dormitory, Room
2019-09-19 22:09:49 +00:00
from .forms import DormitoryForm
2020-09-04 18:03:26 +00:00
from .preferences.models import MultiopOption
2019-09-19 22:09:49 +00:00
def display_rooms_connection(request, dormitory=None):
2020-04-28 10:19:05 +00:00
"""View used to display an overview of the rooms' connection state.
Args:
request: django request.
dormitory: Dormitory, the dormitory used to filter rooms. If no
dormitory is given, all rooms are displayed (default: None).
"""
2021-02-10 10:06:09 +00:00
room_list = (
Room.objects.select_related("building__dormitory")
.filter(
building__dormitory__in=MultiopOption.get_cached_value("enabled_dorm").all()
)
.order_by("building_dormitory", "port")
)
2019-09-19 22:09:49 +00:00
if dormitory:
room_list = room_list.filter(building__dormitory=dormitory)
room_list = SortTable.sort(
room_list,
request.GET.get("col"),
request.GET.get("order"),
SortTable.TOPOLOGIE_INDEX_ROOM,
2019-09-19 22:09:49 +00:00
)
pagination_number = GeneralOption.get_cached_value("pagination_number")
2019-09-19 22:09:49 +00:00
room_list = re2o_paginator(request, room_list, pagination_number)
asso_name = AssoOption.get_cached_value("pseudo")
return render(
request,
"multi_op/index_room_state.html",
{
"room_list": room_list,
"asso_name": asso_name,
},
)
2019-09-19 22:09:49 +00:00
@login_required
@can_view_all(Room)
def aff_state_global(request):
2020-04-28 10:19:05 +00:00
"""View used to display the connection state of all rooms."""
2019-09-19 22:09:49 +00:00
return display_rooms_connection(request)
@login_required
@can_view(Dormitory)
def aff_state_dormitory(request, dormitory, dormitoryid):
2020-04-28 10:19:05 +00:00
"""View used to display the connection state of the rooms in the given
dormitory.
Args:
request: django request.
dormitory: Dormitory, the dormitory used to filter rooms.
dormitoryid: int, the id of the dormitory.
"""
2019-09-19 22:09:49 +00:00
return display_rooms_connection(dormitory=dormitory)
@login_required
@can_view_all(Room)
def aff_pending_connection(request):
2020-04-28 10:19:05 +00:00
"""View used to display rooms pending connection to the organisation's
network.
"""
room_list = (
Room.objects.select_related("building__dormitory")
.filter(port__isnull=True)
.filter(adherent__in=all_has_access())
2021-02-10 10:06:09 +00:00
.filter(
building__dormitory__in=MultiopOption.get_cached_value("enabled_dorm").all()
)
.order_by("building_dormitory", "port")
)
2019-09-19 22:09:49 +00:00
dormitory_form = DormitoryForm(request.POST or None)
if dormitory_form.is_valid():
room_list = room_list.filter(
building__dormitory__in=dormitory_form.cleaned_data["dormitory"]
)
2019-09-19 22:09:49 +00:00
room_list = SortTable.sort(
room_list,
request.GET.get("col"),
request.GET.get("order"),
SortTable.TOPOLOGIE_INDEX_ROOM,
2019-09-19 22:09:49 +00:00
)
pagination_number = GeneralOption.get_cached_value("pagination_number")
2019-09-19 22:09:49 +00:00
room_list = re2o_paginator(request, room_list, pagination_number)
asso_name = AssoOption.get_cached_value("pseudo")
2019-09-19 22:09:49 +00:00
return render(
request,
"multi_op/index_room_state.html",
{
"room_list": room_list,
"dormitory_form": dormitory_form,
"asso_name": asso_name,
},
2019-09-19 22:09:49 +00:00
)
@login_required
@can_view_all(Room)
def aff_pending_disconnection(request):
2020-04-28 10:19:05 +00:00
"""View used to display rooms pending disconnection from the organisation's
network.
"""
room_list = (
Room.objects.select_related("building__dormitory")
.filter(port__isnull=False)
.exclude(Q(adherent__in=all_has_access()) | Q(adherent__in=all_adherent()))
2021-02-10 10:06:09 +00:00
.filter(
building__dormitory__in=MultiopOption.get_cached_value("enabled_dorm").all()
)
.order_by("building_dormitory", "port")
)
2019-09-19 22:09:49 +00:00
dormitory_form = DormitoryForm(request.POST or None)
if dormitory_form.is_valid():
room_list = room_list.filter(
building__dormitory__in=dormitory_form.cleaned_data["dormitory"]
)
2019-09-19 22:09:49 +00:00
room_list = SortTable.sort(
room_list,
request.GET.get("col"),
request.GET.get("order"),
SortTable.TOPOLOGIE_INDEX_ROOM,
2019-09-19 22:09:49 +00:00
)
pagination_number = GeneralOption.get_cached_value("pagination_number")
2019-09-19 22:09:49 +00:00
room_list = re2o_paginator(request, room_list, pagination_number)
asso_name = AssoOption.get_cached_value("pseudo")
2019-09-19 22:09:49 +00:00
return render(
request,
"multi_op/index_room_state.html",
{
"room_list": room_list,
"dormitory_form": dormitory_form,
"asso_name": asso_name,
},
2019-09-19 22:09:49 +00:00
)
2019-09-21 14:25:05 +00:00
@login_required
@can_edit(Room)
def disconnect_room(request, room, roomid):
2020-04-28 10:19:05 +00:00
"""View used to disconnect a room.
Args:
request: django request.
room: Room, the room to be disconnected.
roomid: int, the id of the room.
"""
2019-09-21 14:25:05 +00:00
room.port_set.clear()
room.save()
messages.success(request, _("The room %s was disconnected.") % room)
return redirect(reverse("multi_op:aff-pending-disconnection"))
2019-09-21 14:25:05 +00:00
2019-09-20 17:56:19 +00:00
def navbar_user():
2020-04-28 10:19:05 +00:00
"""View used to display a link to manage operators in the navbar (in the
dropdown menu Topology).
"""
return ("topologie", render_to_string("multi_op/navbar.html"))