8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-04 17:06:27 +00:00
re2o/multi_op/views.py
2019-10-01 22:47:30 +02:00

175 lines
6.1 KiB
Python

# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# 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
from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from django.views.decorators.cache import cache_page
from django.utils.translation import ugettext as _
from django.urls import reverse
from django.forms import modelformset_factory
from django.db.models import Q
from re2o.views import form
from re2o.utils import all_has_access, all_adherent
from re2o.base import (
re2o_paginator,
SortTable,
)
from re2o.acl import(
can_view,
can_view_all,
can_edit,
can_create,
)
from preferences.models import GeneralOption
from .forms import DormitoryForm
from .preferences.models import(
Preferences,
)
from topologie.models import Room, Dormitory
from .preferences.forms import (
EditPreferencesForm,
)
def display_rooms_connection(request, dormitory=None):
"""View to display global state of connection state"""
room_list = Room.objects.select_related('building__dormitory').order_by('building_dormitory', 'port')
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
)
pagination_number = GeneralOption.get_cached_value('pagination_number')
room_list = re2o_paginator(request, room_list, pagination_number)
return render(
request,
'multi_op/index_room_state.html',
{'room_list': room_list}
)
@login_required
@can_view_all(Room)
def aff_state_global(request):
return display_rooms_connection(request)
@login_required
@can_view(Dormitory)
def aff_state_dormitory(request, dormitory, dormitoryid):
return display_rooms_connection(dormitory=dormitory)
@login_required
@can_view_all(Room)
def aff_pending_connection(request):
"""Aff pending Rooms to connect on our network"""
room_list = Room.objects.select_related('building__dormitory').filter(port__isnull=True).filter(adherent__in=all_has_access()).order_by('building_dormitory', 'port')
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'])
room_list = SortTable.sort(
room_list,
request.GET.get('col'),
request.GET.get('order'),
SortTable.TOPOLOGIE_INDEX_ROOM
)
pagination_number = GeneralOption.get_cached_value('pagination_number')
room_list = re2o_paginator(request, room_list, pagination_number)
return render(
request,
'multi_op/index_room_state.html',
{'room_list': room_list, 'dormitory_form': dormitory_form}
)
@login_required
@can_view_all(Room)
def aff_pending_disconnection(request):
"""Aff pending Rooms to disconnect from our 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())).order_by('building_dormitory', 'port')
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'])
room_list = SortTable.sort(
room_list,
request.GET.get('col'),
request.GET.get('order'),
SortTable.TOPOLOGIE_INDEX_ROOM
)
pagination_number = GeneralOption.get_cached_value('pagination_number')
room_list = re2o_paginator(request, room_list, pagination_number)
return render(
request,
'multi_op/index_room_state.html',
{'room_list': room_list, 'dormitory_form': dormitory_form}
)
def edit_preferences(request):
""" View to edit the settings of the tickets """
preferences_instance, created = Preferences.objects.get_or_create(id=1)
preferencesform = EditPreferencesForm(
request.POST or None,
instance = preferences_instance,)
if preferencesform.is_valid():
if preferencesform.changed_data:
preferencesform.save()
messages.success(request,'Preferences updated')
return redirect(reverse('preferences:display-options',))
else:
messages.error(request,'Formulaire Invalide')
return form({'preferencesform':preferencesform,},'multi_op/form_preferences.html',request)
return form({'preferencesform':preferencesform,},'multi_op/form_preferences.html',request)
def navbar_user():
"""View to display the app in user's dropdown in the navbar"""
return ('topologie', render_to_string('multi_op/navbar.html'))
def preferences(request):
""" View to display the settings of the tickets in the preferences page"""
pref, created = Preferences.objects.get_or_create(id=1)
context = {'preferences':pref,'language':str(pref.LANGUES[pref.mail_language][1])}
return render_to_string('tickets/preferences.html', context=context, request=request, using=None)