Set up approval notifications to send

This commit is contained in:
beeankha
2019-09-04 18:04:43 -04:00
parent 6be2d84adb
commit 13450fdbf9
7 changed files with 35 additions and 64 deletions

View File

@@ -5,7 +5,7 @@
import logging
# Django
from django.db import models
from django.db import connection, models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist
@@ -577,6 +577,13 @@ class WorkflowJob(UnifiedJob, WorkflowJobOptions, SurveyJobMixin, JobNotificatio
for node in self.workflow_job_nodes.all().select_related('job'):
if node.job is None:
node_job_description = 'no job.'
elif type(node.unified_job_template) is WorkflowApprovalTemplate:
if node.job.status == 'pending':
node_job_description = 'APPROVE THIS!!!'
if node.job.status == 'successful':
node_job_description = 'THIS GOT APPROVED!!!'
if node.job.status == 'failed':
node_job_description = 'DENIED!!!'
else:
node_job_description = ('job #{0}, "{1}", which finished with status {2}.'
.format(node.job.id, node.job.name, node.job.status))
@@ -647,40 +654,6 @@ class WorkflowApprovalTemplate(UnifiedJobTemplate):
def get_absolute_url(self, request=None):
return reverse('api:workflow_approval_template_detail', kwargs={'pk': self.pk}, request=request)
@property
def notification_templates(self):
base_notification_templates = NotificationTemplate.objects.all()
error_notification_templates = list(base_notification_templates
.filter(unifiedjobtemplate_notification_templates_for_errors__in=[self]))
started_notification_templates = list(base_notification_templates
.filter(unifiedjobtemplate_notification_templates_for_started__in=[self]))
success_notification_templates = list(base_notification_templates
.filter(unifiedjobtemplate_notification_templates_for_success__in=[self]))
return dict(error=list(error_notification_templates),
started=list(started_notification_templates),
success=list(success_notification_templates))
# base_notification_templates = NotificationTemplate.objects.all()
# approval_notification_templates = list(base_notification_templates
# .filter(unifiedjobtemplate_notification_templates_for_errors__in=[self]),
# base_notification_templates
# .filter(unifiedjobtemplate_notification_templates_for_started__in=[self]),
# base_notification_templates
# .filter(unifiedjobtemplate_notification_templates_for_success__in=[self]))
# return dict(approval=list(approval_notification_templates))
# Placeholder... Approval nodes don't have orgs!
# if self.project is not None and self.project.organization is None:
# error_notification_templates = set(error_notification_templates + list(base_notification_templates.filter(
# organization_notification_templates_for_errors=self.project.organization)))
# started_notification_templates = set(started_notification_templates + list(base_notification_templates.filter(
# organization_notification_templates_for_started=self.project.organization)))
# success_notification_templates = set(success_notification_templates + list(base_notification_templates.filter(
# organization_notification_templates_for_success=self.project.organization)))
# return dict(error=list(error_notification_templates),
# approval_notification_templates=list(needs_approval_notification_templates),
# success=list(success_notification_templates))
@property
def workflow_job_template(self):
return self.workflowjobtemplatenodes.first().workflow_job_template
@@ -726,6 +699,7 @@ class WorkflowApproval(UnifiedJob, JobNotificationMixin):
def approve(self, request=None):
self.status = 'successful'
self.save()
self.send_approval_notification(self.status)
self.websocket_emit_status(self.status)
schedule_task_manager()
return reverse('api:workflow_approval_approve', kwargs={'pk': self.pk}, request=request)
@@ -733,24 +707,27 @@ class WorkflowApproval(UnifiedJob, JobNotificationMixin):
def deny(self, request=None):
self.status = 'failed'
self.save()
self.send_approval_notification(self.status)
self.websocket_emit_status(self.status)
schedule_task_manager()
return reverse('api:workflow_approval_deny', kwargs={'pk': self.pk}, request=request)
# Placeholder...
def approval_notification_data(self):
result = super(WorkflowApproval, self).approval_notification_data()
str_arr = ['Approval summary:', '']
for node in self.workflow_job_nodes.all().select_related('job'):
if node.job is None:
node_job_description = 'no job.'
else:
node_job_description = ('job #{0}, "{1}", which finished with status {2}.'
.format(node.job.id, node.job.name, node.job.status))
str_arr.append("- node #{0} spawns {1}".format(node.id, node_job_description))
result['body'] = '\n'.join(str_arr)
return result
def signal_start(self, **kwargs):
can_start = super(WorkflowApproval, self).signal_start(**kwargs)
self.send_approval_notification('running')
return can_start
def send_approval_notification(self, status):
from awx.main.tasks import send_notifications # avoid circular import
for nt in self.workflow_job_template.notification_templates["approvals"]:
(notification_subject, notification_body) = self.workflow_job.build_notification_message(nt, status)
def send_it(local_nt=nt, local_subject=notification_subject, local_body=notification_body):
def _func():
send_notifications.delay([local_nt.generate_notification(local_subject, local_body).id],
job_id=self.id)
return _func
connection.on_commit(send_it())
@property
def workflow_job_template(self):