Optimize background site/location updates

This commit is contained in:
Jeremy Stretch 2021-03-05 09:56:47 -05:00
parent 6afb7424a9
commit d40d3da7fc

View File

@ -37,7 +37,7 @@ def rebuild_paths(obj):
# #
# Site/rack/device assignment # Location/rack/device assignment
# #
@receiver(post_save, sender=Location) @receiver(post_save, sender=Location)
@ -47,18 +47,11 @@ def handle_location_site_change(instance, created, **kwargs):
object instead of calling update() on the QuerySet to ensure the proper change records get created for each. object instead of calling update() on the QuerySet to ensure the proper change records get created for each.
""" """
if not created: if not created:
for location in instance.get_children(): instance.get_descendants().update(site=instance.site)
location.site = instance.site locations = instance.get_descendants(include_self=True).values_list('pk', flat=True)
location.save() Rack.objects.filter(location__in=locations).update(site=instance.site)
for rack in Rack.objects.filter(location=instance).exclude(site=instance.site): Device.objects.filter(location__in=locations).update(site=instance.site)
rack.site = instance.site PowerPanel.objects.filter(location__in=locations).update(site=instance.site)
rack.save()
for device in Device.objects.filter(location=instance).exclude(site=instance.site):
device.site = instance.site
device.save()
for powerpanel in PowerPanel.objects.filter(location=instance).exclude(site=instance.site):
powerpanel.site = instance.site
powerpanel.save()
@receiver(post_save, sender=Rack) @receiver(post_save, sender=Rack)
@ -67,10 +60,7 @@ def handle_rack_site_change(instance, created, **kwargs):
Update child Devices if Site or Location assignment has changed. Update child Devices if Site or Location assignment has changed.
""" """
if not created: if not created:
for device in Device.objects.filter(rack=instance).exclude(site=instance.site, location=instance.location): Device.objects.filter(rack=instance).update(site=instance.site, location=instance.location)
device.site = instance.site
device.location = instance.location
device.save()
# #