Commit f9de0e89 authored by lukas.burgey's avatar lukas.burgey

Refactor DeploymentState creation

parent 1044fc0b
Pipeline #110532 passed with stage
in 1 minute and 27 seconds
......@@ -226,19 +226,9 @@ class VODeployment(Deployment):
created_new_states = False
for service in self.services:
state, created = DeploymentState.objects.get_or_create(
user=self.user,
userinfo=self.user.userinfo,
site=service.site,
service=service,
service_name=service.name,
)
_, created = DeploymentState.get_or_create(self.user, service, self)
if created:
LOGGER.debug(state.msg('Created'))
state.bind_to_deployment(self) # make sure the state is bound to us
created_new_states = created
created_new_states = True
return created_new_states
......@@ -257,19 +247,9 @@ class VODeployment(Deployment):
def service_added(self, service):
LOGGER.debug(self.msg('Adding service {}'.format(service)))
state, created = DeploymentState.objects.get_or_create(
user=self.user,
userinfo=self.user.userinfo,
site=service.site,
service=service,
service_name=service.name,
)
if created:
LOGGER.debug(state.msg('Created'))
state.bind_to_deployment(self) # make sure the state is bound to us
state, _ = DeploymentState.get_or_create(self.user, service, self)
if self.state_target == DEPLOYED:
if state is not None and self.state_target == DEPLOYED:
state.publish_to_client()
state.publish_to_user()
......@@ -310,18 +290,7 @@ class ServiceDeployment(Deployment):
raise ValueError('User is not permitted to have a deployment of this service!')
def _assure_states_exist(self):
state, created = DeploymentState.objects.get_or_create(
user=self.user,
userinfo=self.user.userinfo,
site=self.service.site,
service=self.service,
service_name=self.service.name,
)
if created:
LOGGER.debug(state.msg('Created'))
state.bind_to_deployment(self) # make sure the state is bound to us
_, created = DeploymentState.get_or_create(self.user, self.service, self)
return created
@classmethod
......@@ -462,6 +431,34 @@ class DeploymentState(models.Model):
return False
@classmethod
def get_or_create(cls, user, service, deployment):
try:
state, created = cls.objects.get_or_create(
user=user,
site=service.site,
service=service,
defaults={
'userinfo': user.userinfo,
'service_name': service.name,
}
)
if created:
LOGGER.debug(state.msg('Created'))
state.bind_to_deployment(deployment)
return state, created
except cls.MultipleObjectsReturned:
LOGGER.error(
'Multiple DeploymentStates for %s and %s and %s',
user,
service,
deployment,
)
return None, False
# adds the deployment to our related deployments
def bind_to_deployment(self, deployment):
if not self.deployments.filter(id=deployment.id).exists():
......
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