Skip to content

Commit ba1b478

Browse files
authored
admin: fix ValidationError handling for LinkAdminForm (#375)
Add appropriate exclude parameter when invoking full_clean, analogous to the original ModelForm._post_clean.
1 parent 19f141d commit ba1b478

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

scionlab/admin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,14 @@ def _post_clean(self):
159159
self._update_errors(e)
160160

161161
# Call more stuff that ModelForm usually does.
162+
exclude = self._get_validation_exclusions()
162163
try:
163-
self.instance.full_clean(validate_unique=False)
164+
self.instance.full_clean(exclude=exclude, validate_unique=False)
164165
except ValidationError as e:
165166
self._update_errors(e)
166-
self.validate_unique()
167+
168+
if self._validate_unique:
169+
self.validate_unique()
167170

168171
def save(self, commit=True):
169172
# We override this hook too, because we have already saved our model at this point.

scionlab/tests/test_admin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ def test_create_link(self):
8383
self.assertTrue(edit_form.is_valid(), edit_form.errors)
8484
self.assertFalse(edit_form.has_changed(), edit_form.changed_data)
8585

86+
def test_create_link_error(self):
87+
# Create an illegal loop-back link
88+
as_a = AS.objects.first()
89+
form_data = dict(
90+
type=Link.PROVIDER,
91+
active=True,
92+
bandwidth=1234,
93+
mtu=2345,
94+
from_host=as_a.hosts.first().pk,
95+
from_public_port=50000,
96+
to_host=as_a.hosts.first().pk,
97+
to_public_port=50000,
98+
)
99+
form = LinkAdminForm(data=form_data)
100+
self.assertFalse(form.is_valid())
101+
self.assertIn('Loop AS-link', str(form.errors['__all__'][0]))
102+
86103
def test_render_edit(self):
87104
ases = AS.objects.iterator()
88105
as_a = next(ases)

0 commit comments

Comments
 (0)