From 9c3e7f2c5d0f11dcd8227c7d4eb2655f3431bbe7 Mon Sep 17 00:00:00 2001 From: Andrey Tikhonov <17@itishka.org> Date: Mon, 24 Feb 2025 15:38:19 +0100 Subject: [PATCH] Cleanup --- netbox/dcim/api/views.py | 14 ++++------- netbox/dcim/models/cables.py | 45 ------------------------------------ 2 files changed, 5 insertions(+), 54 deletions(-) diff --git a/netbox/dcim/api/views.py b/netbox/dcim/api/views.py index cfdd5d209..b74b4bb36 100644 --- a/netbox/dcim/api/views.py +++ b/netbox/dcim/api/views.py @@ -434,21 +434,17 @@ class PowerOutletViewSet(PathEndpointMixin, NetBoxModelViewSet): class InterfaceViewSet(PathEndpointMixin, NetBoxModelViewSet): queryset = Interface.objects.prefetch_related( - # '_path', - # 'cable__terminations', GenericPrefetch( "cable__terminations__termination", [ Interface.objects.select_related("device", "cable"), ], ), - Prefetch( - "_path", - CablePath.objects.prefetch_related( - GenericPrefetch("path_objects", [ - Interface.objects.select_related("device"), - ]), - ) + GenericPrefetch( + "_path__path_objects", + [ + Interface.objects.select_related("device", "cable"), + ], ), 'l2vpn_terminations', # Referenced by InterfaceSerializer.l2vpn_termination 'ip_addresses', # Referenced by Interface.count_ipaddresses() diff --git a/netbox/dcim/models/cables.py b/netbox/dcim/models/cables.py index 7ec94ac44..fbbb99998 100644 --- a/netbox/dcim/models/cables.py +++ b/netbox/dcim/models/cables.py @@ -490,15 +490,6 @@ class CablePath(models.Model): ct_id, _ = decompile_path_node(self.path[-1][0]) return ObjectType.objects.get_for_id(ct_id) - @property - def path_objects_old(self): - """ - Cache and return the complete path as lists of objects, derived from their annotation within the path. - """ - if not hasattr(self, '_path_objects'): - self._path_objects = self._get_path() - return self._path_objects - @property def _path_decompiled(self): res = [] @@ -747,42 +738,6 @@ class CablePath(models.Model): self.delete() retrace.alters_data = True - def _get_path(self): - """ - Return the path as a list of prefetched objects. - """ - # Compile a list of IDs to prefetch for each type of model in the path - to_prefetch = defaultdict(list) - for node in self._nodes: - ct_id, object_id = decompile_path_node(node) - to_prefetch[ct_id].append(object_id) - - # Prefetch path objects using one query per model type. Prefetch related devices where appropriate. - prefetched = {} - for ct_id, object_ids in to_prefetch.items(): - model_class = ObjectType.objects.get_for_id(ct_id).model_class() - queryset = model_class.objects.filter(pk__in=object_ids) - if hasattr(model_class, 'device'): - queryset = queryset.prefetch_related('device') - prefetched[ct_id] = { - obj.id: obj for obj in queryset - } - - # Replicate the path using the prefetched objects. - path = [] - for step in self.path: - nodes = [] - for node in step: - ct_id, object_id = decompile_path_node(node) - try: - nodes.append(prefetched[ct_id][object_id]) - except KeyError: - # Ignore stale (deleted) object IDs - pass - path.append(nodes) - - return path - def get_cable_ids(self): """ Return all Cable IDs within the path.