Skip to content

Commit 9a65e4f

Browse files
committed
Validate attribute type for type relation
Ensure the attribute type is validation when specifying a target servertype instread of crashing with an exception or HTTP 500.
1 parent 26591fc commit 9a65e4f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

serveradmin/serverdb/admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from serveradmin.serverdb.forms import (
1010
ServertypeAttributeAdminForm,
11-
ServertypeAdminForm,
11+
ServertypeAdminForm, AttributeAdminForm,
1212
)
1313
from serveradmin.serverdb.models import (
1414
Servertype,
@@ -68,6 +68,7 @@ class ServerAdmin(admin.ModelAdmin):
6868

6969

7070
class AttributeAdmin(admin.ModelAdmin):
71+
form = AttributeAdminForm
7172
list_display = [
7273
'attribute_id',
7374
'type',

serveradmin/serverdb/forms.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django import forms
22
from django.core.exceptions import ValidationError
33

4-
from serveradmin.serverdb.models import ServertypeAttribute
4+
from serveradmin.serverdb.models import ServertypeAttribute, Attribute
55

66

77
class ServertypeAdminForm(forms.ModelForm):
@@ -33,3 +33,14 @@ def clean(self):
3333
'ip_addr_type is null is not possible!')
3434

3535
super().clean()
36+
37+
38+
class AttributeAdminForm(forms.ModelForm):
39+
class Meta:
40+
model = Attribute
41+
fields = '__all__'
42+
43+
def clean(self):
44+
if self.cleaned_data['type'] != 'relation' and self.cleaned_data['target_servertype'] is not None:
45+
raise ValidationError('Attribute type must be relation when target servertype is selected!')
46+
super().clean()

0 commit comments

Comments
 (0)