Optimized queryset condition with .exists

This commit is contained in:
Saria Hajjar 2020-02-20 18:39:14 +00:00
parent 45c09c7e6e
commit 7791ca9313
2 changed files with 5 additions and 5 deletions

View File

@ -549,14 +549,14 @@ class PowerOutlet(CableTermination, ComponentModel):
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()
if hasattr(self, 'connected_endpoint'):
next_powerports = PowerPort.objects.filter(pk=self.connected_endpoint.pk)
while next_powerports:
while next_powerports.exists():
downstream_powerports |= next_powerports
# Prevent loops by excluding those already matched
@ -570,14 +570,14 @@ class PowerOutlet(CableTermination, ComponentModel):
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()
if self.power_port and self.power_port._connected_poweroutlet:
next_poweroutlets = PowerOutlet.objects.filter(pk=self.power_port._connected_poweroutlet.pk)
while next_poweroutlets:
while next_poweroutlets.exists():
upstream_poweroutlets |= next_poweroutlets
# Prevent loops by excluding those already matched

View File

@ -658,7 +658,7 @@ class PowerCalculationTestCase(TestCase):
"""
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['allocated'], 10 + 5 + 3)
self.assertEqual(stats['outlet_count'], 2)