mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-28 11:26:26 -06:00
Validate Location as well as Site scope
This commit is contained in:
parent
1fbfea56c1
commit
30c41755aa
@ -144,26 +144,22 @@ class ClusterAddDevicesForm(forms.Form):
|
||||
def clean(self):
|
||||
super().clean()
|
||||
|
||||
# If the Cluster is assigned to a Site, all Devices must be assigned to that Site.
|
||||
# This validation currently only supports Site scoping, but can be extended to Location etc. if needed.
|
||||
# If the Cluster is assigned to a Site or Location, all Devices must be assigned to that same scope.
|
||||
if self.cluster.scope is not None:
|
||||
for device in self.cleaned_data.get('devices', []):
|
||||
device_scope = None
|
||||
scope_type = None
|
||||
for scope_field in ['site', 'location']:
|
||||
device_scope = getattr(device, scope_field)
|
||||
if (
|
||||
self.cluster.scope_type.model_class() == apps.get_model('dcim', 'site')
|
||||
and device.site != self.cluster.scope
|
||||
self.cluster.scope_type.model_class() == apps.get_model('dcim', scope_field)
|
||||
and device_scope != self.cluster.scope
|
||||
):
|
||||
device_scope = device.site
|
||||
scope_type = 'site'
|
||||
if device_scope:
|
||||
raise ValidationError({
|
||||
'devices': _(
|
||||
"{device} belongs to a different {scope_type} ({device_scope}) than the "
|
||||
"{device} belongs to a different {scope_field} ({device_scope}) than the "
|
||||
"cluster ({cluster_scope})"
|
||||
).format(
|
||||
device=device,
|
||||
scope_type=scope_type,
|
||||
scope_field=scope_field,
|
||||
device_scope=device_scope,
|
||||
cluster_scope=self.cluster.scope
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user