mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
* Fix validation of site in Assign Device to Cluster flow * Validate Location as well as Site scope
This commit is contained in:
parent
c3efa2149c
commit
993d8f1480
@ -1,4 +1,5 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
|
from django.apps import apps
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
@ -143,19 +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.
|
||||||
if self.cluster.site 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', []):
|
||||||
if device.site != self.cluster.site:
|
for scope_field in ['site', 'location']:
|
||||||
raise ValidationError({
|
device_scope = getattr(device, scope_field)
|
||||||
'devices': _(
|
if (
|
||||||
"{device} belongs to a different site ({device_site}) than the cluster ({cluster_site})"
|
self.cluster.scope_type.model_class() == apps.get_model('dcim', scope_field)
|
||||||
).format(
|
and device_scope != self.cluster.scope
|
||||||
device=device,
|
):
|
||||||
device_site=device.site,
|
raise ValidationError({
|
||||||
cluster_site=self.cluster.site
|
'devices': _(
|
||||||
)
|
"{device} belongs to a different {scope_field} ({device_scope}) than the "
|
||||||
})
|
"cluster ({cluster_scope})"
|
||||||
|
).format(
|
||||||
|
device=device,
|
||||||
|
scope_field=scope_field,
|
||||||
|
device_scope=device_scope,
|
||||||
|
cluster_scope=self.cluster.scope
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class ClusterRemoveDevicesForm(ConfirmationForm):
|
class ClusterRemoveDevicesForm(ConfirmationForm):
|
||||||
|
Loading…
Reference in New Issue
Block a user