|
|
|
@ -20,18 +20,17 @@ |
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc., |
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
|
|
""" |
|
|
|
Page des vues de l'application topologie |
|
|
|
|
|
|
|
Permet de créer, modifier et supprimer : |
|
|
|
- un port (add_port, edit_port, del_port) |
|
|
|
- un switch : les vues d'ajout et d'édition font appel aux forms de creation |
|
|
|
de switch, mais aussi aux forms de machines.forms (domain, interface et |
|
|
|
machine). Le views les envoie et les save en même temps. TODO : rationaliser |
|
|
|
et faire que la creation de machines (interfaces, domain etc) soit gérée |
|
|
|
coté models et forms de topologie |
|
|
|
- une chambre (new_room, edit_room, del_room) |
|
|
|
- une stack |
|
|
|
- l'historique de tous les objets cités |
|
|
|
Views for the 'topologie' app of re2o. |
|
|
|
|
|
|
|
They are used to create, edit and delete: |
|
|
|
* a port (add_port, edit_port, del_port) |
|
|
|
* a switch: the views call forms for switches but also machines (domain, |
|
|
|
interface and machine), send and save them at the same time. |
|
|
|
TODO rationalise, enforce the creation of machines (interfaces, domains |
|
|
|
etc.) in models and forms from 'topologie' |
|
|
|
* a room (new_room, edit_room, del_room) |
|
|
|
* a stack |
|
|
|
* histories of all objects mentioned. |
|
|
|
""" |
|
|
|
from __future__ import unicode_literals |
|
|
|
|
|
|
|
@ -105,7 +104,7 @@ from os.path import isfile |
|
|
|
@login_required |
|
|
|
@can_view_all(Switch) |
|
|
|
def index(request): |
|
|
|
""" Vue d'affichage de tous les swicthes""" |
|
|
|
"""View used to display all switches.""" |
|
|
|
switch_list = ( |
|
|
|
Switch.objects.prefetch_related( |
|
|
|
Prefetch( |
|
|
|
@ -171,7 +170,7 @@ def index_port_profile(request): |
|
|
|
@can_view_all(Port) |
|
|
|
@can_view(Switch) |
|
|
|
def index_port(request, switch, switchid): |
|
|
|
""" Affichage de l'ensemble des ports reliés à un switch particulier""" |
|
|
|
"""View used to display all ports related to the given switch.""" |
|
|
|
port_list = ( |
|
|
|
Port.objects.filter(switch=switch) |
|
|
|
.select_related("room__building__dormitory") |
|
|
|
@ -204,7 +203,7 @@ def index_port(request, switch, switchid): |
|
|
|
@login_required |
|
|
|
@can_view_all(Room) |
|
|
|
def index_room(request): |
|
|
|
""" Affichage de l'ensemble des chambres""" |
|
|
|
"""View used to display all rooms.""" |
|
|
|
room_list = Room.objects.select_related("building__dormitory") |
|
|
|
room_list = SortTable.sort( |
|
|
|
room_list, |
|
|
|
@ -220,7 +219,7 @@ def index_room(request): |
|
|
|
@login_required |
|
|
|
@can_view_all(AccessPoint) |
|
|
|
def index_ap(request): |
|
|
|
""" Affichage de l'ensemble des bornes""" |
|
|
|
"""View used to display all APs.""" |
|
|
|
ap_list = AccessPoint.objects.prefetch_related( |
|
|
|
Prefetch( |
|
|
|
"interface_set", |
|
|
|
@ -245,7 +244,7 @@ def index_ap(request): |
|
|
|
@login_required |
|
|
|
@can_view_all(Stack, Building, Dormitory, SwitchBay) |
|
|
|
def index_physical_grouping(request): |
|
|
|
"""Affichage de la liste des stacks (affiche l'ensemble des switches)""" |
|
|
|
"""View used to display the list of stacks (display all switches).""" |
|
|
|
stack_list = Stack.objects.prefetch_related( |
|
|
|
"switch_set__interface_set__domain__extension" |
|
|
|
) |
|
|
|
@ -293,7 +292,7 @@ def index_physical_grouping(request): |
|
|
|
@login_required |
|
|
|
@can_view_all(ModelSwitch, ConstructorSwitch) |
|
|
|
def index_model_switch(request): |
|
|
|
""" Affichage de l'ensemble des modèles de switches""" |
|
|
|
"""View used to display all switch models.""" |
|
|
|
model_switch_list = ModelSwitch.objects.select_related( |
|
|
|
"constructor" |
|
|
|
).prefetch_related("switch_set__interface_set__domain") |
|
|
|
@ -323,7 +322,7 @@ def index_model_switch(request): |
|
|
|
@login_required |
|
|
|
@can_view_all(ModuleSwitch) |
|
|
|
def index_module(request): |
|
|
|
"""Display all modules of switchs""" |
|
|
|
"""View used to display all switch modules.""" |
|
|
|
module_list = ModuleSwitch.objects.all() |
|
|
|
modular_switchs = ( |
|
|
|
Switch.objects.filter(model__is_modular=True) |
|
|
|
@ -342,7 +341,7 @@ def index_module(request): |
|
|
|
@login_required |
|
|
|
@can_edit(Vlan) |
|
|
|
def edit_vlanoptions(request, vlan_instance, **_kwargs): |
|
|
|
""" View used to edit options for switch of VLAN object """ |
|
|
|
"""View used to edit options for switch of VLAN object.""" |
|
|
|
vlan = EditOptionVlanForm(request.POST or None, instance=vlan_instance) |
|
|
|
if vlan.is_valid(): |
|
|
|
if vlan.changed_data: |
|
|
|
@ -357,7 +356,7 @@ def edit_vlanoptions(request, vlan_instance, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(Port) |
|
|
|
def new_port(request, switchid): |
|
|
|
""" Nouveau port""" |
|
|
|
"""View used to create ports.""" |
|
|
|
try: |
|
|
|
switch = Switch.objects.get(pk=switchid) |
|
|
|
except Switch.DoesNotExist: |
|
|
|
@ -383,9 +382,10 @@ def new_port(request, switchid): |
|
|
|
@login_required |
|
|
|
@can_edit(Port) |
|
|
|
def edit_port(request, port_object, **_kwargs): |
|
|
|
""" Edition d'un port. Permet de changer le switch parent et |
|
|
|
l'affectation du port""" |
|
|
|
"""View used to edit ports. |
|
|
|
|
|
|
|
It enables to change the related switch and the port assignment. |
|
|
|
""" |
|
|
|
port = EditPortForm(request.POST or None, instance=port_object) |
|
|
|
if port.is_valid(): |
|
|
|
if port.changed_data: |
|
|
|
@ -410,7 +410,7 @@ def edit_port(request, port_object, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(Port) |
|
|
|
def del_port(request, port, **_kwargs): |
|
|
|
""" Supprime le port""" |
|
|
|
"""View used to delete ports.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
port.delete() |
|
|
|
@ -435,7 +435,7 @@ def del_port(request, port, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(Stack) |
|
|
|
def new_stack(request): |
|
|
|
"""Ajoute un nouveau stack : stackid_min, max, et nombre de switches""" |
|
|
|
"""View used to create stacks.""" |
|
|
|
stack = StackForm(request.POST or None) |
|
|
|
if stack.is_valid(): |
|
|
|
stack.save() |
|
|
|
@ -449,7 +449,7 @@ def new_stack(request): |
|
|
|
@login_required |
|
|
|
@can_edit(Stack) |
|
|
|
def edit_stack(request, stack, **_kwargs): |
|
|
|
"""Edition d'un stack (nombre de switches, nom...)""" |
|
|
|
"""View used to edit stacks.""" |
|
|
|
stack = StackForm(request.POST or None, instance=stack) |
|
|
|
if stack.is_valid(): |
|
|
|
if stack.changed_data: |
|
|
|
@ -464,7 +464,7 @@ def edit_stack(request, stack, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(Stack) |
|
|
|
def del_stack(request, stack, **_kwargs): |
|
|
|
"""Supprime un stack""" |
|
|
|
"""View used to delete stacks.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
stack.delete() |
|
|
|
@ -487,8 +487,7 @@ def del_stack(request, stack, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_edit(Stack) |
|
|
|
def edit_switchs_stack(request, stack, **_kwargs): |
|
|
|
"""Permet d'éditer la liste des switches dans une stack et l'ajouter""" |
|
|
|
|
|
|
|
"""View used to edit the list of switches of the given stack.""" |
|
|
|
if request.method == "POST": |
|
|
|
pass |
|
|
|
else: |
|
|
|
@ -500,9 +499,12 @@ def edit_switchs_stack(request, stack, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(Switch) |
|
|
|
def new_switch(request): |
|
|
|
""" Creation d'un switch. Cree en meme temps l'interface et la machine |
|
|
|
associée. Vue complexe. Appelle successivement les 4 models forms |
|
|
|
adaptés : machine, interface, domain et switch""" |
|
|
|
"""View used to create switches. |
|
|
|
|
|
|
|
At the same time, it creates the related interface and machine. The view |
|
|
|
successively calls the 4 appropriate forms: machine, interface, domain and |
|
|
|
switch. |
|
|
|
""" |
|
|
|
switch = NewSwitchForm(request.POST or None, user=request.user) |
|
|
|
interface = AddInterfaceForm(request.POST or None, user=request.user) |
|
|
|
domain = DomainForm(request.POST or None, user=request.user) |
|
|
|
@ -549,7 +551,7 @@ def new_switch(request): |
|
|
|
@login_required |
|
|
|
@can_create(Port) |
|
|
|
def create_ports(request, switchid): |
|
|
|
""" Création d'une liste de ports pour un switch.""" |
|
|
|
"""View used to create port lists for the given switch.""" |
|
|
|
try: |
|
|
|
switch = Switch.objects.get(pk=switchid) |
|
|
|
except Switch.DoesNotExist: |
|
|
|
@ -578,9 +580,11 @@ def create_ports(request, switchid): |
|
|
|
@login_required |
|
|
|
@can_edit(Switch) |
|
|
|
def edit_switch(request, switch, switchid): |
|
|
|
""" Edition d'un switch. Permet de chambre nombre de ports, |
|
|
|
place dans le stack, interface et machine associée""" |
|
|
|
"""View used to edit switches. |
|
|
|
|
|
|
|
It enables to change the number of ports, location in the stack, or the |
|
|
|
related interface and machine. |
|
|
|
""" |
|
|
|
switch_form = EditSwitchForm( |
|
|
|
request.POST or None, instance=switch, user=request.user |
|
|
|
) |
|
|
|
@ -622,9 +626,11 @@ def edit_switch(request, switch, switchid): |
|
|
|
@login_required |
|
|
|
@can_create(AccessPoint) |
|
|
|
def new_ap(request): |
|
|
|
""" Creation d'une ap. Cree en meme temps l'interface et la machine |
|
|
|
associée. Vue complexe. Appelle successivement les 3 models forms |
|
|
|
adaptés : machine, interface, domain et switch""" |
|
|
|
"""View used to create APs. |
|
|
|
|
|
|
|
At the same time, it creates the related interface and machine. The view |
|
|
|
successively calls the 3 appropriate forms: machine, interface, domain. |
|
|
|
""" |
|
|
|
ap = AddAccessPointForm(request.POST or None, user=request.user) |
|
|
|
interface = AddInterfaceForm(request.POST or None, user=request.user) |
|
|
|
domain = DomainForm(request.POST or None, user=request.user) |
|
|
|
@ -671,8 +677,7 @@ def new_ap(request): |
|
|
|
@login_required |
|
|
|
@can_edit(AccessPoint) |
|
|
|
def edit_ap(request, ap, **_kwargs): |
|
|
|
""" Edition d'un switch. Permet de chambre nombre de ports, |
|
|
|
place dans le stack, interface et machine associée""" |
|
|
|
"""View used to edit APs.""" |
|
|
|
interface_form = EditInterfaceForm( |
|
|
|
request.POST or None, user=request.user, instance=ap.interface_set.first() |
|
|
|
) |
|
|
|
@ -723,7 +728,7 @@ def edit_ap(request, ap, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(Room) |
|
|
|
def new_room(request): |
|
|
|
"""Nouvelle chambre """ |
|
|
|
"""View used to create rooms.""" |
|
|
|
room = EditRoomForm(request.POST or None) |
|
|
|
if room.is_valid(): |
|
|
|
room.save() |
|
|
|
@ -737,7 +742,7 @@ def new_room(request): |
|
|
|
@login_required |
|
|
|
@can_edit(Room) |
|
|
|
def edit_room(request, room, **_kwargs): |
|
|
|
""" Edition numero et details de la chambre""" |
|
|
|
"""View used to edit rooms.""" |
|
|
|
room = EditRoomForm(request.POST or None, instance=room) |
|
|
|
if room.is_valid(): |
|
|
|
if room.changed_data: |
|
|
|
@ -752,7 +757,7 @@ def edit_room(request, room, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(Room) |
|
|
|
def del_room(request, room, **_kwargs): |
|
|
|
""" Suppression d'un chambre""" |
|
|
|
"""View used to delete rooms.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
room.delete() |
|
|
|
@ -777,7 +782,7 @@ def del_room(request, room, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(ModelSwitch) |
|
|
|
def new_model_switch(request): |
|
|
|
"""Nouveau modèle de switch""" |
|
|
|
"""View used to create switch models.""" |
|
|
|
model_switch = EditModelSwitchForm(request.POST or None) |
|
|
|
if model_switch.is_valid(): |
|
|
|
model_switch.save() |
|
|
|
@ -793,8 +798,7 @@ def new_model_switch(request): |
|
|
|
@login_required |
|
|
|
@can_edit(ModelSwitch) |
|
|
|
def edit_model_switch(request, model_switch, **_kwargs): |
|
|
|
""" Edition d'un modèle de switch""" |
|
|
|
|
|
|
|
"""View used to edit switch models.""" |
|
|
|
model_switch = EditModelSwitchForm(request.POST or None, instance=model_switch) |
|
|
|
if model_switch.is_valid(): |
|
|
|
if model_switch.changed_data: |
|
|
|
@ -811,7 +815,7 @@ def edit_model_switch(request, model_switch, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(ModelSwitch) |
|
|
|
def del_model_switch(request, model_switch, **_kwargs): |
|
|
|
""" Suppression d'un modèle de switch""" |
|
|
|
"""View used to delete switch models.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
model_switch.delete() |
|
|
|
@ -838,7 +842,7 @@ def del_model_switch(request, model_switch, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(SwitchBay) |
|
|
|
def new_switch_bay(request): |
|
|
|
"""Nouvelle baie de switch""" |
|
|
|
"""View used to create switch bays.""" |
|
|
|
switch_bay = EditSwitchBayForm(request.POST or None) |
|
|
|
if switch_bay.is_valid(): |
|
|
|
switch_bay.save() |
|
|
|
@ -854,7 +858,7 @@ def new_switch_bay(request): |
|
|
|
@login_required |
|
|
|
@can_edit(SwitchBay) |
|
|
|
def edit_switch_bay(request, switch_bay, **_kwargs): |
|
|
|
""" Edition d'une baie de switch""" |
|
|
|
"""View used to edit switch bays.""" |
|
|
|
switch_bay = EditSwitchBayForm(request.POST or None, instance=switch_bay) |
|
|
|
if switch_bay.is_valid(): |
|
|
|
if switch_bay.changed_data: |
|
|
|
@ -871,7 +875,7 @@ def edit_switch_bay(request, switch_bay, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(SwitchBay) |
|
|
|
def del_switch_bay(request, switch_bay, **_kwargs): |
|
|
|
""" Suppression d'une baie de switch""" |
|
|
|
"""View used to delete switch bays.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
switch_bay.delete() |
|
|
|
@ -898,8 +902,7 @@ def del_switch_bay(request, switch_bay, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(Building) |
|
|
|
def new_building(request): |
|
|
|
"""New Building of a dorm |
|
|
|
Nouveau batiment""" |
|
|
|
"""View used to create buildings.""" |
|
|
|
building = EditBuildingForm(request.POST or None) |
|
|
|
if building.is_valid(): |
|
|
|
building.save() |
|
|
|
@ -915,8 +918,7 @@ def new_building(request): |
|
|
|
@login_required |
|
|
|
@can_edit(Building) |
|
|
|
def edit_building(request, building, **_kwargs): |
|
|
|
"""Edit a building |
|
|
|
Edition d'un batiment""" |
|
|
|
"""View used to edit buildings.""" |
|
|
|
building = EditBuildingForm(request.POST or None, instance=building) |
|
|
|
if building.is_valid(): |
|
|
|
if building.changed_data: |
|
|
|
@ -931,8 +933,7 @@ def edit_building(request, building, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(Building) |
|
|
|
def del_building(request, building, **_kwargs): |
|
|
|
"""Delete a building |
|
|
|
Suppression d'un batiment""" |
|
|
|
"""View used to delete buildings.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
building.delete() |
|
|
|
@ -959,8 +960,7 @@ def del_building(request, building, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(Dormitory) |
|
|
|
def new_dormitory(request): |
|
|
|
"""A new dormitory |
|
|
|
Nouvelle residence""" |
|
|
|
"""View used to create dormitories.""" |
|
|
|
dormitory = EditDormitoryForm(request.POST or None) |
|
|
|
if dormitory.is_valid(): |
|
|
|
dormitory.save() |
|
|
|
@ -976,8 +976,7 @@ def new_dormitory(request): |
|
|
|
@login_required |
|
|
|
@can_edit(Dormitory) |
|
|
|
def edit_dormitory(request, dormitory, **_kwargs): |
|
|
|
"""Edit a dormitory |
|
|
|
Edition d'une residence""" |
|
|
|
"""View used to edit dormitories.""" |
|
|
|
dormitory = EditDormitoryForm(request.POST or None, instance=dormitory) |
|
|
|
if dormitory.is_valid(): |
|
|
|
if dormitory.changed_data: |
|
|
|
@ -994,8 +993,7 @@ def edit_dormitory(request, dormitory, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(Dormitory) |
|
|
|
def del_dormitory(request, dormitory, **_kwargs): |
|
|
|
"""Delete a dormitory |
|
|
|
Suppression d'une residence""" |
|
|
|
"""View used to delete dormitories.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
dormitory.delete() |
|
|
|
@ -1022,7 +1020,7 @@ def del_dormitory(request, dormitory, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(ConstructorSwitch) |
|
|
|
def new_constructor_switch(request): |
|
|
|
"""Nouveau constructeur de switch""" |
|
|
|
"""View used to create switch constructors.""" |
|
|
|
constructor_switch = EditConstructorSwitchForm(request.POST or None) |
|
|
|
if constructor_switch.is_valid(): |
|
|
|
constructor_switch.save() |
|
|
|
@ -1038,8 +1036,7 @@ def new_constructor_switch(request): |
|
|
|
@login_required |
|
|
|
@can_edit(ConstructorSwitch) |
|
|
|
def edit_constructor_switch(request, constructor_switch, **_kwargs): |
|
|
|
""" Edition d'un constructeur de switch""" |
|
|
|
|
|
|
|
"""View used to edit switch constructors.""" |
|
|
|
constructor_switch = EditConstructorSwitchForm( |
|
|
|
request.POST or None, instance=constructor_switch |
|
|
|
) |
|
|
|
@ -1058,7 +1055,7 @@ def edit_constructor_switch(request, constructor_switch, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(ConstructorSwitch) |
|
|
|
def del_constructor_switch(request, constructor_switch, **_kwargs): |
|
|
|
""" Suppression d'un constructeur de switch""" |
|
|
|
"""View used to delete switch constructors.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
constructor_switch.delete() |
|
|
|
@ -1085,7 +1082,7 @@ def del_constructor_switch(request, constructor_switch, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(PortProfile) |
|
|
|
def new_port_profile(request): |
|
|
|
"""Create a new port profile""" |
|
|
|
"""View used to create port profiles.""" |
|
|
|
port_profile = EditPortProfileForm(request.POST or None) |
|
|
|
if port_profile.is_valid(): |
|
|
|
port_profile.save() |
|
|
|
@ -1101,7 +1098,7 @@ def new_port_profile(request): |
|
|
|
@login_required |
|
|
|
@can_edit(PortProfile) |
|
|
|
def edit_port_profile(request, port_profile, **_kwargs): |
|
|
|
"""Edit a port profile""" |
|
|
|
"""View used to edit port profiles.""" |
|
|
|
port_profile = EditPortProfileForm(request.POST or None, instance=port_profile) |
|
|
|
if port_profile.is_valid(): |
|
|
|
if port_profile.changed_data: |
|
|
|
@ -1118,7 +1115,7 @@ def edit_port_profile(request, port_profile, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(PortProfile) |
|
|
|
def del_port_profile(request, port_profile, **_kwargs): |
|
|
|
"""Delete a port profile""" |
|
|
|
"""View used to delete port profiles.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
port_profile.delete() |
|
|
|
@ -1136,7 +1133,7 @@ def del_port_profile(request, port_profile, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(ModuleSwitch) |
|
|
|
def add_module(request): |
|
|
|
""" View used to add a Module object """ |
|
|
|
"""View used to create switch modules.""" |
|
|
|
module = EditModuleForm(request.POST or None) |
|
|
|
if module.is_valid(): |
|
|
|
module.save() |
|
|
|
@ -1150,7 +1147,7 @@ def add_module(request): |
|
|
|
@login_required |
|
|
|
@can_edit(ModuleSwitch) |
|
|
|
def edit_module(request, module_instance, **_kwargs): |
|
|
|
""" View used to edit a Module object """ |
|
|
|
"""View used to edit switch modules.""" |
|
|
|
module = EditModuleForm(request.POST or None, instance=module_instance) |
|
|
|
if module.is_valid(): |
|
|
|
if module.changed_data: |
|
|
|
@ -1165,7 +1162,7 @@ def edit_module(request, module_instance, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(ModuleSwitch) |
|
|
|
def del_module(request, module, **_kwargs): |
|
|
|
"""Compleete delete a module""" |
|
|
|
"""View used to delete switch modules.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
module.delete() |
|
|
|
@ -1190,7 +1187,7 @@ def del_module(request, module, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_create(ModuleOnSwitch) |
|
|
|
def add_module_on(request): |
|
|
|
"""Add a module to a switch""" |
|
|
|
"""View used to add a module to a switch.""" |
|
|
|
module_switch = EditSwitchModuleForm(request.POST or None) |
|
|
|
if module_switch.is_valid(): |
|
|
|
module_switch.save() |
|
|
|
@ -1206,7 +1203,7 @@ def add_module_on(request): |
|
|
|
@login_required |
|
|
|
@can_edit(ModuleOnSwitch) |
|
|
|
def edit_module_on(request, module_instance, **_kwargs): |
|
|
|
""" View used to edit a Module object """ |
|
|
|
"""View used to edit a module on a switch.""" |
|
|
|
module = EditSwitchModuleForm(request.POST or None, instance=module_instance) |
|
|
|
if module.is_valid(): |
|
|
|
if module.changed_data: |
|
|
|
@ -1221,7 +1218,7 @@ def edit_module_on(request, module_instance, **_kwargs): |
|
|
|
@login_required |
|
|
|
@can_delete(ModuleOnSwitch) |
|
|
|
def del_module_on(request, module, **_kwargs): |
|
|
|
"""Compleete delete a module""" |
|
|
|
"""View used to delete a module on a switch.""" |
|
|
|
if request.method == "POST": |
|
|
|
try: |
|
|
|
module.delete() |
|
|
|
@ -1244,9 +1241,7 @@ def del_module_on(request, module, **_kwargs): |
|
|
|
|
|
|
|
|
|
|
|
def make_machine_graph(): |
|
|
|
""" |
|
|
|
Create the graph of switchs, machines and access points. |
|
|
|
""" |
|
|
|
"""Create the graph of switches, machines and access points.""" |
|
|
|
dico = { |
|
|
|
"subs": [], |
|
|
|
"links": [], |
|
|
|
@ -1284,7 +1279,7 @@ def make_machine_graph(): |
|
|
|
"machines": [], |
|
|
|
} |
|
|
|
) |
|
|
|
# Visit all switchs in this building |
|
|
|
# Visit all switches in this building |
|
|
|
for switch in ( |
|
|
|
Switch.objects.filter(switchbay__building=building) |
|
|
|
.prefetch_related( |
|
|
|
@ -1311,7 +1306,7 @@ def make_machine_graph(): |
|
|
|
"ports": [], |
|
|
|
} |
|
|
|
) |
|
|
|
# visit all ports of this switch and add the switchs linked to it |
|
|
|
# visit all ports of this switch and add the switches linked to it |
|
|
|
for port in switch.ports.filter(related__isnull=False).select_related( |
|
|
|
"related__switch" |
|
|
|
): |
|
|
|
@ -1365,29 +1360,29 @@ def make_machine_graph(): |
|
|
|
links, new_detected = recursive_switchs(missing[0], None, [missing[0]]) |
|
|
|
for link in links: |
|
|
|
dico["links"].append(link) |
|
|
|
# Update the lists of missings and already detected switchs |
|
|
|
# Update the lists of missings and already detected switches |
|
|
|
missing = [i for i in missing if i not in new_detected] |
|
|
|
detected += new_detected |
|
|
|
# If the switch have no ports, don't explore it and hop to the next one |
|
|
|
# If the switch has no ports, don't explore it and hop to the next one |
|
|
|
else: |
|
|
|
del missing[0] |
|
|
|
# Switchs that are not connected or not in a building |
|
|
|
# Switches that are not connected or not in a building |
|
|
|
for switch in Switch.objects.filter(switchbay__isnull=True).exclude( |
|
|
|
ports__related__isnull=False |
|
|
|
): |
|
|
|
dico["alone"].append({"id": switch.id, "name": switch.get_name}) |
|
|
|
|
|
|
|
# generate the dot file |
|
|
|
# Generate the dot file |
|
|
|
dot_data = generate_dot(dico, "topologie/graph_switch.dot") |
|
|
|
|
|
|
|
# Create a temporary file to store the dot data |
|
|
|
f = tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", delete=False) |
|
|
|
with f: |
|
|
|
f.write(dot_data) |
|
|
|
unflatten = Popen( # unflatten the graph to make it look better |
|
|
|
unflatten = Popen( # Unflatten the graph to make it look better |
|
|
|
["unflatten", "-l", "3", f.name], stdout=PIPE |
|
|
|
) |
|
|
|
Popen( # pipe the result of the first command into the second |
|
|
|
Popen( # Pipe the result of the first command into the second |
|
|
|
["dot", "-Tpng", "-o", MEDIA_ROOT + "/images/switchs.png"], |
|
|
|
stdin=unflatten.stdout, |
|
|
|
stdout=PIPE, |
|
|
|
@ -1395,10 +1390,15 @@ def make_machine_graph(): |
|
|
|
|
|
|
|
|
|
|
|
def generate_dot(data, template): |
|
|
|
"""create the dot file |
|
|
|
:param data: dictionary passed to the template |
|
|
|
:param template: path to the dot template |
|
|
|
:return: all the lines of the dot file""" |
|
|
|
"""Generate a dot file from the data and template given. |
|
|
|
|
|
|
|
Args: |
|
|
|
data: dictionary passed to the template. |
|
|
|
template: path to the dot template. |
|
|
|
|
|
|
|
Returns: |
|
|
|
All the lines of the dot file. |
|
|
|
""" |
|
|
|
t = loader.get_template(template) |
|
|
|
if not isinstance(t, Template) and not ( |
|
|
|
hasattr(t, "template") and isinstance(t.template, Template) |
|
|
|
@ -1415,18 +1415,19 @@ def generate_dot(data, template): |
|
|
|
|
|
|
|
|
|
|
|
def recursive_switchs(switch_start, switch_before, detected): |
|
|
|
"""Visit the switch and travel to the switchs linked to it. |
|
|
|
:param switch_start: the switch to begin the visit on |
|
|
|
:param switch_before: the switch that you come from. |
|
|
|
None if switch_start is the first one |
|
|
|
:param detected: list of all switchs already visited. |
|
|
|
None if switch_start is the first one |
|
|
|
:return: A list of all the links found and a list of |
|
|
|
all the switchs visited |
|
|
|
"""Visit the switch and travel to the switches linked to it. |
|
|
|
|
|
|
|
Args: |
|
|
|
switch_start: the switch to begin the visit on. |
|
|
|
switch_before: the switch that you come from. None if switch_start is the first one. |
|
|
|
detected: list of all switches already visited. None if switch_start is the first one. |
|
|
|
|
|
|
|
Returns: |
|
|
|
A list of all the links found and a list of all the switches visited. |
|
|
|
""" |
|
|
|
detected.append(switch_start) |
|
|
|
links_return = [] # list of dictionaries of the links to be detected |
|
|
|
# create links to every switchs below |
|
|
|
links_return = [] # List of dictionaries of the links to be detected |
|
|
|
# Create links to every switches below |
|
|
|
for port in switch_start.ports.filter(related__isnull=False): |
|
|
|
# Not the switch that we come from, not the current switch |
|
|
|
if ( |
|
|
|
@ -1440,11 +1441,11 @@ def recursive_switchs(switch_start, switch_before, detected): |
|
|
|
} |
|
|
|
links_return.append(links) # Add current and below levels links |
|
|
|
|
|
|
|
# go down on every related switchs |
|
|
|
# Go down on every related switches |
|
|
|
for port in switch_start.ports.filter(related__isnull=False): |
|
|
|
# The switch at the end of this link has not been visited |
|
|
|
if port.related.switch not in detected: |
|
|
|
# explore it and get the results |
|
|
|
# Explore it and get the results |
|
|
|
links_down, detected = recursive_switchs( |
|
|
|
port.related.switch, switch_start, detected |
|
|
|
) |
|
|
|
|