Commit 0a7206e5 authored by Lukas Burgey's avatar Lukas Burgey

Rework the API facing the webpage

parent 4568bf16
......@@ -337,10 +337,10 @@ class NewDeployment(models.Model):
# sends a state update via RabbitMQ / STOMP to the users webpage instance
def publish_to_user(self):
if self.user is not None:
# mitigating circular dependencies here
from ..views.webpage import user_state
# avoiding circular dependencies here
from .serializers.webpage import NewDeploymentSerializer
msg = dumps({
'user_state': user_state(self.user),
'deployment': NewDeploymentSerializer(self).data,
})
RabbitMQInstance.load().publish_to_user(
self.user,
......
......@@ -88,14 +88,20 @@ class UserSerializer(serializers.ModelSerializer):
]
# contains properties which change a lot
class UserStateSerializer(serializers.ModelSerializer):
# deployment_state_items = NewDeploymentStateItemSerializer(many=True)
groups = backend_serializers.GroupSerializer(many=True)
ssh_keys = backend_serializers.SSHPublicKeySerializer(many=True)
deployments = NewDeploymentSerializer(many=True)
services = ServiceSerializer(many=True)
class Meta:
model = models.User
fields = [
# 'deployment_state_items',
'deployments',
'profile_name',
'groups',
'id',
'ssh_keys',
'userinfo',
'services',
]
......@@ -77,13 +77,14 @@ class User(AbstractUser):
return self.username
@property
def deployment_state_items(self):
items = []
for dep in self.deployments.all():
for item in dep.state_items.all():
items.append(item)
return items
# TODO obsolete right?
# @property
# def deployment_state_items(self):
# items = []
# for dep in self.deployments.all():
# for item in dep.state_items.all():
# items.append(item)
# return items
# we hide deleted keys here
# the full list of ssh keys is self._ssh_keys
......
......@@ -17,48 +17,18 @@ from ..models.serializers import webpage as serializers
LOGGER = logging.getLogger(__name__)
def user_services(user):
if user.is_authenticated:
return (
models.Service.objects
.filter(groups__user=user)
.distinct()
)
return []
def _api_error_response(error):
return Response({'error': error}, status=status.HTTP_400_BAD_REQUEST)
def user_state(user):
return serializers.UserStateSerializer(
user,
).data
def _api_state_response_data(request):
return {
'user_state': user_state(request.user),
}
def state_view_data(request):
data = {}
if request.user.is_authenticated:
data = _api_state_response_data(request)
data['user'] = serializers.UserSerializer(
data['user'] = serializers.UserStateSerializer(
request.user,
).data
data['services'] = serializers.ServiceSerializer(
user_services(request.user),
many=True,
).data
else:
data['user'] = None
data['user_state'] = None
data['services'] = []
if 'error' in request.session:
data['error'] = request.session['error']
......
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