mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-31 04:46:26 -06:00
7699 move CachedScopeMixin
This commit is contained in:
parent
c75bfe147d
commit
c5005455f8
@ -1,6 +1,8 @@
|
|||||||
|
from django.apps import apps
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
'CachedScopeMixin',
|
||||||
'RenderConfigMixin',
|
'RenderConfigMixin',
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,3 +29,65 @@ class RenderConfigMixin(models.Model):
|
|||||||
return self.role.config_template
|
return self.role.config_template
|
||||||
if self.platform and self.platform.config_template:
|
if self.platform and self.platform.config_template:
|
||||||
return self.platform.config_template
|
return self.platform.config_template
|
||||||
|
|
||||||
|
|
||||||
|
class CachedScopeMixin(models.Model):
|
||||||
|
"""
|
||||||
|
Cached associations for scope to enable efficient filtering - must define scope and scope_type on model
|
||||||
|
"""
|
||||||
|
_location = models.ForeignKey(
|
||||||
|
to='dcim.Location',
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name='_%(class)ss',
|
||||||
|
blank=True,
|
||||||
|
null=True
|
||||||
|
)
|
||||||
|
_site = models.ForeignKey(
|
||||||
|
to='dcim.Site',
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name='_%(class)ss',
|
||||||
|
blank=True,
|
||||||
|
null=True
|
||||||
|
)
|
||||||
|
_region = models.ForeignKey(
|
||||||
|
to='dcim.Region',
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name='_%(class)ss',
|
||||||
|
blank=True,
|
||||||
|
null=True
|
||||||
|
)
|
||||||
|
_sitegroup = models.ForeignKey(
|
||||||
|
to='dcim.SiteGroup',
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name='_%(class)ss',
|
||||||
|
blank=True,
|
||||||
|
null=True
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
# Cache objects associated with the terminating object (for filtering)
|
||||||
|
self.cache_related_objects()
|
||||||
|
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
def cache_related_objects(self):
|
||||||
|
self._region = self._sitegroup = self._site = self._location = None
|
||||||
|
if self.scope_type:
|
||||||
|
scope_type = self.scope_type.model_class()
|
||||||
|
if scope_type == apps.get_model('dcim', 'region'):
|
||||||
|
self._region = self.scope
|
||||||
|
elif scope_type == apps.get_model('dcim', 'sitegroup'):
|
||||||
|
self._sitegroup = self.scope
|
||||||
|
elif scope_type == apps.get_model('dcim', 'site'):
|
||||||
|
self._region = self.scope.region
|
||||||
|
self._sitegroup = self.scope.group
|
||||||
|
self._site = self.scope
|
||||||
|
elif scope_type == apps.get_model('dcim', 'location'):
|
||||||
|
self._region = self.scope.site.region
|
||||||
|
self._sitegroup = self.scope.site.group
|
||||||
|
self._site = self.scope.site
|
||||||
|
self._location = self.scope
|
||||||
|
cache_related_objects.alters_data = True
|
||||||
|
@ -2,7 +2,6 @@ import json
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
|
|
||||||
from django.apps import apps
|
|
||||||
from django.contrib.contenttypes.fields import GenericRelation
|
from django.contrib.contenttypes.fields import GenericRelation
|
||||||
from django.core.validators import ValidationError
|
from django.core.validators import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@ -25,7 +24,6 @@ from utilities.views import register_model_view
|
|||||||
__all__ = (
|
__all__ = (
|
||||||
'BookmarksMixin',
|
'BookmarksMixin',
|
||||||
'ChangeLoggingMixin',
|
'ChangeLoggingMixin',
|
||||||
'CachedScopeMixin',
|
|
||||||
'CloningMixin',
|
'CloningMixin',
|
||||||
'ContactsMixin',
|
'ContactsMixin',
|
||||||
'CustomFieldsMixin',
|
'CustomFieldsMixin',
|
||||||
@ -582,68 +580,6 @@ class SyncedDataMixin(models.Model):
|
|||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
class CachedScopeMixin(models.Model):
|
|
||||||
"""
|
|
||||||
Cached associations for scope to enable efficient filtering - must define scope and scope_type on model
|
|
||||||
"""
|
|
||||||
_location = models.ForeignKey(
|
|
||||||
to='dcim.Location',
|
|
||||||
on_delete=models.CASCADE,
|
|
||||||
related_name='_%(class)ss',
|
|
||||||
blank=True,
|
|
||||||
null=True
|
|
||||||
)
|
|
||||||
_site = models.ForeignKey(
|
|
||||||
to='dcim.Site',
|
|
||||||
on_delete=models.CASCADE,
|
|
||||||
related_name='_%(class)ss',
|
|
||||||
blank=True,
|
|
||||||
null=True
|
|
||||||
)
|
|
||||||
_region = models.ForeignKey(
|
|
||||||
to='dcim.Region',
|
|
||||||
on_delete=models.CASCADE,
|
|
||||||
related_name='_%(class)ss',
|
|
||||||
blank=True,
|
|
||||||
null=True
|
|
||||||
)
|
|
||||||
_sitegroup = models.ForeignKey(
|
|
||||||
to='dcim.SiteGroup',
|
|
||||||
on_delete=models.CASCADE,
|
|
||||||
related_name='_%(class)ss',
|
|
||||||
blank=True,
|
|
||||||
null=True
|
|
||||||
)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
abstract = True
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
|
||||||
# Cache objects associated with the terminating object (for filtering)
|
|
||||||
self.cache_related_objects()
|
|
||||||
|
|
||||||
super().save(*args, **kwargs)
|
|
||||||
|
|
||||||
def cache_related_objects(self):
|
|
||||||
self._region = self._sitegroup = self._site = self._location = None
|
|
||||||
if self.scope_type:
|
|
||||||
scope_type = self.scope_type.model_class()
|
|
||||||
if scope_type == apps.get_model('dcim', 'region'):
|
|
||||||
self._region = self.scope
|
|
||||||
elif scope_type == apps.get_model('dcim', 'sitegroup'):
|
|
||||||
self._sitegroup = self.scope
|
|
||||||
elif scope_type == apps.get_model('dcim', 'site'):
|
|
||||||
self._region = self.scope.region
|
|
||||||
self._sitegroup = self.scope.group
|
|
||||||
self._site = self.scope
|
|
||||||
elif scope_type == apps.get_model('dcim', 'location'):
|
|
||||||
self._region = self.scope.site.region
|
|
||||||
self._sitegroup = self.scope.site.group
|
|
||||||
self._site = self.scope.site
|
|
||||||
self._location = self.scope
|
|
||||||
cache_related_objects.alters_data = True
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Feature registration
|
# Feature registration
|
||||||
#
|
#
|
||||||
|
@ -5,8 +5,9 @@ from django.db import models
|
|||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from dcim.models import Device
|
from dcim.models import Device
|
||||||
|
from dcim.models.mixins import CachedScopeMixin
|
||||||
from netbox.models import OrganizationalModel, PrimaryModel
|
from netbox.models import OrganizationalModel, PrimaryModel
|
||||||
from netbox.models.features import CachedScopeMixin, ContactsMixin
|
from netbox.models.features import ContactsMixin
|
||||||
from virtualization.choices import *
|
from virtualization.choices import *
|
||||||
from virtualization.constants import CLUSTER_SCOPE_TYPES
|
from virtualization.constants import CLUSTER_SCOPE_TYPES
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user