utils.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.contrib.contenttypes.models import ContentType
  2. from .models import FrontPort, RearPort
  3. def object_to_path_node(obj):
  4. return f'{obj._meta.model_name}:{obj.pk}'
  5. def objects_to_path(*obj_list):
  6. return [object_to_path_node(obj) for obj in obj_list]
  7. def path_node_to_object(repr):
  8. model_name, object_id = repr.split(':')
  9. model_class = ContentType.objects.get(model=model_name).model_class()
  10. return model_class.objects.get(pk=int(object_id))
  11. def trace_paths(node):
  12. destination = None
  13. path = []
  14. position_stack = []
  15. if node.cable is None:
  16. return []
  17. while node.cable is not None:
  18. # Follow the cable to its far-end termination
  19. path.append(object_to_path_node(node.cable))
  20. peer_termination = node.get_cable_peer()
  21. # Follow a FrontPort to its corresponding RearPort
  22. if isinstance(peer_termination, FrontPort):
  23. path.append(object_to_path_node(peer_termination))
  24. position_stack.append(peer_termination.rear_port_position)
  25. node = peer_termination.rear_port
  26. path.append(object_to_path_node(node))
  27. # Follow a RearPort to its corresponding FrontPort
  28. elif isinstance(peer_termination, RearPort):
  29. path.append(object_to_path_node(peer_termination))
  30. if position_stack:
  31. position = position_stack.pop()
  32. node = FrontPort.objects.get(rear_port=peer_termination, rear_port_position=position)
  33. path.append(object_to_path_node(node))
  34. else:
  35. # No position indicated, so we have to trace _all_ peer FrontPorts
  36. paths = []
  37. for frontport in FrontPort.objects.filter(rear_port=peer_termination):
  38. branches = trace_paths(frontport)
  39. if branches:
  40. for branch, destination in branches:
  41. paths.append(([*path, object_to_path_node(frontport), *branch], destination))
  42. else:
  43. paths.append(([*path, object_to_path_node(frontport)], None))
  44. return paths
  45. # Anything else marks the end of the path
  46. else:
  47. destination = peer_termination
  48. break
  49. return [(path, destination)]