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