Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #15490: CustomValidator support for accessing related object attribute via dotted path #15511

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.test import TestCase, override_settings

from dcim.choices import SiteStatusChoices
from dcim.models import Site
from dcim.models import Site, Region
from extras.validators import CustomValidator
from ipam.models import ASN, RIR
from users.models import User
Expand Down Expand Up @@ -82,6 +82,13 @@ def validate(self, instance):
})


region_validator = CustomValidator({
'region.name': {
'eq': 'Bar',
}
})


request_validator = CustomValidator({
'request.user.username': {
'eq': 'Bob'
Expand Down Expand Up @@ -154,6 +161,20 @@ def test_prohibited(self):
def test_valid(self):
Site(name='abcdef123', slug='abcdef123').clean()

@override_settings(CUSTOM_VALIDATORS={'dcim.site': [region_validator]})
def test_valid(self):
region1 = Region(name='Foo', slug='foo')
region1.save()
region2 = Region(name='Bar', slug='bar')
region2.save()

# Invalid region
with self.assertRaises(ValidationError):
Site(name='abcdef123', slug='abcdef123', region=region1).clean()

# Valid region
Site(name='abcdef123', slug='abcdef123', region=region2).clean()

@override_settings(CUSTOM_VALIDATORS={'dcim.site': [custom_validator]})
def test_custom_invalid(self):
with self.assertRaises(ValidationError):
Expand Down Expand Up @@ -207,7 +228,7 @@ def test_plain_data(self):
@override_settings(
CUSTOM_VALIDATORS={
'dcim.site': (
'extras.tests.test_customvalidation.MyValidator',
'extras.tests.test_customvalidators.MyValidator',
)
}
)
Expand Down Expand Up @@ -254,7 +275,7 @@ def test_plain_data(self):
@override_settings(
PROTECTION_RULES={
'dcim.site': (
'extras.tests.test_customvalidation.MyValidator',
'extras.tests.test_customvalidators.MyValidator',
)
}
)
Expand Down
6 changes: 3 additions & 3 deletions netbox/extras/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ def _get_instance_attr(instance, name):
return []

# Raise a ValidationError for unknown attributes
if not hasattr(instance, name):
try:
return operator.attrgetter(name)(instance)
except AttributeError:
raise ValidationError(_('Invalid attribute "{name}" for {model}').format(
name=name,
model=instance.__class__.__name__
))

return getattr(instance, name)

def get_validator(self, descriptor, value):
"""
Instantiate and return the appropriate validator based on the descriptor given. For
Expand Down
Loading