From b2d57ec827e37e5702c673f90134f2b28c44d122 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Fri, 11 May 2018 23:17:57 +0200 Subject: [PATCH] Refactor de la fonction gen_graph + graph quand modification des objets --- .../migrations/0081_auto_20180511_1254.py | 21 +++++++++ machines/models.py | 24 ++++++++-- topologie/migrations/0060_server.py | 25 +++++++++++ topologie/models.py | 45 ++++++++++++++----- topologie/views.py | 14 +++--- 5 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 machines/migrations/0081_auto_20180511_1254.py create mode 100644 topologie/migrations/0060_server.py diff --git a/machines/migrations/0081_auto_20180511_1254.py b/machines/migrations/0081_auto_20180511_1254.py new file mode 100644 index 00000000..8f7af9f7 --- /dev/null +++ b/machines/migrations/0081_auto_20180511_1254.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-05-11 17:54 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0080_auto_20180502_2334'), + ] + + operations = [ + migrations.AlterField( + model_name='service_link', + name='server', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='machines.Interface'), + ), + ] diff --git a/machines/models.py b/machines/models.py index fe1923bb..4057bb16 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1373,9 +1373,8 @@ class Service(RevMixin, AclMixin, models.Model): def regen(service): """ Fonction externe pour régérération d'un service, prend un objet service en arg""" - obj = Service.objects.filter(service_type=service) - if obj: - obj[0].ask_regen() + obj, created = Service.objects.get_or_create(service_type=service) + obj.ask_regen() return @@ -1384,7 +1383,12 @@ class Service_link(RevMixin, AclMixin, models.Model): PRETTY_NAME = "Relation entre service et serveur" service = models.ForeignKey('Service', on_delete=models.CASCADE) - server = models.ForeignKey('Interface', on_delete=models.CASCADE) + server = models.ForeignKey( + 'Interface', + on_delete=models.CASCADE, + null=True, + blank=True + ) last_regen = models.DateTimeField(auto_now_add=True) asked_regen = models.BooleanField(default=False) @@ -1525,6 +1529,8 @@ def machine_post_save(**kwargs): user.ldap_sync(base=False, access_refresh=False, mac_refresh=True) regen('dhcp') regen('mac_ip_list') + if user == preferences.models.OptionalMachine.get_cached_value('utilisateur_asso'): + regen('graph_topo') @receiver(post_delete, sender=Machine) @@ -1549,6 +1555,8 @@ def interface_post_save(**kwargs): # Regen services regen('dhcp') regen('mac_ip_list') + if interface.machine.user == preferences.models.OptionalMachine.get_cached_value('utilisateur_asso'): + regen('graph_topo') @receiver(post_delete, sender=Interface) @@ -1659,3 +1667,11 @@ def srv_post_save(**_kwargs): def srv_post_delete(**_kwargs): """Regeneration dns après modification d'un SRV""" regen('dns') + + +@receiver(post_save, sender=Service) +def service_post_save(**kwargs): + """Création d'un service_link si non existant""" + service = kwargs['instance'] + service_link, created = Service_link.objects.get_or_create(service=service) + diff --git a/topologie/migrations/0060_server.py b/topologie/migrations/0060_server.py new file mode 100644 index 00000000..802affcf --- /dev/null +++ b/topologie/migrations/0060_server.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-05-11 17:54 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0081_auto_20180511_1254'), + ('topologie', '0059_auto_20180415_2249'), + ] + + operations = [ + migrations.CreateModel( + name='Server', + fields=[ + ], + options={ + 'proxy': True, + }, + bases=('machines.machine',), + ), + ] diff --git a/topologie/models.py b/topologie/models.py index 8640e6fc..e1c945c7 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -492,26 +492,47 @@ class Room(AclMixin, RevMixin, models.Model): def ap_post_save(**_kwargs): """Regeneration des noms des bornes vers le controleur""" regen('unifi-ap-names') - + regen("graph_topo") @receiver(post_delete, sender=AccessPoint) def ap_post_delete(**_kwargs): """Regeneration des noms des bornes vers le controleur""" regen('unifi-ap-names') - + regen("graph_topo") @receiver(post_delete, sender=Stack) def stack_post_delete(**_kwargs): """Vide les id des switches membres d'une stack supprimée""" Switch.objects.filter(stack=None).update(stack_member_id=None) -#@receiver(post_save, sender=Port) -#@receiver(post_save, sender=AccessPoint) -#@receiver(post_save, sender=ModelSwitch) -#@receiver(post_save, sender=Building) -#@receiver(post_save, sender=Building) -#@receiver(post_save, sender=Server) -@receiver(pre_save, sender=Switch) -def delete_graph(**_kwargs): - if isfile("media/images/switchs.png"): - remove("media/images/switchs.png") +@receiver(post_save, sender=Port) +def port_post_save(**_kwargs): + regen("graph_topo") + +@receiver(post_delete, sender=Port) +def port_post_delete(**_kwargs): + regen("graph_topo") + +@receiver(post_save, sender=ModelSwitch) +def modelswitch_post_save(**_kwargs): + regen("graph_topo") + +@receiver(post_delete, sender=ModelSwitch) +def modelswitch_post_delete(**_kwargs): + regen("graph_topo") + +@receiver(post_save, sender=Building) +def building_post_save(**_kwargs): + regen("graph_topo") + +@receiver(post_delete, sender=Building) +def building_post_delete(**_kwargs): + regen("graph_topo") + +@receiver(post_save, sender=Switch) +def switch_post_save(**_kwargs): + regen("graph_topo") + +@receiver(post_delete, sender=Switch) +def switch_post_delete(**_kwargs): + regen("graph_topo") diff --git a/topologie/views.py b/topologie/views.py index 992e1df1..7d0fad53 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -57,13 +57,14 @@ from re2o.acl import ( can_view, can_view_all, ) +from re2o.settings import MEDIA_ROOT from machines.forms import ( DomainForm, EditInterfaceForm, AddInterfaceForm ) from machines.views import generate_ipv4_mbf_param -from machines.models import Interface +from machines.models import Interface, Service_link from preferences.models import AssoOption, GeneralOption from .models import ( @@ -118,7 +119,10 @@ def index(request): pagination_number = GeneralOption.get_cached_value('pagination_number') switch_list = re2o_paginator(request, switch_list, pagination_number) - # make_machine_graph() + if any(service_link.need_regen() for service_link in Service_link.objects.filter(service__service_type='graph_topo')): + make_machine_graph() + for service_link in Service_link.objects.filter(service__service_type='graph_topo'): + service_link.done_regen() return render( request, @@ -1022,15 +1026,15 @@ def make_machine_graph(): #Exportation du dot et génération de l'image dot_data=generate_image(dico) - fichier = open("media/images/switchs.dot","w") + fichier = open(MEDIA_ROOT + "/images/switchs.dot","w", encoding='utf-8') fichier.write(dot_data) fichier.close() unflatten = Popen( - ["unflatten","-l", "3", "media/images/switchs.dot"], + ["unflatten","-l", "3", MEDIA_ROOT + "/images/switchs.dot"], stdout=PIPE ) image = Popen( - ["dot", "-Tpng", "-o", "media/images/switchs.png"], + ["dot", "-Tpng", "-o", MEDIA_ROOT + "/images/switchs.png"], stdin=unflatten.stdout, stdout=PIPE )