Commit c0173efa authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Change the API a little bit

parent 2991bcf8
# we don't need to deserialize, so we do not implement the abstract methods
# pylint: disable=abstract-method
from django_mysql.models import JSONField
from rest_framework import serializers
from .. import models, serializers as backend_serializers
......@@ -13,10 +14,11 @@ class ServiceSerializer(serializers.ModelSerializer):
class UserSerializer(serializers.ModelSerializer):
groups = backend_serializers.GroupSerializer(many=True)
userinfo = JSONField()
class Meta:
model = models.User
fields = ['sub', 'email', 'groups']
fields = ['email', 'groups', 'userinfo']
class DeploymentSerializer(serializers.ModelSerializer):
......
......@@ -2,7 +2,7 @@ from django.conf.urls import url
from . import views
URLPATTERNS = [
url(r'^deployments/', views.DeploymentsView.as_view()),
url(r'^config/', views.ConfigurationView.as_view()),
url(r'^ack/(?P<task_id>\d+)/', views.AckView.as_view()),
url(r'^deployments', views.DeploymentsView.as_view()),
url(r'^config', views.ConfigurationView.as_view()),
url(r'^ack/(?P<task_id>\d+)', views.AckView.as_view()),
]
......@@ -20,10 +20,31 @@ class ServiceSerializer(serializers.ModelSerializer):
exclude = []
class DeploymentTaskItemSerializer(serializers.ModelSerializer):
service = ServiceSerializer()
key = backend_serializers.SSHPublicKeySerializerB()
site = SiteSerializer()
class Meta:
model = models.DeploymentTaskItem
fields = [
'action',
'key',
'service',
'site',
]
class DeploymentTaskSerializer(serializers.ModelSerializer):
key = backend_serializers.SSHPublicKeySerializerB()
service = ServiceSerializer()
class Meta:
model = models.DeploymentTask
fields = ['action', 'key']
fields = [
'action',
'key',
'service',
]
class DeploymentSerializer(serializers.Serializer):
......@@ -51,11 +72,21 @@ class UserSerializer(serializers.ModelSerializer):
deployments = DeploymentSerializer(many=True)
auth_groups = backend_serializers.AuthGroupSerializer(many=True)
deployment_tasks = DeploymentTaskSerializer(many=True)
deployment_task_items = DeploymentTaskItemSerializer(many=True)
class Meta:
model = models.User
fields = ['id', 'email', 'userinfo', 'ssh_keys', 'groups', 'deployments', 'auth_groups',
'deployment_tasks']
fields = [
'id',
'email',
'userinfo',
'ssh_keys',
'groups',
'deployments',
'auth_groups',
'deployment_tasks',
'deployment_task_items',
]
class ClientSerializer(serializers.HyperlinkedModelSerializer):
......
......@@ -4,6 +4,7 @@
import json
import logging
import pika
from pika.exceptions import ConnectionClosed
from requests.auth import HTTPBasicAuth
from django.contrib.auth.models import AbstractUser, Group
from django.core.cache import cache
......@@ -127,9 +128,14 @@ class RabbitMQInstance(SingletonModel):
@property
def _channel(self):
channel = self._connection.channel()
channel.confirm_delivery()
return channel
try:
channel = self._connection.channel()
channel.confirm_delivery()
return channel
except ConnectionClosed as exception:
LOGGER.error(self.msg('ConnectionClosed: ' + str(exception)))
self._init_connection()
return self._channel
def _publish(self, exchange, routing_key, body):
channel = self._channel
......@@ -647,6 +653,7 @@ class DeploymentTask(models.Model):
deploy = DeploymentTaskItem(
task=task,
site=site,
user=deployment.user
)
deploy.save()
LOGGER.debug(deploy.msg('generated'))
......@@ -674,6 +681,7 @@ class DeploymentTask(models.Model):
deploy = DeploymentTaskItem(
task=task,
site=site,
user=deployment.user
)
deploy.save()
LOGGER.debug(deploy.msg('generated'))
......@@ -736,7 +744,11 @@ class DeploymentTask(models.Model):
if self.action == 'withdraw':
self.key.try_final_deletion()
message = 'Completed: ' + str(self.action) + ' ' + str(self.key) + ' '+ str(self.site)
message = 'Completed: {} {} @ {}'.format(
self.action,
self.key,
self.service,
)
from .frontend.views import user_state_dict
content = {
......@@ -760,6 +772,23 @@ class DeploymentTaskItem(models.Model):
related_name='task_items',
on_delete=models.CASCADE,
)
user = models.ForeignKey(
User,
related_name='deployment_task_items',
on_delete=models.CASCADE,
)
@property
def service(self):
return self.task.service
@property
def key(self):
return self.task.key
@property
def action(self):
return self.deployment.key
def __str__(self):
return "{}@{}#{}".format(
......
......@@ -22,3 +22,9 @@ class SSHPublicKeySerializer(serializers.ModelSerializer):
class Meta:
model = SSHPublicKey
fields = ['id', 'name', 'key']
class SSHPublicKeySerializerB(serializers.ModelSerializer):
class Meta:
model = SSHPublicKey
fields = ['id', 'name']
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