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 ):
@@ -24,8 +25,8 @@ def clean(self):
24
25
# It makes no sense to add inet or supernet attributes to hosts of
25
26
# ip_addr_type null because they would have to be empty anyways.
26
27
inet_attribute = (
27
- self .cleaned_data ['attribute' ].type in ('inet' , 'supernet' ) and
28
- self .instance .servertype .ip_addr_type == 'null'
28
+ self .cleaned_data ['attribute' ].type in ('inet' , 'supernet' ) and
29
+ self .instance .servertype .ip_addr_type == 'null'
29
30
)
30
31
if inet_attribute :
31
32
raise ValidationError (
@@ -49,4 +50,12 @@ def clean(self):
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 ()
0 commit comments