Moved cable caching logic from Cable.save() to signals

This commit is contained in:
Jeremy Stretch 2018-10-31 15:01:01 -04:00
parent 8992c57039
commit d22c23290f
2 changed files with 18 additions and 13 deletions

View File

@ -68,6 +68,7 @@ class CableTermination(models.Model):
cable = models.ForeignKey(
to='dcim.Cable',
on_delete=models.SET_NULL,
related_name='+',
blank=True,
null=True
)
@ -2426,12 +2427,6 @@ class Cable(ChangeLoggedModel):
super(Cable, self).save(*args, **kwargs)
# Cache the Cable on its two termination points
self.termination_a.cable = self
self.termination_a.save()
self.termination_b.cable = self
self.termination_b.save()
def get_path_endpoints(self):
"""
Traverse both ends of a cable path and return its connected endpoints. Note that one or both endpoints may be

View File

@ -23,9 +23,14 @@ def clear_virtualchassis_members(instance, **kwargs):
@receiver(post_save, sender=Cable)
def update_connected_endpoints(instance, **kwargs):
"""
When a Cable is saved, update its connected endpoints.
"""
# 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.
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
@ -36,11 +41,16 @@ def update_connected_endpoints(instance, **kwargs):
endpoint_b.save()
@receiver(post_delete, sender=Cable)
@receiver(pre_delete, sender=Cable)
def nullify_connected_endpoints(instance, **kwargs):
"""
When a Cable is deleted, nullify its connected endpoints.
"""
# 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
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