Add support for tracing split paths

This commit is contained in:
Jeremy Stretch
2020-11-16 15:49:07 -05:00
parent ba2ff0acb8
commit c559775135
6 changed files with 137 additions and 97 deletions

View File

@@ -1,7 +1,5 @@
from django.contrib.contenttypes.models import ContentType
from .choices import CableStatusChoices
def compile_path_node(ct_id, object_id):
return f'{ct_id}:{object_id}'
@@ -21,53 +19,10 @@ def object_to_path_node(obj):
return compile_path_node(ct.pk, obj.pk)
def trace_path(node):
from .models import FrontPort, RearPort
destination = None
path = []
position_stack = []
is_active = True
if node is None or node.cable is None:
return [], None, False
while node.cable is not None:
if node.cable.status != CableStatusChoices.STATUS_CONNECTED:
is_active = False
# Follow the cable to its far-end termination
path.append(object_to_path_node(node.cable))
peer_termination = node.get_cable_peer()
# Follow a FrontPort to its corresponding RearPort
if isinstance(peer_termination, FrontPort):
path.append(object_to_path_node(peer_termination))
node = peer_termination.rear_port
if node.positions > 1:
position_stack.append(peer_termination.rear_port_position)
path.append(object_to_path_node(node))
# Follow a RearPort to its corresponding FrontPort
elif isinstance(peer_termination, RearPort):
path.append(object_to_path_node(peer_termination))
if peer_termination.positions == 1:
node = FrontPort.objects.get(rear_port=peer_termination, rear_port_position=1)
path.append(object_to_path_node(node))
elif position_stack:
position = position_stack.pop()
node = FrontPort.objects.get(rear_port=peer_termination, rear_port_position=position)
path.append(object_to_path_node(node))
else:
# No position indicated: path has split, so we stop at the RearPort
break
# Anything else marks the end of the path
else:
destination = peer_termination
break
if destination is None:
is_active = False
return path, destination, is_active
def path_node_to_object(repr):
"""
Given the string representation of a path node, return the corresponding instance.
"""
ct_id, object_id = decompile_path_node(repr)
ct = ContentType.objects.get_for_id(ct_id)
return ct.model_class().objects.get(pk=object_id)