diff --git a/topologie/migrations/0024_auto_20170818_1021.py b/topologie/migrations/0024_auto_20170818_1021.py new file mode 100644 index 00000000..0d11be65 --- /dev/null +++ b/topologie/migrations/0024_auto_20170818_1021.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-08-18 08:21 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0023_auto_20170817_1654'), + ] + + operations = [ + migrations.AlterField( + model_name='switch', + name='stack', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='topologie.Stack'), + ), + ] diff --git a/topologie/models.py b/topologie/models.py index 5d512f05..c28f1d4f 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -21,6 +21,8 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from django.db import models +from django.db.models.signals import post_delete +from django.dispatch import receiver from django.forms import ModelForm, Form from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.fields import GenericForeignKey @@ -69,7 +71,7 @@ class Switch(models.Model): location = models.CharField(max_length=255) number = models.IntegerField() details = models.CharField(max_length=255, blank=True) - stack = models.ForeignKey(Stack, blank=True, null=True) + stack = models.ForeignKey(Stack, blank=True, null=True, on_delete=models.SET_NULL) stack_member_id = models.IntegerField(blank=True, null=True) class Meta: @@ -138,3 +140,6 @@ class Room(models.Model): def __str__(self): return str(self.name) +@receiver(post_delete, sender=Stack) +def stack_post_delete(sender, **kwargs): + Switch.objects.filter(stack=None).update(stack_member_id = None) diff --git a/topologie/templates/topologie/aff_stacks.html b/topologie/templates/topologie/aff_stacks.html index 18dcb30a..e4ca286d 100644 --- a/topologie/templates/topologie/aff_stacks.html +++ b/topologie/templates/topologie/aff_stacks.html @@ -43,6 +43,9 @@ with this program; if not, write to the Free Software Foundation, Inc., + + + {% endif %} diff --git a/topologie/urls.py b/topologie/urls.py index 3c2270b2..776c4259 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -42,5 +42,6 @@ urlpatterns = [ url(r'^new_stack/$', views.new_stack, name='new-stack'), url(r'^index_stack/$', views.index_stack, name='index-stack'), url(r'^edit_stack/(?P[0-9]+)$', views.edit_stack, name='edit-stack'), + url(r'^del_stack/(?P[0-9]+)$', views.del_stack, name='del-stack'), ] diff --git a/topologie/views.py b/topologie/views.py index cc6b5284..121ff65e 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -179,7 +179,7 @@ def edit_stack(request,stack_id): stack = Stack.objects.get(pk=stack_id) except Stack.DoesNotExist: messages.error(request, u"Stack inexistante") - return redirect("/topologie/index_stack/") + return redirect('/topologie/index_stack/') stack = StackForm(request.POST or None, instance=stack) if stack.is_valid(): with transaction.atomic(), reversion.create_revision(): @@ -189,6 +189,26 @@ def edit_stack(request,stack_id): return redirect('/topologie/index_stack') return form({'topoform':stack}, 'topologie/topo.html', request) +@login_required +@permission_required('infra') +def del_stack(request,stack_id): + try: + stack = Stack.objects.get(pk=stack_id) + except Stack.DoesNotExist: + messages.error(request, u"Stack inexistante") + return redirect('/topologie/index_stack') + if request.method == "POST": + try: + with transaction.atomic(), reversion.create_revision(): + stack.delete() + reversion.set_user(request.user) + reversion.set_comment("Destruction") + 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) + return redirect('/topologie/index_stack') + return form({'objet':stack}, 'topologie/delete.html', request) + @login_required @permission_required('infra') def new_switch(request):