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

Remove new deployments

parent df1a24d7
from logging import getLogger
from django.contrib.auth.models import Group
from django.db import models
from django.db.models import Q
from django_mysql.models import JSONField
from polymorphic.models import PolymorphicModel
from rest_framework import serializers
from rest_polymorphic.serializers import PolymorphicSerializer
from .users import User
LOGGER = getLogger(__name__)
STATE_CHOICES = (
('deployment_pending', 'Deployment Pending'),
('removal_pending', 'Removal Pending'),
('deployed', 'Deployed'),
('not_deployed', 'Not Deployed'),
('questionnaire', 'Questionnaire'),
('failed', 'Failed'),
('rejected', 'Rejected'),
)
def questionnaire_default():
return {}
def credential_default():
return {}
class NSite(models.Model):
name = models.CharField(
max_length=150,
unique=True,
)
description = models.TextField(
max_length=300,
blank=True,
)
client = models.OneToOneField(
User,
related_name='site',
on_delete=models.SET_NULL,
null=True,
blank=True,
)
class NService(PolymorphicModel):
name = models.CharField(
max_length=150,
unique=True,
)
description = models.TextField(
max_length=300,
blank=True,
)
site = models.ForeignKey(
NSite,
related_name='services',
on_delete=models.CASCADE,
)
class SimpleService(NService):
groups = models.ManyToManyField(
Group,
related_name='services',
)
class GroupService(NService):
groups = models.ForeignKey(
Group,
related_name='services',
on_delete=models.CASCADE,
)
class NDeployment(PolymorphicModel):
user = models.ForeignKey(
User,
related_name='deployments',
on_delete=models.SET_NULL,
null=True,
)
is_active = models.BooleanField(
default=True,
)
@classmethod
def get_deployment(cls, user, service=None, group=None):
if service is not None:
try:
return cls.objects.get(
Q(ServiceDeployment___service=service)
)
except cls.DoesNotExist:
dep = ServiceDeployment(
user=user,
service=service,
)
dep.save()
return dep
elif group is not None:
try:
return cls.objects.get(
Q(GroupDeployment___group=group)
)
except cls.DoesNotExist:
dep = GroupDeployment(
user=user,
group=group,
)
dep.save()
return dep
else:
raise ValueError('get_deployment needs either a service or a group')
class ServiceDeployment(NDeployment):
service = models.ForeignKey(
NService,
related_name='service_deployments',
on_delete=models.CASCADE,
)
class GroupDeployment(NDeployment):
group = models.ForeignKey(
Group,
related_name='group_deployments',
on_delete=models.CASCADE,
)
class NDeploymentStateItem(PolymorphicModel):
site = models.ForeignKey(
NSite,
related_name='deployment_state_items',
on_delete=models.CASCADE,
)
state = models.CharField(
max_length=50,
choices=STATE_CHOICES,
default='deployment_pending',
)
# message for the user
message = models.TextField(
max_length=300,
default='',
)
# questions for the user (needed for deployment
questionnaire = JSONField(
default=questionnaire_default,
null=True,
blank=True,
)
# credentials for the service
# only valid when state == deployed
credentials = JSONField(
default=credential_default,
null=True,
blank=True,
)
class GroupDeploymentStateItem(NDeploymentStateItem):
parent = models.ForeignKey(
GroupDeployment,
related_name='group_deployment_state_items',
on_delete=models.CASCADE,
)
service = models.ForeignKey(
GroupService,
related_name='group_deployment_state_items',
on_delete=models.CASCADE,
)
# serializers
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ('name',)
class ServiceSerializer(serializers.ModelSerializer):
class Meta:
model = NService
fields = ('name', 'description', 'site',)
class SimpleServiceSerializer(serializers.ModelSerializer):
groups = GroupSerializer(many=True)
class Meta:
model = SimpleService
fields = ('name', 'description', 'site', 'groups',)
class GroupServiceSerializer(serializers.ModelSerializer):
group = GroupSerializer()
class Meta:
model = SimpleService
fields = ('name', 'description', 'site', 'group',)
class GenericServiceSerializer(PolymorphicSerializer):
mode_serializer_mapping = {
NService: ServiceSerializer,
SimpleService: SimpleServiceSerializer,
GroupService: GroupServiceSerializer,
}
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