Commit 580739c0 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Change the sending of deployment changes

parent 70836be3
from django.dispatch import receiver
from django.db.models.signals import post_save
from .. import models
from .serializers import DeploymentSerializer
import pika
......@@ -11,7 +10,7 @@ class Deployments:
pass
@receiver(post_save, sender=models.Deployment)
@receiver(models.deployment_change, sender=models.Deployment)
def publish_deployment(sender, instance=None, created=False, **kwargs):
if instance is None:
return
......@@ -27,14 +26,11 @@ def publish_deployment(sender, instance=None, created=False, **kwargs):
message = json.dumps(DeploymentSerializer(instance).data)
# we only use the service as routing key
routing_keys = ['service.' + instance.service.name]
routing_key = 'service.' + instance.service.name
channel.basic_publish(exchange=exchange_name,
routing_key=routing_key,
body=message)
for routing_key in routing_keys:
channel.basic_publish(exchange=exchange_name,
routing_key=routing_key,
body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
from django.conf import settings
from django.dispatch import receiver
from django.dispatch import receiver, Signal
from rest_framework.authtoken.models import Token
from django.db.models.signals import post_save
from datetime import datetime
......@@ -9,6 +9,8 @@ from datetime import datetime
import requests
from requests.auth import HTTPBasicAuth
deployment_change = Signal(providing_args=['instance'])
class User(AbstractUser):
TYPE_CHOICES = (
......@@ -234,6 +236,7 @@ class Deployment(models.Model):
if key in self.ssh_keys_to_withdraw.all():
self.ssh_keys_to_withdraw.remove(key)
self.save()
self.send_change()
def withdraw_key(self, key):
# key state: -> (4)
......@@ -242,6 +245,7 @@ class Deployment(models.Model):
# keys which are to be withdrawn by the clients
self.ssh_keys_to_withdraw.add(key)
self.save()
self.send_change()
def client_got_deployment(self):
# the client has withdrawn the keys so we can empty the list
......@@ -249,5 +253,14 @@ class Deployment(models.Model):
# TODO: does the deletion of the ssh_keys actually occur?
self.save()
def send_change(self):
deployment_change.send(sender=self.__class__, instance=self)
def __str__(self):
return '{}@{}'.format(self.user, self.service)
@receiver(post_save, sender=Deployment)
def publish_deployment_creation(
sender, instance=None, created=False, **kwargs):
instance.send_change()
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