Commit 8afc8e43 authored by Maël Kervella's avatar Maël Kervella

Pep8 compliance on topologie

parent 48d904b6
......@@ -26,6 +26,7 @@
Here are defined some functions to check acl on the application.
"""
def can_view(user):
"""Check if an user can view the application.
......
......@@ -144,7 +144,7 @@ def search_single_word(word, filters, user,
if not User.can_view_all(user)[0]:
filter_users &= Q(id=user.id)
filter_clubs = filter_users
filter_users |= Q(name__icontains=word)
filter_users |= Q(name__icontains=word)
filters['users'] |= filter_users
filters['clubs'] |= filter_clubs
......
......@@ -26,6 +26,7 @@
Here are defined some functions to check acl on the application.
"""
def can_view(user):
"""Check if an user can view the application.
......
......@@ -41,7 +41,7 @@ from machines.forms import (
from django import forms
from django.forms import ModelForm, Form
from django.db.models import Prefetch
from .models import (
from .models import (
Port,
Switch,
Room,
......@@ -54,6 +54,7 @@ from .models import (
)
from re2o.mixins import FormRevMixin
class PortForm(FormRevMixin, ModelForm):
"""Formulaire pour la création d'un port d'un switch
Relié directement au modèle port"""
......@@ -82,32 +83,48 @@ class EditPortForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditPortForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['machine_interface'].queryset = Interface.objects.all()\
.select_related('domain__extension')
self.fields['related'].queryset = Port.objects.all()\
self.fields['machine_interface'].queryset = (
Interface.objects.all().select_related('domain__extension')
)
self.fields['related'].queryset = (
Port.objects.all()
.prefetch_related(Prefetch(
'switch__interface_set',
queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension')
'switch__interface_set',
queryset=(Interface.objects
.select_related('ipv4__ip_type__extension')
.select_related('domain__extension'))
))
)
class AddPortForm(FormRevMixin, ModelForm):
"""Permet d'ajouter un port de switch. Voir EditPortForm pour plus
d'informations"""
class Meta(PortForm.Meta):
fields = ['port', 'room', 'machine_interface', 'related',
'radius', 'vlan_force', 'details']
fields = [
'port',
'room',
'machine_interface',
'related',
'radius',
'vlan_force',
'details'
]
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(AddPortForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['machine_interface'].queryset = Interface.objects.all()\
.select_related('domain__extension')
self.fields['related'].queryset = Port.objects.all()\
.prefetch_related(Prefetch(
'switch__interface_set',
queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension')
self.fields['machine_interface'].queryset = (
Interface.objects.all().select_related('domain__extension')
)
self.fields['related'].queryset = (
Port.objects.all().prefetch_related(Prefetch(
'switch__interface_set',
queryset=(Interface.objects
.select_related('ipv4__ip_type__extension')
.select_related('domain__extension'))
))
)
class StackForm(FormRevMixin, ModelForm):
......@@ -170,15 +187,22 @@ class CreatePortsForm(forms.Form):
class EditModelSwitchForm(FormRevMixin, ModelForm):
"""Permet d'éediter un modèle de switch : nom et constructeur"""
members = forms.ModelMultipleChoiceField(Switch.objects.all(), required=False)
members = forms.ModelMultipleChoiceField(
Switch.objects.all(),
required=False
)
class Meta:
model = ModelSwitch
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditModelSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
super(EditModelSwitchForm, self).__init__(
*args,
prefix=prefix,
**kwargs
)
instance = kwargs.get('instance', None)
if instance:
self.initial['members'] = Switch.objects.filter(model=instance)
......@@ -197,13 +221,20 @@ class EditConstructorSwitchForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditConstructorSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
super(EditConstructorSwitchForm, self).__init__(
*args,
prefix=prefix,
**kwargs
)
class EditSwitchBayForm(FormRevMixin, ModelForm):
"""Permet d'éditer une baie de brassage"""
members = forms.ModelMultipleChoiceField(Switch.objects.all(), required=False)
members = forms.ModelMultipleChoiceField(
Switch.objects.all(),
required=False
)
class Meta:
model = SwitchBay
fields = '__all__'
......
......@@ -50,6 +50,7 @@ from reversion import revisions as reversion
from machines.models import Machine, Interface, regen
from re2o.mixins import AclMixin, RevMixin
class Stack(AclMixin, RevMixin, models.Model):
"""Un objet stack. Regrouppe des switchs en foreign key
,contient une id de stack, un switch id min et max dans
......@@ -85,12 +86,12 @@ class Stack(AclMixin, RevMixin, models.Model):
class AccessPoint(AclMixin, Machine):
"""Define a wireless AP. Inherit from machines.interfaces
Definition pour une borne wifi , hérite de machines.interfaces
"""
PRETTY_NAME = "Borne WiFi"
location = models.CharField(
location = models.CharField(
max_length=255,
help_text="Détails sur la localisation de l'AP",
blank=True,
......@@ -120,7 +121,6 @@ class Switch(AclMixin, Machine):
id_max de la stack parente"""
PRETTY_NAME = "Switch / Commutateur"
number = models.PositiveIntegerField()
stack = models.ForeignKey(
'topologie.Stack',
......@@ -165,7 +165,8 @@ class Switch(AclMixin, Machine):
ne peut être nul"})
def create_ports(self, begin, end):
""" Crée les ports de begin à end si les valeurs données sont cohérentes. """
""" Crée les ports de begin à end si les valeurs données
sont cohérentes. """
s_begin = s_end = 0
nb_ports = self.ports.count()
......
......@@ -51,12 +51,10 @@ urlpatterns = [
url(r'^switch/(?P<switchid>[0-9]+)$',
views.index_port,
name='index-port'),
url(
r'^history/(?P<object_name>\w+)/(?P<object_id>[0-9]+)$',
url(r'^history/(?P<object_name>\w+)/(?P<object_id>[0-9]+)$',
re2o.views.history,
name='history',
kwargs={'application':'topologie'},
),
kwargs={'application': 'topologie'}),
url(r'^edit_port/(?P<portid>[0-9]+)$', views.edit_port, name='edit-port'),
url(r'^new_port/(?P<switchid>[0-9]+)$', views.new_port, name='new-port'),
url(r'^del_port/(?P<portid>[0-9]+)$', views.del_port, name='del-port'),
......@@ -64,7 +62,9 @@ urlpatterns = [
views.edit_switch,
name='edit-switch'),
url(r'^new_stack/$', views.new_stack, name='new-stack'),
url(r'^index_physical_grouping/$', views.index_physical_grouping, name='index-physical-grouping'),
url(r'^index_physical_grouping/$',
views.index_physical_grouping,
name='index-physical-grouping'),
url(r'^edit_stack/(?P<stackid>[0-9]+)$',
views.edit_stack,
name='edit-stack'),
......@@ -73,16 +73,13 @@ urlpatterns = [
name='del-stack'),
url(r'^index_model_switch/$',
views.index_model_switch,
name='index-model-switch'
),
name='index-model-switch'),
url(r'^index_model_switch/$',
views.index_model_switch,
name='index-model-switch'
),
name='index-model-switch'),
url(r'^new_model_switch/$',
views.new_model_switch,
name='new-model-switch'
),
name='new-model-switch'),
url(r'^edit_model_switch/(?P<modelswitchid>[0-9]+)$',
views.edit_model_switch,
name='edit-model-switch'),
......@@ -91,8 +88,7 @@ urlpatterns = [
name='del-model-switch'),
url(r'^new_constructor_switch/$',
views.new_constructor_switch,
name='new-constructor-switch'
),
name='new-constructor-switch'),
url(r'^edit_constructor_switch/(?P<constructorswitchid>[0-9]+)$',
views.edit_constructor_switch,
name='edit-constructor-switch'),
......@@ -101,8 +97,7 @@ urlpatterns = [
name='del-constructor-switch'),
url(r'^new_switch_bay/$',
views.new_switch_bay,
name='new-switch-bay'
),
name='new-switch-bay'),
url(r'^edit_switch_bay/(?P<switchbayid>[0-9]+)$',
views.edit_switch_bay,
name='edit-switch-bay'),
......@@ -111,8 +106,7 @@ urlpatterns = [
name='del-switch-bay'),
url(r'^new_building/$',
views.new_building,
name='new-building'
),
name='new-building'),
url(r'^edit_building/(?P<buildingid>[0-9]+)$',
views.edit_building,
name='edit-building'),
......
......@@ -93,12 +93,14 @@ from preferences.models import AssoOption, GeneralOption
@can_view_all(Switch)
def index(request):
""" Vue d'affichage de tous les swicthes"""
switch_list = Switch.objects\
.prefetch_related(Prefetch(
'interface_set',
queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension')
))\
.select_related('stack')
switch_list = (Switch.objects
.prefetch_related(Prefetch(
'interface_set',
queryset=(Interface.objects
.select_related('ipv4__ip_type__extension')
.select_related('domain__extension'))
))
.select_related('stack'))
switch_list = SortTable.sort(
switch_list,
request.GET.get('col'),
......@@ -107,9 +109,11 @@ def index(request):
)
pagination_number = GeneralOption.get_cached_value('pagination_number')
switch_list = re2o_paginator(request, switch_list, pagination_number)
return render(request, 'topologie/index.html', {
'switch_list': switch_list
})
return render(
request,
'topologie/index.html',
{'switch_list': switch_list}
)
@login_required
......@@ -117,27 +121,33 @@ def index(request):
@can_view(Switch)
def index_port(request, switch, switchid):
""" Affichage de l'ensemble des ports reliés à un switch particulier"""
port_list = Port.objects.filter(switch=switch)\
.select_related('room')\
.select_related('machine_interface__domain__extension')\
.select_related('machine_interface__machine__user')\
.select_related('related__switch')\
.prefetch_related(Prefetch(
'related__switch__interface_set',
queryset=Interface.objects.select_related('domain__extension')
))\
.select_related('switch')
port_list = (Port.objects
.filter(switch=switch)
.select_related('room')
.select_related('machine_interface__domain__extension')
.select_related('machine_interface__machine__user')
.select_related('related__switch')
.prefetch_related(Prefetch(
'related__switch__interface_set',
queryset=(Interface.objects
.select_related('domain__extension'))
))
.select_related('switch'))
port_list = SortTable.sort(
port_list,
request.GET.get('col'),
request.GET.get('order'),
SortTable.TOPOLOGIE_INDEX_PORT
)
return render(request, 'topologie/index_p.html', {
'port_list': port_list,
'id_switch': switchid,
'nom_switch': switch
})
return render(
request,
'topologie/index_p.html',
{
'port_list': port_list,
'id_switch': switchid,
'nom_switch': switch
}
)
@login_required
......@@ -153,20 +163,24 @@ def index_room(request):
)
pagination_number = GeneralOption.get_cached_value('pagination_number')
room_list = re2o_paginator(request, room_list, pagination_number)
return render(request, 'topologie/index_room.html', {
'room_list': room_list
})
return render(
request,
'topologie/index_room.html',
{'room_list': room_list}
)
@login_required
@can_view_all(AccessPoint)
def index_ap(request):
""" Affichage de l'ensemble des bornes"""
ap_list = AccessPoint.objects\
.prefetch_related(Prefetch(
'interface_set',
queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension')
))
ap_list = (AccessPoint.objects
.prefetch_related(Prefetch(
'interface_set',
queryset=(Interface.objects
.select_related('ipv4__ip_type__extension')
.select_related('domain__extension'))
)))
ap_list = SortTable.sort(
ap_list,
request.GET.get('col'),
......@@ -175,9 +189,11 @@ def index_ap(request):
)
pagination_number = GeneralOption.get_cached_value('pagination_number')
ap_list = re2o_paginator(request, ap_list, pagination_number)
return render(request, 'topologie/index_ap.html', {
'ap_list': ap_list
})
return render(
request,
'topologie/index_ap.html',
{'ap_list': ap_list}
)
@login_required
......@@ -186,8 +202,10 @@ def index_ap(request):
@can_view_all(SwitchBay)
def index_physical_grouping(request):
"""Affichage de la liste des stacks (affiche l'ensemble des switches)"""
stack_list = Stack.objects\
.prefetch_related('switch_set__interface_set__domain__extension')
stack_list = (Stack.objects
.prefetch_related(
'switch_set__interface_set__domain__extension'
))
building_list = Building.objects.all()
switch_bay_list = SwitchBay.objects.select_related('building')
stack_list = SortTable.sort(
......@@ -208,11 +226,15 @@ def index_physical_grouping(request):
request.GET.get('order'),
SortTable.TOPOLOGIE_INDEX_SWITCH_BAY
)
return render(request, 'topologie/index_physical_grouping.html', {
'stack_list': stack_list,
'switch_bay_list': switch_bay_list,
'building_list' : building_list,
})
return render(
request,
'topologie/index_physical_grouping.html',
{
'stack_list': stack_list,
'switch_bay_list': switch_bay_list,
'building_list': building_list,
}
)
@login_required
......@@ -234,10 +256,14 @@ def index_model_switch(request):
request.GET.get('order'),
SortTable.TOPOLOGIE_INDEX_CONSTRUCTOR_SWITCH
)
return render(request, 'topologie/index_model_switch.html', {
'model_switch_list': model_switch_list,
'constructor_switch_list': constructor_switch_list,
})
return render(
request,
'topologie/index_model_switch.html',
{
'model_switch_list': model_switch_list,
'constructor_switch_list': constructor_switch_list,
}
)
@login_required
......@@ -260,9 +286,12 @@ def new_port(request, switchid):
messages.error(request, "Ce port existe déjà")
return redirect(reverse(
'topologie:index-port',
kwargs={'switchid':switchid}
))
return form({'id_switch': switchid,'topoform': port, 'action_name' : 'Ajouter'}, 'topologie/topo.html', request)
kwargs={'switchid': switchid}
))
return form(
{'id_switch': switchid, 'topoform': port, 'action_name': 'Ajouter'},
'topologie/topo.html',
request)
@login_required
......@@ -279,8 +308,16 @@ def edit_port(request, port_object, portid):
return redirect(reverse(
'topologie:index-port',
kwargs={'switchid': str(port_object.switch.id)}
))
return form({'id_switch': str(port_object.switch.id), 'topoform': port, 'action_name' : 'Editer'}, 'topologie/topo.html', request)
))
return form(
{
'id_switch': str(port_object.switch.id),
'topoform': port,
'action_name': 'Editer'
},
'topologie/topo.html',
request
)
@login_required
......@@ -292,12 +329,15 @@ def del_port(request, port, portid):
port.delete()
messages.success(request, "Le port a été détruit")
except ProtectedError:
messages.error(request, "Le port %s est affecté à un autre objet,\
impossible de le supprimer" % port)
messages.error(
request,
("Le port %s est affecté à un autre objet, impossible "
"de le supprimer" % port)
)
return redirect(reverse(
'topologie:index-port',
kwargs={'switchid':str(port.switch.id)}
))
kwargs={'switchid': str(port.switch.id)}
))
return form({'objet': port}, 'topologie/delete.html', request)
......@@ -309,7 +349,11 @@ def new_stack(request):
if stack.is_valid():
stack.save()
messages.success(request, "Stack crée")
return form({'topoform': stack, 'action_name' : 'Créer'}, 'topologie/topo.html', request)
return form(
{'topoform': stack, 'action_name': 'Créer'},
'topologie/topo.html',
request
)
@login_required
......@@ -321,7 +365,11 @@ def edit_stack(request, stack, stackid):
if stack.changed_data:
stack.save()
return redirect(reverse('topologie:index-physical-grouping'))
return form({'topoform': stack, 'action_name' : 'Editer'}, 'topologie/topo.html', request)
return form(
{'topoform': stack, 'action_name': 'Editer'},
'topologie/topo.html',
request
)
@login_required
......@@ -333,8 +381,11 @@ def del_stack(request, stack, stackid):
stack.delete()
messages.success(request, "La stack a eté détruite")
except ProtectedError:
messages.error(request, "La stack %s est affectée à un autre\
objet, impossible de la supprimer" % stack)
messages.error(
request,
("La stack %s est affectée à un autre objet, impossible "
"de la supprimer" % stack)
)
return redirect(reverse('topologie:index-physical-grouping'))
return form({'objet': stack}, 'topologie/delete.html', request)
......@@ -372,8 +423,11 @@ def new_switch(request):
if switch.is_valid() and interface.is_valid():
user = AssoOption.get_cached_value('utilisateur_asso')
if not user:
messages.error(request, "L'user association n'existe pas encore,\
veuillez le créer ou le linker dans preferences")
messages.error(
request,
("L'user association n'existe pas encore, veuillez le "
"créer ou le linker dans preferences")
)
return redirect(reverse('topologie:index'))
new_switch = switch.save(commit=False)
new_switch.user = user
......@@ -389,13 +443,17 @@ def new_switch(request):
messages.success(request, "Le switch a été créé")
return redirect(reverse('topologie:index'))
i_mbf_param = generate_ipv4_mbf_param(interface, False)
return form({
'topoform': interface,
'machineform': switch,
'domainform': domain,
'i_mbf_param': i_mbf_param,
'device' : 'switch',
}, 'topologie/topo_more.html', request)
return form(
{
'topoform': interface,
'machineform': switch,
'domainform': domain,
'i_mbf_param': i_mbf_param,
'device': 'switch',
},
'topologie/topo_more.html',
request
)
@login_required
......@@ -430,9 +488,13 @@ def create_ports(request, switchid):
messages.error(request, ''.join(e))
return redirect(reverse(
'topologie:index-port',
kwargs={'switchid':switchid}
kwargs={'switchid': switchid}
))
return form({'id_switch': switchid, 'topoform': port_form}, 'topologie/switch.html', request)
return form(
{'id_switch': switchid, 'topoform': port_form},
'topologie/switch.html',
request
)
@login_required
......@@ -467,15 +529,19 @@ def edit_switch(request, switch, switchid):
new_domain.save()
messages.success(request, "Le switch a bien été modifié")
return redirect(reverse('topologie:index'))
i_mbf_param = generate_ipv4_mbf_param(interface_form, False )
return form({
'id_switch': switchid,
'topoform': interface_form,
'machineform': switch_form,
'domainform': domain_form,
'i_mbf_param': i_mbf_param,
'device' : 'switch',
}, 'topologie/topo_more.html', request)
i_mbf_param = generate_ipv4_mbf_param(interface_form, False)
return form(
{
'id_switch': switchid,
'topoform': interface_form,
'machineform': switch_form,
'domainform': domain_form,
'i_mbf_param': i_mbf_param,
'device': 'switch',
},
'topologie/topo_more.html',
request
)
@login_required
......@@ -498,8 +564,11 @@ def new_ap(request):
if ap.is_valid() and interface.is_valid():
user = AssoOption.get_cached_value('utilisateur_asso')
if not user:
messages.error(request, "L'user association n'existe pas encore,\
veuillez le créer ou le linker dans preferences")
messages.error(
request,
("L'user association n'existe pas encore, veuillez le "
"créer ou le linker dans preferences")
)
return redirect(reverse('topologie:index'))
new_ap = ap.save(commit=False)
new_ap.user = user
......@@ -515,13 +584,17 @@ def new_ap(request):
messages.success(request, "La borne a été créé")
return redirect(reverse('topologie:index-ap'))
i_mbf_param = generate_ipv4_mbf_param(interface, False)
return form({
'topoform': interface,
'machineform': ap,
'domainform': domain,
'i_mbf_param': i_mbf_param,
'device' : 'wifi ap',
}, 'topologie/topo_more.html', request)
return form(
{
'topoform': interface,
'machineform': ap,
'domainform': domain,
'i_mbf_param': i_mbf_param,
'device': 'wifi ap',
},
'topologie/topo_more.html',
request
)
@login_required
......@@ -546,8 +619,11 @@ def edit_ap(request, ap, accesspointid):
if ap_form.is_valid() and interface_form.is_valid():
user = AssoOption.get_cached_value('utilisateur_asso')
if not user:
messages.error(request, "L'user association n'existe pas encore,\
veuillez le créer ou le linker dans preferences")
messages.error(
request,
("L'user association n'existe pas encore, veuillez le "
"créer ou le linker dans preferences")
)
return redirect(reverse('topologie:index-ap'))
new_ap = ap_form.save(commit=False)
new_interface = interface_form.save(commit=False)
......@@ -560,15 +636,19 @@ def edit_ap(request, ap, accesspointid):
new_domain.save()
messages.success(request, "La borne a été modifiée")
return redirect(reverse('topologie:index-ap'))
i_mbf_param = generate_ipv4_mbf_param(interface_form, False )
return form({
'topoform': interface_form,
'machineform': ap_form,
'domainform': domain_form,
'i_mbf_param': i_mbf_param,
'device' : 'wifi ap',
}, 'topologie/topo_more.html', request)
i_mbf_param = generate_ipv4_mbf_param(interface_form, False)
return form(
{
'topoform': interface_form,
'machineform': ap_form,
'domainform': domain_form,
'i_mbf_param': i_mbf_param,
'device': 'wifi ap',
},
'topologie/topo_more.html',
request
)
@login_required
@can_create(Room)
......@@ -579,7 +659,11 @@ def new_room(request):
room.save()
messages.success(request, "La chambre a été créé")
return redirect(reverse('topologie:index-room'))
return form({'topoform': room, 'action_name' : 'Ajouter'}, 'topologie/topo.html', request)
return form(
{'topoform': room, 'action_name': 'Ajouter'},
'topologie/topo.html',
request
)
@login_required
......@@ -592,7 +676,11 @@ def edit_room(request, room, roomid):
room.save()
messages.success(request, "La chambre a bien été modifiée")
return redirect(reverse('topologie:index-room'))
return form({'topoform': room, 'action_name' : 'Editer'}, 'topologie/topo.html', request)
return form(
{'topoform': room, 'action_name': 'Editer'},
'topologie/topo.html',
request
)
@login_required
......@@ -604,13 +692,17 @@ def del_room(request, room, roomid):
room.delete()
messages.success(request, "La chambre/prise a été détruite")
except ProtectedError:
messages.error(request, "La chambre %s est affectée à un autre objet,\
impossible de la supprimer (switch ou user)" % room)
messages.error(
request,
("La chambre %s est affectée à un autre objet, impossible "
"de la supprimer (switch ou user)" % room)
)
return redirect(reverse('topologie:index-room'))
return form({
'objet': room,
'objet_name': 'Chambre'
}, 'topologie/delete.html', request)
return form(
{'objet': room, 'objet_name': 'Chambre'},
'topologie/delete.html',
request