Commit 84a5d56f authored by Lukas Burgey's avatar Lukas Burgey

Add direct relation between user and deployment state item

parent 86f454a0
# Generated by Django 2.1.3 on 2018-11-15 15:57
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0021_auto_20181115_1153'),
]
operations = [
migrations.AddField(
model_name='newdeploymentstateitem',
name='user',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='state_items', to=settings.AUTH_USER_MODEL),
),
]
......@@ -199,7 +199,6 @@ class NewDeployment(models.Model):
def services(self):
return self.group.services.all()
@property
def sites(self):
return [
......@@ -214,6 +213,7 @@ class NewDeployment(models.Model):
if site is not None:
NewDeploymentStateItem.get_state_item(
parent=self,
user=self.user,
site=site,
service=service,
).save()
......@@ -227,6 +227,7 @@ class NewDeployment(models.Model):
# LOGGER.debug('create_state_items: creating NewDeploymentStateItems for service %s at site %s', service, service_site)
NewDeploymentStateItem.get_state_item(
parent=self,
user=self.user,
site=service_site,
service=service,
).save()
......@@ -346,16 +347,27 @@ class NewDeploymentStateItem(models.Model):
related_name='state_items',
on_delete=models.CASCADE,
)
user = models.ForeignKey(
User,
related_name='state_items',
on_delete=models.SET_NULL,
blank=True,
null=True,
)
site = models.ForeignKey(
Site,
related_name='state_items',
on_delete=models.CASCADE,
)
service = models.ForeignKey(
Service,
related_name='state_items',
on_delete=models.CASCADE,
)
state = models.CharField(
max_length=50,
choices=STATE_CHOICES,
......@@ -374,6 +386,7 @@ class NewDeploymentStateItem(models.Model):
null=True,
blank=True,
)
# credentials for the service
# only valid when state == deployed
credentials = JSONField(
......@@ -382,23 +395,22 @@ class NewDeploymentStateItem(models.Model):
blank=True,
)
@property
def user(self):
return self.parent.user
@property
def user_credentials(self):
return self.parent.credentials
if self.user is not None:
return self.user.credentials
return {}
@property
def group(self):
return self.parent.group
@classmethod
def get_state_item(cls, parent=None, site=None, service=None):
def get_state_item(cls, parent=None, user=None, site=None, service=None):
try:
item = cls.objects.get(
parent=parent,
user=user,
site=site,
service=service,
)
......@@ -408,6 +420,7 @@ class NewDeploymentStateItem(models.Model):
except cls.DoesNotExist:
item = cls(
parent=parent,
user=user,
site=site,
service=service,
)
......
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