2018-10-18 15:43:55 -04:00
|
|
|
from django.db.models.signals import post_save, post_delete, pre_delete
|
2018-02-01 11:39:13 -05:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2018-10-18 15:43:55 -04:00
|
|
|
from .models import Cable, Device, VirtualChassis
|
2018-02-01 11:39:13 -05:00
|
|
|
|
|
|
|
|
2018-02-01 13:52:41 -05:00
|
|
|
@receiver(post_save, sender=VirtualChassis)
|
|
|
|
def assign_virtualchassis_master(instance, created, **kwargs):
|
|
|
|
"""
|
|
|
|
When a VirtualChassis is created, automatically assign its master device to the VC.
|
|
|
|
"""
|
|
|
|
if created:
|
2018-07-18 14:17:35 -04:00
|
|
|
Device.objects.filter(pk=instance.master.pk).update(virtual_chassis=instance, vc_position=None)
|
2018-02-01 13:52:41 -05:00
|
|
|
|
|
|
|
|
2018-02-01 11:39:13 -05:00
|
|
|
@receiver(pre_delete, sender=VirtualChassis)
|
|
|
|
def clear_virtualchassis_members(instance, **kwargs):
|
|
|
|
"""
|
|
|
|
When a VirtualChassis is deleted, nullify the vc_position and vc_priority fields of its prior members.
|
|
|
|
"""
|
|
|
|
Device.objects.filter(virtual_chassis=instance.pk).update(vc_position=None, vc_priority=None)
|
2018-10-18 15:43:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Cable)
|
|
|
|
def update_connected_endpoints(instance, **kwargs):
|
2018-10-31 15:01:01 -04:00
|
|
|
|
|
|
|
# Cache the Cable on its two termination points
|
|
|
|
instance.termination_a.cable = instance
|
|
|
|
instance.termination_a.save()
|
|
|
|
instance.termination_b.cable = instance
|
|
|
|
instance.termination_b.save()
|
|
|
|
|
|
|
|
# Check if this Cable has formed a complete path. If so, update both endpoints.
|
2018-10-31 13:59:44 -04:00
|
|
|
endpoint_a, endpoint_b = instance.get_path_endpoints()
|
|
|
|
if endpoint_a is not None and endpoint_b is not None:
|
|
|
|
endpoint_a.connected_endpoint = endpoint_b
|
|
|
|
endpoint_a.connection_status = True
|
|
|
|
endpoint_a.save()
|
|
|
|
endpoint_b.connected_endpoint = endpoint_a
|
|
|
|
endpoint_b.connection_status = True
|
|
|
|
endpoint_b.save()
|
2018-10-18 15:43:55 -04:00
|
|
|
|
|
|
|
|
2018-10-31 15:01:01 -04:00
|
|
|
@receiver(pre_delete, sender=Cable)
|
2018-10-18 15:43:55 -04:00
|
|
|
def nullify_connected_endpoints(instance, **kwargs):
|
2018-10-31 15:01:01 -04:00
|
|
|
|
|
|
|
# Disassociate the Cable from its termination points
|
|
|
|
instance.termination_a.cable = None
|
|
|
|
instance.termination_a.save()
|
|
|
|
instance.termination_b.cable = None
|
|
|
|
instance.termination_b.save()
|
|
|
|
|
|
|
|
# If this Cable was part of a complete path, tear it down
|
2018-10-31 13:59:44 -04:00
|
|
|
endpoint_a, endpoint_b = instance.get_path_endpoints()
|
|
|
|
if endpoint_a is not None and endpoint_b is not None:
|
|
|
|
endpoint_a.connected_endpoint = None
|
|
|
|
endpoint_a.connection_status = None
|
|
|
|
endpoint_a.save()
|
|
|
|
endpoint_b.connected_endpoint = None
|
|
|
|
endpoint_b.connection_status = None
|
|
|
|
endpoint_b.save()
|