Commit 9d60ce0b authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add new deployment models

parent 747b8e29
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 .user_models 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 Site(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 Service(PolymorphicModel):
name = models.CharField(
max_length=150,
unique=True,
)
description = models.TextField(
max_length=300,
blank=True,
)
site = models.ForeignKey(
Site,
related_name='services',
on_delete=models.CASCADE,
)
class SimpleService(Service):
groups = models.ManyToManyField(
Group,
related_name='services',
)
class GroupService(Service):
groups = models.ForeignKey(
Group,
related_name='services',
on_delete=models.CASCADE,
)
class Deployment(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(Deployment):
service = models.ForeignKey(
Service,
related_name='service_deployments',
on_delete=models.CASCADE,
)
class GroupDeployment(Deployment):
group = models.ForeignKey(
Group,
related_name='group_deployments',
on_delete=models.CASCADE,
)
class DeploymentStateItem(PolymorphicModel):
site = models.ForeignKey(
Site,
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(DeploymentStateItem):
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,
)
......@@ -44,6 +44,7 @@ ALLOWED_HOSTS = [
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'polymorphic',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
......
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