Skip to content

Commit

Permalink
Restoring an object with "computed" attributes fails (#306)
Browse files Browse the repository at this point in the history
There are attributes we call "computed" which don't have stored values
in the database. Their values are based on other attributes of the same
object or related objects.

Setting values for these attributes when creating an object fails with
an exception and makes no sense so we exclude them here.

One might argue to not store them for deleted objects in the first place
but the values might still be of interest e.g. VMs that ran on a
hypervisor.
  • Loading branch information
kofrezo committed Apr 4, 2023
1 parent 7493574 commit dbf8333
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions serveradmin/serverdb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
ChangeCommit,
Server,
ServertypeAttribute,
Change, )
Change,
)
from serveradmin.serverdb.query_committer import CommitError, commit_query


Expand Down Expand Up @@ -154,12 +155,18 @@ def restore(request, change_id):
change = get_object_or_404(Change, pk=change_id)
server_object = change.change_json

# Remove related_via_attribute values they are implicit
related_attribute_ids = ServertypeAttribute.objects.filter(
servertype_id=server_object['servertype']).exclude(
related_via_attribute=None)
for attribute in related_attribute_ids:
server_object.pop(attribute.attribute_id)
# Remove related_via_attribute and "computed" attributes they are implicit
# and have no values in the database.
servertype_id = server_object['servertype']
exclude = ServertypeAttribute.objects.filter(servertype_id=servertype_id)
exclude = exclude.filter(
~Q(related_via_attribute=None) |
Q(attribute__type__in=['supernet', 'domain', 'reverse'])
).values_list('attribute_id', flat=True)

for attribute_id in exclude:
if attribute_id in server_object:
server_object.pop(attribute_id)

try:
commit = commit_query([server_object], user=request.user)
Expand Down

0 comments on commit dbf8333

Please sign in to comment.