Validate Location as well as Site scope

This commit is contained in:
Brian Tiemann 2025-01-11 12:46:38 -05:00
parent 1fbfea56c1
commit 30c41755aa

View File

@ -144,30 +144,26 @@ class ClusterAddDevicesForm(forms.Form):
def clean(self): def clean(self):
super().clean() super().clean()
# If the Cluster is assigned to a Site, all Devices must be assigned to that Site. # If the Cluster is assigned to a Site or Location, all Devices must be assigned to that same scope.
# This validation currently only supports Site scoping, but can be extended to Location etc. if needed.
if self.cluster.scope is not None: if self.cluster.scope is not None:
for device in self.cleaned_data.get('devices', []): for device in self.cleaned_data.get('devices', []):
device_scope = None for scope_field in ['site', 'location']:
scope_type = None device_scope = getattr(device, scope_field)
if ( if (
self.cluster.scope_type.model_class() == apps.get_model('dcim', 'site') self.cluster.scope_type.model_class() == apps.get_model('dcim', scope_field)
and device.site != self.cluster.scope and device_scope != self.cluster.scope
): ):
device_scope = device.site raise ValidationError({
scope_type = 'site' 'devices': _(
if device_scope: "{device} belongs to a different {scope_field} ({device_scope}) than the "
raise ValidationError({ "cluster ({cluster_scope})"
'devices': _( ).format(
"{device} belongs to a different {scope_type} ({device_scope}) than the " device=device,
"cluster ({cluster_scope})" scope_field=scope_field,
).format( device_scope=device_scope,
device=device, cluster_scope=self.cluster.scope
scope_type=scope_type, )
device_scope=device_scope, })
cluster_scope=self.cluster.scope
)
})
class ClusterRemoveDevicesForm(ConfirmationForm): class ClusterRemoveDevicesForm(ConfirmationForm):