6347 fix tracking mixin to tracked-classes

This commit is contained in:
Arthur 2023-07-19 22:28:59 +07:00
parent 8b8e9a44e5
commit 4156fb31b8
5 changed files with 15 additions and 20 deletions

View File

@ -9,10 +9,7 @@ class DCIMConfig(AppConfig):
def ready(self): def ready(self):
from . import signals, search from . import signals, search
from .models import ( from .models import CableTermination, Device
CableTermination, ConsolePort, ConsoleServerPort, DeviceBay, FrontPort,
Interface, InventoryItem, PowerOutlet, PowerPort, RearPort,
)
from utilities.counter import connect_counters from utilities.counter import connect_counters
@ -30,4 +27,4 @@ class DCIMConfig(AppConfig):
'_site': 'site', '_site': 'site',
}) })
connect_counters(self) connect_counters([Device,])

View File

@ -470,7 +470,7 @@ class PowerOutlet(ModularComponentModel, CabledObjectModel, PathEndpoint, Tracki
# Interfaces # Interfaces
# #
class BaseInterface(models.Model, TrackingModelMixin): class BaseInterface(models.Model):
""" """
Abstract base class for fields shared by dcim.Interface and virtualization.VMInterface. Abstract base class for fields shared by dcim.Interface and virtualization.VMInterface.
""" """
@ -538,7 +538,7 @@ class BaseInterface(models.Model, TrackingModelMixin):
return self.fhrp_group_assignments.count() return self.fhrp_group_assignments.count()
class Interface(ModularComponentModel, BaseInterface, CabledObjectModel, PathEndpoint): class Interface(ModularComponentModel, BaseInterface, CabledObjectModel, PathEndpoint, TrackingModelMixin):
""" """
A network interface within a Device. A physical Interface can connect to exactly one other Interface. A network interface within a Device. A physical Interface can connect to exactly one other Interface.
""" """

View File

@ -78,13 +78,11 @@ class Counter:
return self.set_counter_field(parent_id, F(self.counter_name) + amount) return self.set_counter_field(parent_id, F(self.counter_name) + amount)
def connect_counters(app_config): def connect_counters(models):
models = app_config.get_models()
for model in models: for model in models:
if issubclass(model, TrackingModelMixin): fields = model._meta.get_fields()
fields = model._meta.get_fields() for field in fields:
for field in fields: if type(field) is CounterCacheField:
if type(field) is CounterCacheField: to_model = apps.get_model(field.to_model_name)
to_model = apps.get_model(field.to_model_name) to_field = getattr(to_model, field.to_field_name)
to_field = getattr(to_model, field.to_field_name) Counter(field.name, to_field)
Counter(field.name, to_field)

View File

@ -26,7 +26,7 @@ class CountersTest(TestCase):
def test_interface_count_addition(self): def test_interface_count_addition(self):
""" """
When a new Cable is created, it must be cached on either termination point. When a tracked object (Interface) is added the tracking counter should be updated.
""" """
device1 = Device.objects.get(name='TestDevice1') device1 = Device.objects.get(name='TestDevice1')
device2 = Device.objects.get(name='TestDevice2') device2 = Device.objects.get(name='TestDevice2')
@ -43,8 +43,7 @@ class CountersTest(TestCase):
def test_interface_count_deletion(self): def test_interface_count_deletion(self):
""" """
When a Cable is deleted, the `cable` field on its termination points must be nullified. The str() method When a tracked object (Interface) is deleted the tracking counter should be updated.
should still return the PK of the string even after being nullified.
""" """
device1 = Device.objects.get(name='TestDevice1') device1 = Device.objects.get(name='TestDevice1')
device2 = Device.objects.get(name='TestDevice2') device2 = Device.objects.get(name='TestDevice2')

View File

@ -6,6 +6,7 @@ class VirtualizationConfig(AppConfig):
def ready(self): def ready(self):
from . import search from . import search
from .models import VirtualMachine
from utilities.counter import connect_counters from utilities.counter import connect_counters
connect_counters(self) connect_counters([VirtualMachine,])