From 5c5d1b7748c5ad8accc341fb536f96c170747710 Mon Sep 17 00:00:00 2001 From: Aaron Tan Date: Mon, 7 Nov 2016 20:21:37 -0500 Subject: [PATCH] Enable notification endpoints. --- awx/api/serializers.py | 10 ++++------ awx/api/urls.py | 5 ++++- awx/api/views.py | 31 +++++++++++++++++++++++++++++++ awx/main/models/workflow.py | 22 +++++++++++++++++----- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index c8a7b98cfc..986f5c8c1e 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -2201,10 +2201,9 @@ class WorkflowJobTemplateSerializer(LabelsListMixin, UnifiedJobTemplateSerialize launch = reverse('api:workflow_job_template_launch', args=(obj.pk,)), workflow_nodes = reverse('api:workflow_job_template_workflow_nodes_list', args=(obj.pk,)), labels = reverse('api:workflow_job_template_label_list', args=(obj.pk,)), - # TODO: Implement notifications - #notification_templates_any = reverse('api:system_job_template_notification_templates_any_list', args=(obj.pk,)), - #notification_templates_success = reverse('api:system_job_template_notification_templates_success_list', args=(obj.pk,)), - #notification_templates_error = reverse('api:system_job_template_notification_templates_error_list', args=(obj.pk,)), + notification_templates_any = reverse('api:workflow_job_template_notification_templates_any_list', args=(obj.pk,)), + notification_templates_success = reverse('api:workflow_job_template_notification_templates_success_list', args=(obj.pk,)), + notification_templates_error = reverse('api:workflow_job_template_notification_templates_error_list', args=(obj.pk,)), survey_spec = reverse('api:workflow_job_template_survey_spec', args=(obj.pk,)), )) return res @@ -2228,8 +2227,7 @@ class WorkflowJobSerializer(LabelsListMixin, UnifiedJobSerializer): if obj.workflow_job_template: res['workflow_job_template'] = reverse('api:workflow_job_template_detail', args=(obj.workflow_job_template.pk,)) - # TODO: - #res['notifications'] = reverse('api:system_job_notifications_list', args=(obj.pk,)) + res['notifications'] = reverse('api:workflow_job_notifications_list', args=(obj.pk,)) res['workflow_nodes'] = reverse('api:workflow_job_workflow_nodes_list', args=(obj.pk,)) res['labels'] = reverse('api:workflow_job_label_list', args=(obj.pk,)) if obj.can_cancel or True: diff --git a/awx/api/urls.py b/awx/api/urls.py index 4699d9f2bf..8e349008e6 100644 --- a/awx/api/urls.py +++ b/awx/api/urls.py @@ -265,6 +265,9 @@ workflow_job_template_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/schedules/$', 'workflow_job_template_schedules_list'), url(r'^(?P[0-9]+)/survey_spec/$', 'workflow_job_template_survey_spec'), url(r'^(?P[0-9]+)/workflow_nodes/$', 'workflow_job_template_workflow_nodes_list'), + url(r'^(?P[0-9]+)/notification_templates_any/$', 'workflow_job_template_notification_templates_any_list'), + url(r'^(?P[0-9]+)/notification_templates_error/$', 'workflow_job_template_notification_templates_error_list'), + url(r'^(?P[0-9]+)/notification_templates_success/$', 'workflow_job_template_notification_templates_success_list'), url(r'^(?P[0-9]+)/labels/$', 'workflow_job_template_label_list'), # url(r'^(?P[0-9]+)/cancel/$', 'workflow_job_template_cancel'), ) @@ -275,7 +278,7 @@ workflow_job_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/workflow_nodes/$', 'workflow_job_workflow_nodes_list'), url(r'^(?P[0-9]+)/labels/$', 'workflow_job_label_list'), url(r'^(?P[0-9]+)/cancel/$', 'workflow_job_cancel'), - #url(r'^(?P[0-9]+)/notifications/$', 'workflow_job_notifications_list'), + url(r'^(?P[0-9]+)/notifications/$', 'workflow_job_notifications_list'), ) diff --git a/awx/api/views.py b/awx/api/views.py index ba76e777b3..390fc0194a 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2857,6 +2857,29 @@ class WorkflowJobTemplateSchedulesList(SubListCreateAttachDetachAPIView): parent_model = WorkflowJobTemplate relationship = 'schedules' parent_key = 'unified_job_template' + +class WorkflowJobTemplateNotificationTemplatesAnyList(SubListCreateAttachDetachAPIView): + + model = NotificationTemplate + serializer_class = NotificationTemplateSerializer + parent_model = WorkflowJobTemplate + relationship = 'notification_templates_any' + new_in_310 = True + +class WorkflowJobTemplateNotificationTemplatesErrorList(SubListCreateAttachDetachAPIView): + + model = NotificationTemplate + serializer_class = NotificationTemplateSerializer + parent_model = WorkflowJobTemplate + relationship = 'notification_templates_error' + new_in_310 = True + +class WorkflowJobTemplateNotificationTemplatesSuccessList(SubListCreateAttachDetachAPIView): + + model = NotificationTemplate + serializer_class = NotificationTemplateSerializer + parent_model = WorkflowJobTemplate + relationship = 'notification_templates_success' new_in_310 = True # TODO: @@ -2900,6 +2923,14 @@ class WorkflowJobCancel(RetrieveAPIView): else: return self.http_method_not_allowed(request, *args, **kwargs) +class WorkflowJobNotificationsList(SubListAPIView): + + model = Notification + serializer_class = NotificationSerializer + parent_model = WorkflowJob + relationship = 'notifications' + new_in_310 = True + class SystemJobTemplateList(ListAPIView): model = SystemJobTemplate diff --git a/awx/main/models/workflow.py b/awx/main/models/workflow.py index 0a9c081680..b232221347 100644 --- a/awx/main/models/workflow.py +++ b/awx/main/models/workflow.py @@ -13,7 +13,10 @@ from jsonfield import JSONField # AWX from awx.main.models import UnifiedJobTemplate, UnifiedJob -from awx.main.models.notifications import JobNotificationMixin +from awx.main.models.notifications import ( + NotificationTemplate, + JobNotificationMixin +) from awx.main.models.base import BaseModel, CreatedModifiedModel, VarsDictProperty from awx.main.models.rbac import ( ROLE_SINGLETON_SYSTEM_ADMINISTRATOR, @@ -306,7 +309,18 @@ class WorkflowJobTemplate(UnifiedJobTemplate, WorkflowJobOptions, SurveyJobTempl # TODO: don't allow running of job template if same workflow template running return False - # TODO: Notifications + @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])) + success_notification_templates = list(base_notification_templates + .filter(unifiedjobtemplate_notification_templates_for_success__in=[self])) + any_notification_templates = list(base_notification_templates + .filter(unifiedjobtemplate_notification_templates_for_any__in=[self])) + return dict(error=list(error_notification_templates), + success=list(success_notification_templates), + any=list(any_notification_templates)) # TODO: Surveys #def create_job(self, **kwargs): @@ -437,11 +451,9 @@ class WorkflowJob(UnifiedJob, WorkflowJobOptions, SurveyJobMixin, JobNotificatio def task_impact(self): return 0 - # TODO: workflow job notifications def get_notification_templates(self): - return [] + return self.workflow_job_template.notification_templates - # TODO: workflow job notifications def get_notification_friendly_name(self): return "Workflow Job"