Skip to content
Open
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
15 changes: 12 additions & 3 deletions serveradmin/serverdb/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django import forms
from django.core.exceptions import ValidationError
from django.db.models.aggregates import Count

from serveradmin.serverdb.models import ServertypeAttribute, Attribute
from serveradmin.serverdb.models import ServertypeAttribute, Attribute, ServerStringAttribute


class ServertypeAdminForm(forms.ModelForm):
Expand Down Expand Up @@ -41,12 +42,20 @@ class Meta:
fields = '__all__'

def clean(self):
attr_type = self.cleaned_data.get('type') or self.instance.type # New or existing attribute ?
attr_type = self.cleaned_data.get('type') or self.instance.type # New or existing attribute ?

if attr_type != 'relation' and self.cleaned_data.get('target_servertype') is not None:
raise ValidationError('Attribute type must be relation when target servertype is selected!')

if attr_type == 'inet' and self.cleaned_data.get('multi') is True:
raise ValidationError('Multi attributes of type inet are not supported!')

super().clean()
if self.cleaned_data.get('multi') is False:
any_attrs_have_multiple_values = ServerStringAttribute.get_model(self.instance.type).objects.filter(
attribute_id=self.instance.attribute_id).values('server_id').annotate(
occurences=Count('server_id')).filter(occurences__gt=1).exists()
if any_attrs_have_multiple_values:
raise ValidationError(
'Refusing to make attribute type single because one ore more objects still have multiple values!')

super().clean()
Loading