From ca242f53fa4dddf14d2ecfffcf724e1f5867b25b Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 29 Aug 2023 10:31:48 -0700 Subject: [PATCH 1/3] 13599 fix cache counter --- netbox/utilities/counters.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/netbox/utilities/counters.py b/netbox/utilities/counters.py index ee6865ca2c7..6b5adffad33 100644 --- a/netbox/utilities/counters.py +++ b/netbox/utilities/counters.py @@ -27,7 +27,7 @@ def update_counter(model, pk, counter_name, value): # Signal handlers # -def post_save_receiver(sender, instance, **kwargs): +def post_save_receiver(sender, instance, created, **kwargs): """ Update counter fields on related objects when a TrackingModelMixin subclass is created or modified. """ @@ -40,7 +40,8 @@ def post_save_receiver(sender, instance, **kwargs): if old_pk is not None: update_counter(parent_model, old_pk, counter_name, -1) if new_pk is not None: - update_counter(parent_model, new_pk, counter_name, 1) + if old_pk or created: + update_counter(parent_model, new_pk, counter_name, 1) def post_delete_receiver(sender, instance, **kwargs): From 417b57171ac4257477bb510c517c6018b6b77031 Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 29 Aug 2023 10:52:02 -0700 Subject: [PATCH 2/3] 13599 update test --- netbox/utilities/tests/test_counters.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/netbox/utilities/tests/test_counters.py b/netbox/utilities/tests/test_counters.py index e9561c91b84..0c61c0890fb 100644 --- a/netbox/utilities/tests/test_counters.py +++ b/netbox/utilities/tests/test_counters.py @@ -29,13 +29,17 @@ def test_interface_count_creation(self): self.assertEqual(device1.interface_count, 2) self.assertEqual(device2.interface_count, 2) - Interface.objects.create(device=device1, name='Interface 5') + interface1 = Interface.objects.create(device=device1, name='Interface 5') Interface.objects.create(device=device2, name='Interface 6') device1.refresh_from_db() device2.refresh_from_db() self.assertEqual(device1.interface_count, 3) self.assertEqual(device2.interface_count, 3) + interface1.save() + device1.refresh_from_db() + self.assertEqual(device1.interface_count, 3) + def test_interface_count_deletion(self): """ When a tracked object (Interface) is deleted the tracking counter should be updated. From 7f643f9c58c0c2f4e3216cc22f5922e953112c2e Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 29 Aug 2023 15:07:08 -0400 Subject: [PATCH 3/3] Merge conditionals --- netbox/utilities/counters.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/netbox/utilities/counters.py b/netbox/utilities/counters.py index 6b5adffad33..6c1418dff37 100644 --- a/netbox/utilities/counters.py +++ b/netbox/utilities/counters.py @@ -39,9 +39,8 @@ def post_save_receiver(sender, instance, created, **kwargs): # Update the counters on the old and/or new parents as needed if old_pk is not None: update_counter(parent_model, old_pk, counter_name, -1) - if new_pk is not None: - if old_pk or created: - update_counter(parent_model, new_pk, counter_name, 1) + if new_pk is not None and (old_pk or created): + update_counter(parent_model, new_pk, counter_name, 1) def post_delete_receiver(sender, instance, **kwargs):