diff --git a/re2o/utils.py b/re2o/utils.py index 8336aab5..fc268e22 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -238,6 +238,10 @@ class SortTable: 'room_name': ['name'], 'default': ['name'] } + TOPOLOGIE_INDEX_BUILDING = { + 'building_name': ['name'], + 'default': ['name'] + } TOPOLOGIE_INDEX_BORNE = { 'ap_name': ['interface__domain__name'], 'ap_ip': ['interface__ipv4__ipv4'], @@ -250,12 +254,17 @@ class SortTable: 'default': ['stack_id'], } TOPOLOGIE_INDEX_MODEL_SWITCH = { - 'model_switch_name': ['reference'], - 'model_switch__contructor' : ['constructor__name'], + 'model-switch_name': ['reference'], + 'model-switch_contructor' : ['constructor__name'], 'default': ['reference'], } + TOPOLOGIE_INDEX_SWITCH_BAY = { + 'switch-bay_name': ['name'], + 'switch-bay_building': ['building__name'], + 'default': ['name'], + } TOPOLOGIE_INDEX_CONSTRUCTOR_SWITCH = { - 'room_name': ['name'], + 'constructor-switch_name': ['name'], 'default': ['name'], } LOGS_INDEX = { diff --git a/topologie/forms.py b/topologie/forms.py index 6e45a833..6d96a5af 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -170,6 +170,8 @@ 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) + class Meta: model = ModelSwitch fields = '__all__' @@ -177,6 +179,14 @@ class EditModelSwitchForm(FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditModelSwitchForm, self).__init__(*args, prefix=prefix, **kwargs) + instance = kwargs.get('instance', None) + if instance: + self.initial['members'] = Switch.objects.filter(model=instance) + + def save(self, commit=True): + instance = super().save(commit) + instance.switch_set = self.cleaned_data['members'] + return instance class EditConstructorSwitchForm(FormRevMixin, ModelForm): @@ -192,6 +202,8 @@ class EditConstructorSwitchForm(FormRevMixin, ModelForm): class EditSwitchBayForm(FormRevMixin, ModelForm): """Permet d'éditer une baie de brassage""" + members = forms.ModelMultipleChoiceField(Switch.objects.all(), required=False) + class Meta: model = SwitchBay fields = '__all__' @@ -199,6 +211,14 @@ class EditSwitchBayForm(FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditSwitchBayForm, self).__init__(*args, prefix=prefix, **kwargs) + instance = kwargs.get('instance', None) + if instance: + self.initial['members'] = Switch.objects.filter(switchbay=instance) + + def save(self, commit=True): + instance = super().save(commit) + instance.switch_set = self.cleaned_data['members'] + return instance class EditBuildingForm(FormRevMixin, ModelForm): diff --git a/topologie/migrations/0057_auto_20180408_0316.py b/topologie/migrations/0057_auto_20180408_0316.py new file mode 100644 index 00000000..e4a0d09b --- /dev/null +++ b/topologie/migrations/0057_auto_20180408_0316.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-04-08 01:16 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0056_building_switchbay'), + ] + + operations = [ + migrations.RemoveField( + model_name='switchbay', + name='members', + ), + migrations.AddField( + model_name='switch', + name='switchbay', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='topologie.SwitchBay'), + ), + ] diff --git a/topologie/models.py b/topologie/models.py index 5fd5056a..b30c5a55 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -136,6 +136,12 @@ class Switch(AclMixin, Machine): null=True, on_delete=models.SET_NULL ) + switchbay = models.ForeignKey( + 'topologie.SwitchBay', + blank=True, + null=True, + on_delete=models.SET_NULL + ) class Meta: unique_together = ('stack', 'stack_member_id') @@ -233,11 +239,6 @@ class SwitchBay(AclMixin, RevMixin, models.Model): 'Building', on_delete=models.PROTECT ) - members = models.ManyToManyField( - blank=True, - to='Switch', - related_name='bay_switches' - ) info = models.CharField( max_length=255, blank=True, diff --git a/topologie/templates/topologie/aff_switch_bay.html b/topologie/templates/topologie/aff_switch_bay.html index 6f8ab848..e8d11c60 100644 --- a/topologie/templates/topologie/aff_switch_bay.html +++ b/topologie/templates/topologie/aff_switch_bay.html @@ -32,7 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% include "buttons/sort.html" with prefix='switch-bay' col='name' text='Baie' %} - Bâtiment + {% include "buttons/sort.html" with prefix='switch-bay' col='building' text='Bâtiment' %} Info particulières Switchs du batiment @@ -43,9 +43,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {{switch_bay.name}} {{switch_bay.building}} {{switch_bay.info}} - {{switch_bay.members}} + {{switch_bay.switch_set.all |join:", "}} - + {% can_edit switch_bay %} diff --git a/topologie/templates/topologie/index_model_switch.html b/topologie/templates/topologie/index_model_switch.html index ea346f56..19752b2a 100644 --- a/topologie/templates/topologie/index_model_switch.html +++ b/topologie/templates/topologie/index_model_switch.html @@ -47,6 +47,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% acl_end %} {% include "topologie/aff_switch_bay.html" with switch_bay_list=switch_bay_list %} +

