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

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): ...@@ -199,7 +199,6 @@ class NewDeployment(models.Model):
def services(self): def services(self):
return self.group.services.all() return self.group.services.all()
@property @property
def sites(self): def sites(self):
return [ return [
...@@ -214,6 +213,7 @@ class NewDeployment(models.Model): ...@@ -214,6 +213,7 @@ class NewDeployment(models.Model):
if site is not None: if site is not None:
NewDeploymentStateItem.get_state_item( NewDeploymentStateItem.get_state_item(
parent=self, parent=self,
user=self.user,
site=site, site=site,
service=service, service=service,
).save() ).save()
...@@ -227,6 +227,7 @@ class NewDeployment(models.Model): ...@@ -227,6 +227,7 @@ class NewDeployment(models.Model):
# LOGGER.debug('create_state_items: creating NewDeploymentStateItems for service %s at site %s', service, service_site) # LOGGER.debug('create_state_items: creating NewDeploymentStateItems for service %s at site %s', service, service_site)
NewDeploymentStateItem.get_state_item( NewDeploymentStateItem.get_state_item(
parent=self, parent=self,
user=self.user,
site=service_site, site=service_site,
service=service, service=service,
).save() ).save()
...@@ -346,16 +347,27 @@ class NewDeploymentStateItem(models.Model): ...@@ -346,16 +347,27 @@ class NewDeploymentStateItem(models.Model):
related_name='state_items', related_name='state_items',
on_delete=models.CASCADE, 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 = models.ForeignKey(
Site, Site,
related_name='state_items', related_name='state_items',
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
service = models.ForeignKey( service = models.ForeignKey(
Service, Service,
related_name='state_items', related_name='state_items',
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
state = models.CharField( state = models.CharField(
max_length=50, max_length=50,
choices=STATE_CHOICES, choices=STATE_CHOICES,
...@@ -374,6 +386,7 @@ class NewDeploymentStateItem(models.Model): ...@@ -374,6 +386,7 @@ class NewDeploymentStateItem(models.Model):
null=True, null=True,
blank=True, blank=True,
) )
# credentials for the service # credentials for the service
# only valid when state == deployed # only valid when state == deployed
credentials = JSONField( credentials = JSONField(
...@@ -382,23 +395,22 @@ class NewDeploymentStateItem(models.Model): ...@@ -382,23 +395,22 @@ class NewDeploymentStateItem(models.Model):
blank=True, blank=True,
) )
@property
def user(self):
return self.parent.user
@property @property
def user_credentials(self): def user_credentials(self):
return self.parent.credentials if self.user is not None:
return self.user.credentials
return {}
@property @property
def group(self): def group(self):
return self.parent.group return self.parent.group
@classmethod @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: try:
item = cls.objects.get( item = cls.objects.get(
parent=parent, parent=parent,
user=user,
site=site, site=site,
service=service, service=service,
) )
...@@ -408,6 +420,7 @@ class NewDeploymentStateItem(models.Model): ...@@ -408,6 +420,7 @@ class NewDeploymentStateItem(models.Model):
except cls.DoesNotExist: except cls.DoesNotExist:
item = cls( item = cls(
parent=parent, parent=parent,
user=user,
site=site, site=site,
service=service, 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