From dde70dafec20097bfdef7263c5ce2cc592195231 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Wed, 17 Feb 2016 15:18:18 +0000 Subject: [PATCH] Refactor NotificationTemplate to Notifier --- awx/api/metadata.py | 4 +- awx/api/serializers.py | 40 +++---- awx/api/urls.py | 36 +++--- awx/api/views.py | 128 +++++++++++----------- awx/main/access.py | 8 +- awx/main/models/__init__.py | 2 +- awx/main/models/activity_stream.py | 2 +- awx/main/models/base.py | 18 +-- awx/main/models/inventory.py | 10 +- awx/main/models/jobs.py | 10 +- awx/main/models/notifications.py | 12 +- awx/main/models/projects.py | 10 +- awx/main/models/unified_jobs.py | 2 +- awx/main/notifications/webhook_backend.py | 2 + awx/main/signals.py | 2 +- 15 files changed, 145 insertions(+), 141 deletions(-) diff --git a/awx/api/metadata.py b/awx/api/metadata.py index b5e6d7043a..05bd11a4c7 100644 --- a/awx/api/metadata.py +++ b/awx/api/metadata.py @@ -12,7 +12,7 @@ from rest_framework import serializers from rest_framework.request import clone_request # Ansible Tower -from awx.main.models import InventorySource, NotificationTemplate +from awx.main.models import InventorySource, Notifier class Metadata(metadata.SimpleMetadata): @@ -58,7 +58,7 @@ class Metadata(metadata.SimpleMetadata): # Special handling of notification configuration where the required properties # are conditional on the type selected. if field.field_name == 'notification_configuration': - for (notification_type_name, notification_tr_name, notification_type_class) in NotificationTemplate.NOTIFICATION_TYPES: + for (notification_type_name, notification_tr_name, notification_type_class) in Notifier.NOTIFICATION_TYPES: field_info[notification_type_name] = notification_type_class.init_parameters # Update type of fields returned... diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 7bc25f532d..3a324ce21f 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -769,9 +769,9 @@ class OrganizationSerializer(BaseSerializer): teams = reverse('api:organization_teams_list', args=(obj.pk,)), activity_stream = reverse('api:organization_activity_stream_list', args=(obj.pk,)), notifiers = reverse('api:organization_notifiers_list', args=(obj.pk,)), - notifiers_any = reverse('api:organization_notifications_any_list', args=(obj.pk,)), - notifiers_success = reverse('api:organization_notifications_success_list', args=(obj.pk,)), - notifiers_error = reverse('api:organization_notifications_error_list', args=(obj.pk,)), + notifiers_any = reverse('api:organization_notifiers_any_list', args=(obj.pk,)), + notifiers_success = reverse('api:organization_notifiers_success_list', args=(obj.pk,)), + notifiers_error = reverse('api:organization_notifiers_error_list', args=(obj.pk,)), )) return res @@ -851,9 +851,9 @@ class ProjectSerializer(UnifiedJobTemplateSerializer, ProjectOptionsSerializer): project_updates = reverse('api:project_updates_list', args=(obj.pk,)), schedules = reverse('api:project_schedules_list', args=(obj.pk,)), activity_stream = reverse('api:project_activity_stream_list', args=(obj.pk,)), - notifiers_any = reverse('api:project_notifications_any_list', args=(obj.pk,)), - notifiers_success = reverse('api:project_notifications_success_list', args=(obj.pk,)), - notifiers_error = reverse('api:project_notifications_error_list', args=(obj.pk,)), + notifiers_any = reverse('api:project_notifiers_any_list', args=(obj.pk,)), + notifiers_success = reverse('api:project_notifiers_success_list', args=(obj.pk,)), + notifiers_error = reverse('api:project_notifiers_error_list', args=(obj.pk,)), )) # Backwards compatibility. if obj.current_update: @@ -1298,9 +1298,9 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt activity_stream = reverse('api:inventory_activity_stream_list', args=(obj.pk,)), hosts = reverse('api:inventory_source_hosts_list', args=(obj.pk,)), groups = reverse('api:inventory_source_groups_list', args=(obj.pk,)), - notifiers_any = reverse('api:inventory_source_notifications_any_list', args=(obj.pk,)), - notifiers_success = reverse('api:inventory_source_notifications_success_list', args=(obj.pk,)), - notifiers_error = reverse('api:inventory_source_notifications_error_list', args=(obj.pk,)), + notifiers_any = reverse('api:inventory_source_notifiers_any_list', args=(obj.pk,)), + notifiers_success = reverse('api:inventory_source_notifiers_success_list', args=(obj.pk,)), + notifiers_error = reverse('api:inventory_source_notifiers_error_list', args=(obj.pk,)), )) if obj.inventory and obj.inventory.active: res['inventory'] = reverse('api:inventory_detail', args=(obj.inventory.pk,)) @@ -1564,9 +1564,9 @@ class JobTemplateSerializer(UnifiedJobTemplateSerializer, JobOptionsSerializer): schedules = reverse('api:job_template_schedules_list', args=(obj.pk,)), activity_stream = reverse('api:job_template_activity_stream_list', args=(obj.pk,)), launch = reverse('api:job_template_launch', args=(obj.pk,)), - notifiers_any = reverse('api:job_template_notifications_any_list', args=(obj.pk,)), - notifiers_success = reverse('api:job_template_notifications_success_list', args=(obj.pk,)), - notifiers_error = reverse('api:job_template_notifications_error_list', args=(obj.pk,)), + notifiers_any = reverse('api:job_template_notifiers_any_list', args=(obj.pk,)), + notifiers_success = reverse('api:job_template_notifiers_success_list', args=(obj.pk,)), + notifiers_error = reverse('api:job_template_notifiers_error_list', args=(obj.pk,)), )) if obj.host_config_key: res['callback'] = reverse('api:job_template_callback', args=(obj.pk,)) @@ -2053,22 +2053,24 @@ class JobLaunchSerializer(BaseSerializer): attrs = super(JobLaunchSerializer, self).validate(attrs) return attrs -class NotificationTemplateSerializer(BaseSerializer): +class NotifierSerializer(BaseSerializer): class Meta: - model = NotificationTemplate + model = Notifier fields = ('*', 'organization', 'notification_type', 'notification_configuration') def get_related(self, obj): - res = super(NotificationTemplateSerializer, self).get_related(obj) + res = super(NotifierSerializer, self).get_related(obj) res.update(dict( - test = reverse('api:notification_template_test', args=(obj.pk,)), - notifications = reverse('api:notification_template_notification_list', args=(obj.pk,)), + test = reverse('api:notifier_test', args=(obj.pk,)), + notifications = reverse('api:notifier_notification_list', args=(obj.pk,)), )) + if obj.organization and obj.organization.active: + res['organization'] = reverse('api:organization_detail', args=(obj.organization.pk,)) return res def validate(self, attrs): - notification_class = NotificationTemplate.CLASS_FOR_NOTIFICATION_TYPE[attrs['notification_type']] + notification_class = Notifier.CLASS_FOR_NOTIFICATION_TYPE[attrs['notification_type']] missing_fields = [] for field in notification_class.init_parameters: if field not in attrs['notification_configuration']: @@ -2088,7 +2090,7 @@ class NotificationSerializer(BaseSerializer): def get_related(self, obj): res = super(NotificationSerializer, self).get_related(obj) res.update(dict( - notification_template = reverse('api:notification_template_detail', args=(obj.notifier.pk,)), + notifier = reverse('api:notifier_detail', args=(obj.notifier.pk,)), )) return res diff --git a/awx/api/urls.py b/awx/api/urls.py index 7e55e46a5c..d9e0fca5cb 100644 --- a/awx/api/urls.py +++ b/awx/api/urls.py @@ -21,9 +21,9 @@ organization_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/teams/$', 'organization_teams_list'), url(r'^(?P[0-9]+)/activity_stream/$', 'organization_activity_stream_list'), url(r'^(?P[0-9]+)/notifiers/$', 'organization_notifiers_list'), - url(r'^(?P[0-9]+)/notifications_any/$', 'organization_notifications_any_list'), - url(r'^(?P[0-9]+)/notifications_error/$', 'organization_notifications_error_list'), - url(r'^(?P[0-9]+)/notifications_success/$', 'organization_notifications_success_list'), + url(r'^(?P[0-9]+)/notifiers_any/$', 'organization_notifiers_any_list'), + url(r'^(?P[0-9]+)/notifiers_error/$', 'organization_notifiers_error_list'), + url(r'^(?P[0-9]+)/notifiers_success/$', 'organization_notifiers_success_list'), ) user_urls = patterns('awx.api.views', @@ -48,9 +48,9 @@ project_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/project_updates/$', 'project_updates_list'), url(r'^(?P[0-9]+)/activity_stream/$', 'project_activity_stream_list'), url(r'^(?P[0-9]+)/schedules/$', 'project_schedules_list'), - url(r'^(?P[0-9]+)/notifications_any/$', 'project_notifications_any_list'), - url(r'^(?P[0-9]+)/notifications_error/$', 'project_notifications_error_list'), - url(r'^(?P[0-9]+)/notifications_success/$', 'project_notifications_success_list'), + url(r'^(?P[0-9]+)/notifiers_any/$', 'project_notifiers_any_list'), + url(r'^(?P[0-9]+)/notifiers_error/$', 'project_notifiers_error_list'), + url(r'^(?P[0-9]+)/notifiers_success/$', 'project_notifiers_success_list'), ) project_update_urls = patterns('awx.api.views', @@ -128,9 +128,9 @@ inventory_source_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/schedules/$', 'inventory_source_schedules_list'), url(r'^(?P[0-9]+)/groups/$', 'inventory_source_groups_list'), url(r'^(?P[0-9]+)/hosts/$', 'inventory_source_hosts_list'), - url(r'^(?P[0-9]+)/notifications_any/$', 'inventory_source_notifications_any_list'), - url(r'^(?P[0-9]+)/notifications_error/$', 'inventory_source_notifications_error_list'), - url(r'^(?P[0-9]+)/notifications_success/$', 'inventory_source_notifications_success_list'), + url(r'^(?P[0-9]+)/notifiers_any/$', 'inventory_source_notifiers_any_list'), + url(r'^(?P[0-9]+)/notifiers_error/$', 'inventory_source_notifiers_error_list'), + url(r'^(?P[0-9]+)/notifiers_success/$', 'inventory_source_notifiers_success_list'), ) inventory_update_urls = patterns('awx.api.views', @@ -165,9 +165,9 @@ job_template_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/schedules/$', 'job_template_schedules_list'), url(r'^(?P[0-9]+)/survey_spec/$', 'job_template_survey_spec'), url(r'^(?P[0-9]+)/activity_stream/$', 'job_template_activity_stream_list'), - url(r'^(?P[0-9]+)/notifications_any/$', 'job_template_notifications_any_list'), - url(r'^(?P[0-9]+)/notifications_error/$', 'job_template_notifications_error_list'), - url(r'^(?P[0-9]+)/notifications_success/$', 'job_template_notifications_success_list'), + url(r'^(?P[0-9]+)/notifiers_any/$', 'job_template_notifiers_any_list'), + url(r'^(?P[0-9]+)/notifiers_error/$', 'job_template_notifiers_error_list'), + url(r'^(?P[0-9]+)/notifiers_success/$', 'job_template_notifiers_success_list'), ) job_urls = patterns('awx.api.views', @@ -225,11 +225,11 @@ system_job_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/cancel/$', 'system_job_cancel'), ) -notification_template_urls = patterns('awx.api.views', - url(r'^$', 'notification_template_list'), - url(r'^(?P[0-9]+)/$', 'notification_template_detail'), - url(r'^(?P[0-9]+)/test/$', 'notification_template_test'), - url(r'^(?P[0-9]+)/notifications/$', 'notification_template_notification_list'), +notifier_urls = patterns('awx.api.views', + url(r'^$', 'notifier_list'), + url(r'^(?P[0-9]+)/$', 'notifier_detail'), + url(r'^(?P[0-9]+)/test/$', 'notifier_test'), + url(r'^(?P[0-9]+)/notifications/$', 'notifier_notification_list'), ) notification_urls = patterns('awx.api.views', @@ -285,7 +285,7 @@ v1_urls = patterns('awx.api.views', url(r'^ad_hoc_command_events/', include(ad_hoc_command_event_urls)), url(r'^system_job_templates/', include(system_job_template_urls)), url(r'^system_jobs/', include(system_job_urls)), - url(r'^notification_templates/', include(notification_template_urls)), + url(r'^notifiers/', include(notifier_urls)), url(r'^notifications/', include(notification_urls)), url(r'^unified_job_templates/$', 'unified_job_template_list'), url(r'^unified_jobs/$', 'unified_job_list'), diff --git a/awx/api/views.py b/awx/api/views.py index be1bd3b609..439de3f845 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -135,7 +135,7 @@ class ApiV1RootView(APIView): data['system_job_templates'] = reverse('api:system_job_template_list') data['system_jobs'] = reverse('api:system_job_list') data['schedules'] = reverse('api:schedule_list') - data['notification_templates'] = reverse('api:notification_template_list') + data['notifiers'] = reverse('api:notifier_list') data['notifications'] = reverse('api:notification_list') data['unified_job_templates'] = reverse('api:unified_job_template_list') data['unified_jobs'] = reverse('api:unified_job_list') @@ -687,32 +687,32 @@ class OrganizationActivityStreamList(SubListAPIView): class OrganizationNotifiersList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = Organization - relationship = 'notification_templates' + relationship = 'notifiers' parent_key = 'organization' -class OrganizationNotificationsAnyList(SubListCreateAttachDetachAPIView): +class OrganizationNotifiersAnyList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = Organization - relationship = 'notification_any' + relationship = 'notifiers_any' -class OrganizationNotificationsErrorList(SubListCreateAttachDetachAPIView): +class OrganizationNotifiersErrorList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = Organization - relationship = 'notification_erros' + relationship = 'notifiers_error' -class OrganizationNotificationsSuccessList(SubListCreateAttachDetachAPIView): +class OrganizationNotifiersSuccessList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = Organization - relationship = 'notification_success' + relationship = 'notifiers_success' class TeamList(ListCreateAPIView): @@ -879,26 +879,26 @@ class ProjectActivityStreamList(SubListAPIView): return qs.filter(project=parent) return qs.filter(Q(project=parent) | Q(credential__in=parent.credential)) -class ProjectNotificationsAnyList(SubListCreateAttachDetachAPIView): +class ProjectNotifiersAnyList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = Project - relationship = 'notification_any' + relationship = 'notifiers_any' -class ProjectNotificationsErrorList(SubListCreateAttachDetachAPIView): +class ProjectNotifiersErrorList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = Project - relationship = 'notification_errors' + relationship = 'notifiers_error' -class ProjectNotificationsSuccessList(SubListCreateAttachDetachAPIView): +class ProjectNotifiersSuccessList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = Project - relationship = 'notification_success' + relationship = 'notifiers_success' class ProjectUpdatesList(SubListAPIView): @@ -1781,26 +1781,26 @@ class InventorySourceActivityStreamList(SubListAPIView): # Okay, let it through. return super(type(self), self).get(request, *args, **kwargs) -class InventorySourceNotificationsAnyList(SubListCreateAttachDetachAPIView): +class InventorySourceNotifiersAnyList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = InventorySource - relationship = 'notification_any' + relationship = 'notifiers_any' -class InventorySourceNotificationsErrorList(SubListCreateAttachDetachAPIView): +class InventorySourceNotifiersErrorList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = InventorySource - relationship = 'notification_errors' + relationship = 'notifiers_error' -class InventorySourceNotificationsSuccessList(SubListCreateAttachDetachAPIView): +class InventorySourceNotifiersSuccessList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = InventorySource - relationship = 'notification_success' + relationship = 'notifiers_success' class InventorySourceHostsList(SubListAPIView): @@ -2027,26 +2027,26 @@ class JobTemplateActivityStreamList(SubListAPIView): # Okay, let it through. return super(type(self), self).get(request, *args, **kwargs) -class JobTemplateNotificationsAnyList(SubListCreateAttachDetachAPIView): +class JobTemplateNotifiersAnyList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = JobTemplate - relationship = 'notification_any' + relationship = 'notifiers_any' -class JobTemplateNotificationsErrorList(SubListCreateAttachDetachAPIView): +class JobTemplateNotifiersErrorList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = JobTemplate - relationship = 'notification_errors' + relationship = 'notifiers_error' -class JobTemplateNotificationsSuccessList(SubListCreateAttachDetachAPIView): +class JobTemplateNotifiersSuccessList(SubListCreateAttachDetachAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer parent_model = JobTemplate - relationship = 'notification_success' + relationship = 'notifiers_success' class JobTemplateCallback(GenericAPIView): @@ -3032,22 +3032,22 @@ class AdHocCommandStdout(UnifiedJobStdout): model = AdHocCommand new_in_220 = True -class NotificationTemplateList(ListCreateAPIView): +class NotifierList(ListCreateAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer new_in_300 = True -class NotificationTemplateDetail(RetrieveUpdateDestroyAPIView): +class NotifierDetail(RetrieveUpdateDestroyAPIView): - model = NotificationTemplate - serializer_class = NotificationTemplateSerializer + model = Notifier + serializer_class = NotifierSerializer new_in_300 = True -class NotificationTemplateTest(GenericAPIView): +class NotifierTest(GenericAPIView): - view_name = 'Notification Template Test' - model = NotificationTemplate + view_name = 'Notifier Test' + model = Notifier serializer_class = EmptySerializer new_in_300 = True @@ -3063,11 +3063,11 @@ class NotificationTemplateTest(GenericAPIView): headers=headers, status=status.HTTP_202_ACCEPTED) -class NotificationTemplateNotificationList(SubListAPIView): +class NotifierNotificationList(SubListAPIView): model = Notification serializer_class = NotificationSerializer - parent_model = NotificationTemplate + parent_model = Notifier relationship = 'notifications' parent_key = 'notifier' @@ -3079,7 +3079,7 @@ class NotificationList(ListAPIView): class NotificationDetail(RetrieveAPIView): - model = NotificationTemplate + model = Notification serializer_class = NotificationSerializer new_in_300 = True diff --git a/awx/main/access.py b/awx/main/access.py index 3ffbaf7f85..fb89dd30d8 100644 --- a/awx/main/access.py +++ b/awx/main/access.py @@ -1484,11 +1484,11 @@ class ScheduleAccess(BaseAccess): else: return False -class NotificationTemplateAccess(BaseAccess): +class NotifierAccess(BaseAccess): ''' - I can see/use a notification template if I have permission to + I can see/use a notifier if I have permission to ''' - model = NotificationTemplate + model = Notifier def get_queryset(self): qs = self.model.objects.filter(active=True).distinct() @@ -1708,5 +1708,5 @@ register_access(UnifiedJob, UnifiedJobAccess) register_access(ActivityStream, ActivityStreamAccess) register_access(CustomInventoryScript, CustomInventoryScriptAccess) register_access(TowerSettings, TowerSettingsAccess) -register_access(NotificationTemplate, NotificationTemplateAccess) +register_access(Notifier, NotifierAccess) register_access(Notification, NotificationAccess) diff --git a/awx/main/models/__init__.py b/awx/main/models/__init__.py index 2397b6137b..cf8235ba18 100644 --- a/awx/main/models/__init__.py +++ b/awx/main/models/__init__.py @@ -61,5 +61,5 @@ activity_stream_registrar.connect(AdHocCommand) activity_stream_registrar.connect(Schedule) activity_stream_registrar.connect(CustomInventoryScript) activity_stream_registrar.connect(TowerSettings) -activity_stream_registrar.connect(NotificationTemplate) +activity_stream_registrar.connect(Notifier) activity_stream_registrar.connect(Notification) diff --git a/awx/main/models/activity_stream.py b/awx/main/models/activity_stream.py index 12a54c7af2..dfada31484 100644 --- a/awx/main/models/activity_stream.py +++ b/awx/main/models/activity_stream.py @@ -53,7 +53,7 @@ class ActivityStream(models.Model): ad_hoc_command = models.ManyToManyField("AdHocCommand", blank=True) schedule = models.ManyToManyField("Schedule", blank=True) custom_inventory_script = models.ManyToManyField("CustomInventoryScript", blank=True) - notification_template = models.ManyToManyField("NotificationTemplate", blank=True) + notifier = models.ManyToManyField("Notifier", blank=True) notification = models.ManyToManyField("Notification", blank=True) def get_absolute_url(self): diff --git a/awx/main/models/base.py b/awx/main/models/base.py index f3f158855c..c4edfbd8ba 100644 --- a/awx/main/models/base.py +++ b/awx/main/models/base.py @@ -343,20 +343,20 @@ class NotificationFieldsModel(BaseModel): class Meta: abstract = True - notification_errors = models.ManyToManyField( - "NotificationTemplate", + notifiers_error = models.ManyToManyField( + "Notifier", blank=True, - related_name='%(class)s_notifications_for_errors' + related_name='%(class)s_notifiers_for_errors' ) - notification_success = models.ManyToManyField( - "NotificationTemplate", + notifiers_success = models.ManyToManyField( + "Notifier", blank=True, - related_name='%(class)s_notifications_for_success' + related_name='%(class)s_notifiers_for_success' ) - notification_any = models.ManyToManyField( - "NotificationTemplate", + notifiers_any = models.ManyToManyField( + "Notifier", blank=True, - related_name='%(class)s_notifications_for_any' + related_name='%(class)s_notifiers_for_any' ) diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index febf010f20..b0acf0a90b 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -23,7 +23,7 @@ from awx.main.managers import HostManager from awx.main.models.base import * # noqa from awx.main.models.jobs import Job from awx.main.models.unified_jobs import * # noqa -from awx.main.models.notifications import NotificationTemplate +from awx.main.models.notifications import Notifier from awx.main.utils import ignore_inventory_computed_fields, _inventory_updates __all__ = ['Inventory', 'Host', 'Group', 'InventorySource', 'InventoryUpdate', 'CustomInventoryScript'] @@ -1184,10 +1184,10 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions): @property def notifiers(self): # Return all notifiers defined on the Project, and on the Organization for each trigger type - base_notifiers = NotificationTemplate.objects.filter(active=True) - error_notifiers = list(base_notifiers.filter(organization_notifications_for_errors__in=self)) - success_notifiers = list(base_notifiers.filter(organization_notifications_for_success__in=self)) - any_notifiers = list(base_notifiers.filter(organization_notifications_for_any__in=self)) + base_notifiers = Notifier.objects.filter(active=True) + error_notifiers = list(base_notifiers.filter(organization_notifiers_for_errors__in=self)) + success_notifiers = list(base_notifiers.filter(organization_notifiers_for_success__in=self)) + any_notifiers = list(base_notifiers.filter(organization_notifiers_for_any__in=self)) return dict(error=error_notifiers, success=success_notifiers, any=any_notifiers) def clean_source(self): diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 42f7ccf676..dd772d695d 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -22,7 +22,7 @@ from jsonfield import JSONField from awx.main.constants import CLOUD_PROVIDERS from awx.main.models.base import * # noqa from awx.main.models.unified_jobs import * # noqa -from awx.main.models.notifications import NotificationTemplate +from awx.main.models.notifications import Notifier from awx.main.utils import decrypt_field, ignore_inventory_computed_fields from awx.main.utils import emit_websocket_notification from awx.main.redact import PlainTextCleaner @@ -336,10 +336,10 @@ class JobTemplate(UnifiedJobTemplate, JobOptions): # Return all notifiers defined on the Job Template, on the Project, and on the Organization for each trigger type # TODO: Currently there is no org fk on project so this will need to be added once that is # available after the rbac pr - base_notifiers = NotificationTemplate.objects.filter(active=True) - error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_errors__in=[self, self.project])) - success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_success__in=[self, self.project])) - any_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_any__in=[self, self.project])) + base_notifiers = Notifier.objects.filter(active=True) + error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_errors__in=[self, self.project])) + success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_success__in=[self, self.project])) + any_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_any__in=[self, self.project])) return dict(error=error_notifiers, success=success_notifiers, any=any_notifiers) class Job(UnifiedJob, JobOptions): diff --git a/awx/main/models/notifications.py b/awx/main/models/notifications.py index 4fc005256b..2e1bb9df2f 100644 --- a/awx/main/models/notifications.py +++ b/awx/main/models/notifications.py @@ -23,9 +23,9 @@ from jsonfield import JSONField logger = logging.getLogger('awx.main.models.notifications') -__all__ = ['NotificationTemplate', 'Notification'] +__all__ = ['Notifier', 'Notification'] -class NotificationTemplate(CommonModel): +class Notifier(CommonModel): NOTIFICATION_TYPES = [('email', _('Email'), CustomEmailBackend), ('slack', _('Slack'), SlackBackend), @@ -45,7 +45,7 @@ class NotificationTemplate(CommonModel): blank=False, null=True, on_delete=models.SET_NULL, - related_name='notification_templates', + related_name='notifiers', ) notification_type = models.CharField( @@ -56,7 +56,7 @@ class NotificationTemplate(CommonModel): notification_configuration = JSONField(blank=False) def get_absolute_url(self): - return reverse('api:notification_template_detail', args=(self.pk,)) + return reverse('api:notifier_detail', args=(self.pk,)) @property def notification_class(self): @@ -100,7 +100,7 @@ class Notification(CreatedModifiedModel): ordering = ('pk',) notifier = models.ForeignKey( - 'NotificationTemplate', + 'Notifier', related_name='notifications', on_delete=models.CASCADE, editable=False @@ -122,7 +122,7 @@ class Notification(CreatedModifiedModel): ) notification_type = models.CharField( max_length = 32, - choices=NotificationTemplate.NOTIFICATION_TYPE_CHOICES, + choices=Notifier.NOTIFICATION_TYPE_CHOICES, ) recipients = models.TextField( blank=True, diff --git a/awx/main/models/projects.py b/awx/main/models/projects.py index 730604d3e4..8a320e3cfc 100644 --- a/awx/main/models/projects.py +++ b/awx/main/models/projects.py @@ -21,7 +21,7 @@ from django.utils.timezone import now, make_aware, get_default_timezone from awx.lib.compat import slugify from awx.main.models.base import * # noqa from awx.main.models.jobs import Job -from awx.main.models.notifications import NotificationTemplate +from awx.main.models.notifications import Notifier from awx.main.models.unified_jobs import * # noqa from awx.main.utils import update_scm_url @@ -316,16 +316,16 @@ class Project(UnifiedJobTemplate, ProjectOptions): # Return all notifiers defined on the Project, and on the Organization for each trigger type # TODO: Currently there is no org fk on project so this will need to be added back once that is # available after the rbac pr - base_notifiers = NotificationTemplate.objects.filter(active=True) + base_notifiers = Notifier.objects.filter(active=True) # error_notifiers = list(base_notifiers.filter(Q(project_notifications_for_errors__in=self) | # Q(organization_notifications_for_errors__in=self.organization))) # success_notifiers = list(base_notifiers.filter(Q(project_notifications_for_success__in=self) | # Q(organization_notifications_for_success__in=self.organization))) # any_notifiers = list(base_notifiers.filter(Q(project_notifications_for_any__in=self) | # Q(organization_notifications_for_any__in=self.organization))) - error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_errors=self)) - success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_success=self)) - any_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_any=self)) + error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_errors=self)) + success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_success=self)) + any_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_any=self)) return dict(error=error_notifiers, success=success_notifiers, any=any_notifiers) def get_absolute_url(self): diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index c6ea2b082b..986be923fb 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -304,7 +304,7 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio Return notifiers relevant to this Unified Job Template ''' # NOTE: Derived classes should implement - return NotificationTemplate.objects.none() + return Notifier.objects.none() def create_unified_job(self, **kwargs): ''' diff --git a/awx/main/notifications/webhook_backend.py b/awx/main/notifications/webhook_backend.py index ffef641198..5bdbff0e02 100644 --- a/awx/main/notifications/webhook_backend.py +++ b/awx/main/notifications/webhook_backend.py @@ -17,12 +17,14 @@ class WebhookBackend(BaseEmailBackend): sender_parameter = None def __init__(self, headers, fail_silently=False, **kwargs): + self.headers = headers super(WebhookBackend, self).__init__(fail_silently=fail_silently) def send_messages(self, messages): sent_messages = 0 for m in messages: r = requests.post("{}".format(m.recipients()[0]), + data=m.body, headers=self.headers) if r.status_code >= 400: logger.error("Error sending notification webhook: {}".format(r.text)) diff --git a/awx/main/signals.py b/awx/main/signals.py index f4d0014905..29c5c7d016 100644 --- a/awx/main/signals.py +++ b/awx/main/signals.py @@ -307,7 +307,7 @@ model_serializer_mapping = { Job: JobSerializer, AdHocCommand: AdHocCommandSerializer, TowerSettings: TowerSettingsSerializer, - NotificationTemplate: NotificationTemplateSerializer, + Notifier: NotifierSerializer, Notification: NotificationSerializer, }