Batiment

+{% can_create Building %} + Ajouter un bâtiment +
+{% acl_end %} +{% include "topologie/aff_building.html" with building_list=building_list %}


diff --git a/topologie/templates/topologie/topo.html b/topologie/templates/topologie/topo.html index 019625f9..7d1662f0 100644 --- a/topologie/templates/topologie/topo.html +++ b/topologie/templates/topologie/topo.html @@ -36,7 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% endif %}
{% csrf_token %} - {% massive_bootstrap_form topoform 'room,related,machine_interface' %} + {% massive_bootstrap_form topoform 'room,related,machine_interface,members' %} {% bootstrap_button action_name button_type="submit" icon="ok" %}

diff --git a/topologie/urls.py b/topologie/urls.py index c7243200..66da7642 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -109,4 +109,14 @@ urlpatterns = [ url(r'^del_switch_bay/(?P[0-9]+)$', views.del_switch_bay, name='del-switch-bay'), + url(r'^new_building/$', + views.new_building, + name='new-building' + ), + url(r'^edit_building/(?P[0-9]+)$', + views.edit_building, + name='edit-building'), + url(r'^del_building/(?P[0-9]+)$', + views.del_building, + name='del-building'), ] diff --git a/topologie/views.py b/topologie/views.py index 57ff3cb6..e28db5a3 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -205,6 +205,7 @@ def index_model_switch(request): model_switch_list = ModelSwitch.objects.select_related('constructor') constructor_switch_list = ConstructorSwitch.objects switch_bay_list = SwitchBay.objects.select_related('building') + building_list = Building.objects.all() model_switch_list = SortTable.sort( model_switch_list, request.GET.get('col'), @@ -217,10 +218,23 @@ def index_model_switch(request): request.GET.get('order'), SortTable.TOPOLOGIE_INDEX_CONSTRUCTOR_SWITCH ) + building_list = SortTable.sort( + building_list, + request.GET.get('col'), + request.GET.get('order'), + SortTable.TOPOLOGIE_INDEX_BUILDING + ) + switch_bay_list = SortTable.sort( + switch_bay_list, + request.GET.get('col'), + request.GET.get('order'), + SortTable.TOPOLOGIE_INDEX_SWITCH_BAY + ) return render(request, 'topologie/index_model_switch.html', { 'model_switch_list': model_switch_list, 'constructor_switch_list': constructor_switch_list, 'switch_bay_list': switch_bay_list, + 'building_list' : building_list, }) @@ -684,6 +698,49 @@ def del_switch_bay(request, switch_bay, switchbayid): }, 'topologie/delete.html', request) +@login_required +@can_create(Building) +def new_building(request): + """Nouveau batiment""" + building = EditBuildingForm(request.POST or None) + if building.is_valid(): + building.save() + messages.success(request, "Le batiment a été créé") + return redirect(reverse('topologie:index-model-switch')) + return form({'topoform': building, 'action_name' : 'Ajouter'}, 'topologie/topo.html', request) + + +@login_required +@can_edit(Building) +def edit_building(request, building, buildingid): + """ Edition d'un batiment""" + building = EditBuildingForm(request.POST or None, instance=building) + if building.is_valid(): + if building.changed_data: + building.save() + messages.success(request, "Le batiment a bien été modifié") + return redirect(reverse('topologie:index-model-switch')) + return form({'topoform': building, 'action_name' : 'Editer'}, 'topologie/topo.html', request) + + +@login_required +@can_delete(Building) +def del_building(request, building, buildingid): + """ Suppression d'un batiment""" + if request.method == "POST": + try: + building.delete() + messages.success(request, "La batiment a été détruit") + except ProtectedError: + messages.error(request, "Le batiment %s est affecté à un autre objet,\ + impossible de la supprimer (switch ou user)" % building) + return redirect(reverse('topologie:index-model-switch')) + return form({ + 'objet': building, + 'objet_name': 'Bâtiment' + }, 'topologie/delete.html', request) + + @login_required @can_create(ConstructorSwitch) def new_constructor_switch(request):