Fixes #19851: Fix WirelessLANImportForm has no field scope, improve validation (#20273)

This commit is contained in:
Jason Novinger 2025-09-05 21:59:38 +00:00 committed by GitHub
parent 94faf58c27
commit 026737b62b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 7 deletions

View File

@ -1,6 +1,6 @@
from django import forms from django import forms
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from dcim.constants import LOCATION_SCOPE_TYPES from dcim.constants import LOCATION_SCOPE_TYPES
@ -48,8 +48,17 @@ class ScopedForm(forms.Form):
def clean(self): def clean(self):
super().clean() super().clean()
scope = self.cleaned_data.get('scope')
scope_type = self.cleaned_data.get('scope_type')
if scope_type and not scope:
raise ValidationError({
'scope': _(
"Please select a {scope_type}."
).format(scope_type=scope_type.model_class()._meta.model_name)
})
# Assign the selected scope (if any) # Assign the selected scope (if any)
self.instance.scope = self.cleaned_data.get('scope') self.instance.scope = scope
def _set_scoped_values(self): def _set_scoped_values(self):
if scope_type_id := get_field_value(self, 'scope_type'): if scope_type_id := get_field_value(self, 'scope_type'):
@ -107,3 +116,15 @@ class ScopedImportForm(forms.Form):
required=False, required=False,
label=_('Scope type (app & model)') label=_('Scope type (app & model)')
) )
def clean(self):
super().clean()
scope_id = self.cleaned_data.get('scope_id')
scope_type = self.cleaned_data.get('scope_type')
if scope_type and not scope_id:
raise ValidationError({
'scope_id': _(
"Please select a {scope_type}."
).format(scope_type=scope_type.model_class()._meta.model_name)
})

View File

@ -87,11 +87,9 @@ class CachedScopeMixin(models.Model):
def clean(self): def clean(self):
if self.scope_type and not (self.scope or self.scope_id): if self.scope_type and not (self.scope or self.scope_id):
scope_type = self.scope_type.model_class() scope_type = self.scope_type.model_class()
raise ValidationError({ raise ValidationError(
'scope': _( _("Please select a {scope_type}.").format(scope_type=scope_type._meta.model_name)
"Please select a {scope_type}." )
).format(scope_type=scope_type._meta.model_name)
})
super().clean() super().clean()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):