mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-08 00:28:16 -06:00
Optimized queryset condition with .exists
This commit is contained in:
parent
45c09c7e6e
commit
7791ca9313
@ -549,14 +549,14 @@ class PowerOutlet(CableTermination, ComponentModel):
|
|||||||
|
|
||||||
def calculate_downstream_powerports(self):
|
def calculate_downstream_powerports(self):
|
||||||
"""
|
"""
|
||||||
Return a queryset of the downstream power ports.
|
Return a queryset of the power ports drawing power from this outlet.
|
||||||
"""
|
"""
|
||||||
downstream_powerports = PowerPort.objects.none()
|
downstream_powerports = PowerPort.objects.none()
|
||||||
|
|
||||||
if hasattr(self, 'connected_endpoint'):
|
if hasattr(self, 'connected_endpoint'):
|
||||||
next_powerports = PowerPort.objects.filter(pk=self.connected_endpoint.pk)
|
next_powerports = PowerPort.objects.filter(pk=self.connected_endpoint.pk)
|
||||||
|
|
||||||
while next_powerports:
|
while next_powerports.exists():
|
||||||
downstream_powerports |= next_powerports
|
downstream_powerports |= next_powerports
|
||||||
|
|
||||||
# Prevent loops by excluding those already matched
|
# Prevent loops by excluding those already matched
|
||||||
@ -570,14 +570,14 @@ class PowerOutlet(CableTermination, ComponentModel):
|
|||||||
|
|
||||||
def calculate_upstream_poweroutlets(self):
|
def calculate_upstream_poweroutlets(self):
|
||||||
"""
|
"""
|
||||||
Return a queryset of the upstream power outlets.
|
Return a queryset of the power outlets supplying power to this outlet.
|
||||||
"""
|
"""
|
||||||
upstream_poweroutlets = PowerOutlet.objects.none()
|
upstream_poweroutlets = PowerOutlet.objects.none()
|
||||||
|
|
||||||
if self.power_port and self.power_port._connected_poweroutlet:
|
if self.power_port and self.power_port._connected_poweroutlet:
|
||||||
next_poweroutlets = PowerOutlet.objects.filter(pk=self.power_port._connected_poweroutlet.pk)
|
next_poweroutlets = PowerOutlet.objects.filter(pk=self.power_port._connected_poweroutlet.pk)
|
||||||
|
|
||||||
while next_poweroutlets:
|
while next_poweroutlets.exists():
|
||||||
upstream_poweroutlets |= next_poweroutlets
|
upstream_poweroutlets |= next_poweroutlets
|
||||||
|
|
||||||
# Prevent loops by excluding those already matched
|
# Prevent loops by excluding those already matched
|
||||||
|
@ -658,7 +658,7 @@ class PowerCalculationTestCase(TestCase):
|
|||||||
"""
|
"""
|
||||||
stats = self.power_port1.get_power_draw()
|
stats = self.power_port1.get_power_draw()
|
||||||
|
|
||||||
# Global stats: all devices 2
|
# Global stats: all devices/feeds
|
||||||
self.assertEqual(stats['maximum'], 25 + 7 + 4)
|
self.assertEqual(stats['maximum'], 25 + 7 + 4)
|
||||||
self.assertEqual(stats['allocated'], 10 + 5 + 3)
|
self.assertEqual(stats['allocated'], 10 + 5 + 3)
|
||||||
self.assertEqual(stats['outlet_count'], 2)
|
self.assertEqual(stats['outlet_count'], 2)
|
||||||
|
Loading…
Reference in New Issue
Block a user