Skip to content

Commit

Permalink
Make target servertype optional for relation attributes (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
kofrezo authored Nov 10, 2023
1 parent 04350bd commit 78cbe1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.23 on 2023-11-03 08:04

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('serverdb', '0015_attribute_history_field'),
]

operations = [
migrations.RunSQL(
"ALTER TABLE attribute DROP CONSTRAINT IF EXISTS attribute_target_servertype_id_check"
),
# This is the same as before but without a check for type relation.
migrations.RunSQL(
"ALTER TABLE attribute ADD CONSTRAINT attribute_target_servertype_id_check "
"CHECK((type IN ('domain', 'supernet', 'relation')) = (target_servertype_id IS NOT NULL OR type = 'relation'))"
),
]
5 changes: 2 additions & 3 deletions serveradmin/serverdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,16 +591,15 @@ class Meta:
index_together = [['attribute', 'value']]

def save_value(self, value):
target_servertype = self.attribute.target_servertype

try:
target_server = Server.objects.get(hostname=value)
except Server.DoesNotExist:
raise ValidationError(
'No server with hostname "{0}" exist.'.format(value)
)

if target_server.servertype != target_servertype:
target_servertype = self.attribute.target_servertype
if target_servertype and target_server.servertype != target_servertype:
raise ValidationError(
'Attribute "{0}" has to be from servertype "{1}".'
.format(self.attribute, self.attribute.target_servertype)
Expand Down

0 comments on commit 78cbe1f

Please sign in to comment.