File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 1
1
from django import forms
2
2
from django .core .exceptions import ValidationError
3
+ from django .db .models .aggregates import Count
3
4
4
- from serveradmin .serverdb .models import ServertypeAttribute , Attribute
5
+ from serveradmin .serverdb .models import ServertypeAttribute , Attribute , ServerStringAttribute
5
6
6
7
7
8
class ServertypeAdminForm (forms .ModelForm ):
@@ -41,12 +42,20 @@ class Meta:
41
42
fields = '__all__'
42
43
43
44
def clean (self ):
44
- attr_type = self .cleaned_data .get ('type' ) or self .instance .type # New or existing attribute ?
45
+ attr_type = self .cleaned_data .get ('type' ) or self .instance .type # New or existing attribute ?
45
46
46
47
if attr_type != 'relation' and self .cleaned_data .get ('target_servertype' ) is not None :
47
48
raise ValidationError ('Attribute type must be relation when target servertype is selected!' )
48
49
49
50
if attr_type == 'inet' and self .cleaned_data .get ('multi' ) is True :
50
51
raise ValidationError ('Multi attributes of type inet are not supported!' )
51
52
52
- super ().clean ()
53
+ if self .cleaned_data .get ('multi' ) is False :
54
+ any_attrs_have_multiple_values = ServerStringAttribute .get_model (self .instance .type ).objects .filter (
55
+ attribute_id = self .instance .attribute_id ).values ('server_id' ).annotate (
56
+ occurences = Count ('server_id' )).filter (occurences__gt = 1 ).exists ()
57
+ if any_attrs_have_multiple_values :
58
+ raise ValidationError (
59
+ 'Refusing to make attribute type single because one ore more objects still have multiple values!' )
60
+
61
+ super ().clean ()
You can’t perform that action at this time.
0 commit comments