Commit 4b1ae74c authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Change deletion signal of ssh keys

parent 6be09f81
......@@ -3,7 +3,7 @@ from django.db import models
from django.conf import settings
from django.dispatch import receiver, Signal
from rest_framework.authtoken.models import Token
from django.db.models.signals import post_save
from django.db.models.signals import post_save, m2m_changed
from datetime import datetime
import requests
......@@ -186,9 +186,15 @@ class SSHPublicKey(models.Model):
editable=False,
)
# does not directly delete the key, but merely starts the deletion process
# the receiver 'delete_old_ssh_key' does the actual deletion afterwards
# does not directly delete the key if the key is deployed or withdrawn
# somewhere
# the receiver 'delete_withdrawn_ssh_key' does the actual deletion
def delete_key(self):
if (not self.deployments.exists()
and not self.withdrawn_deployments.exists()):
self.delete()
return
self.deleted = True
self.save()
......@@ -201,11 +207,15 @@ class SSHPublicKey(models.Model):
return self.name
@receiver(post_save, sender=SSHPublicKey)
def delete_old_ssh_key(
# finally delete the key if all the deployments are withdrawn
# and the withdrawal was seen by all clients
@receiver(m2m_changed, sender=SSHPublicKey)
def delete_withdrawn_ssh_key(
sender, instance=None, created=False, **kwargs):
if instance.deleted and len(instance.deployments.all()) == 0:
if (instance.deleted
and not instance.deployments.exists()
and not instance.withdrawn_deployments.exists()):
instance.delete()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment