From 0e60a5de4467491122fca8bf73e21792857a8807 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Mon, 21 Oct 2024 15:42:04 -0700 Subject: [PATCH] 9604 updates --- docs/models/circuits/circuittermination.md | 6 ++-- netbox/circuits/models/circuits.py | 33 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/docs/models/circuits/circuittermination.md b/docs/models/circuits/circuittermination.md index c6aa966d0..7454857be 100644 --- a/docs/models/circuits/circuittermination.md +++ b/docs/models/circuits/circuittermination.md @@ -21,13 +21,13 @@ Designates the termination as forming either the A or Z end of the circuit. If selected, the circuit termination will be considered "connected" even if no cable has been connected to it in NetBox. -### Site +### Scope -The [site](../dcim/site.md) with which this circuit termination is associated. Once created, a cable can be connected between the circuit termination and a device interface (or similar component). +The [region](../dcim/region.md), [site](../dcim/site.md) or [location](../dcim/location.md) with which this circuit termination is associated. Once created, a cable can be connected between the circuit termination and a device interface (or similar component). ### Provider Network -Circuits which do not connect to a site modeled by NetBox can instead be terminated to a [provider network](./providernetwork.md) representing an unknown network operated by a [provider](./provider.md). +Circuits which do not connect to a scope modeled by NetBox can instead be terminated to a [provider network](./providernetwork.md) representing an unknown network operated by a [provider](./provider.md). ### Port Speed diff --git a/netbox/circuits/models/circuits.py b/netbox/circuits/models/circuits.py index 28acafb59..3ffc9bde2 100644 --- a/netbox/circuits/models/circuits.py +++ b/netbox/circuits/models/circuits.py @@ -339,10 +339,35 @@ class CircuitTermination( super().clean() # Must define either site *or* provider network - if self.site is None and self.provider_network is None: - raise ValidationError(_("A circuit termination must attach to either a site or a provider network.")) - if self.site and self.provider_network: - raise ValidationError(_("A circuit termination cannot attach to both a site and a provider network.")) + if self.scope is None and self.provider_network is None: + raise ValidationError(_("A circuit termination must attach to either a scope or a provider network.")) + if self.scope and self.provider_network: + raise ValidationError(_("A circuit termination cannot attach to both a scope and a provider network.")) + + 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 def to_objectchange(self, action): objectchange = super().to_objectchange(action)