Refactor NotificationTemplate to Notifier

This commit is contained in:
Matthew Jones
2016-02-17 15:18:18 +00:00
parent 9d6739045a
commit dde70dafec
15 changed files with 145 additions and 141 deletions

View File

@@ -12,7 +12,7 @@ from rest_framework import serializers
from rest_framework.request import clone_request from rest_framework.request import clone_request
# Ansible Tower # Ansible Tower
from awx.main.models import InventorySource, NotificationTemplate from awx.main.models import InventorySource, Notifier
class Metadata(metadata.SimpleMetadata): class Metadata(metadata.SimpleMetadata):
@@ -58,7 +58,7 @@ class Metadata(metadata.SimpleMetadata):
# Special handling of notification configuration where the required properties # Special handling of notification configuration where the required properties
# are conditional on the type selected. # are conditional on the type selected.
if field.field_name == 'notification_configuration': 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 field_info[notification_type_name] = notification_type_class.init_parameters
# Update type of fields returned... # Update type of fields returned...

View File

@@ -769,9 +769,9 @@ class OrganizationSerializer(BaseSerializer):
teams = reverse('api:organization_teams_list', args=(obj.pk,)), teams = reverse('api:organization_teams_list', args=(obj.pk,)),
activity_stream = reverse('api:organization_activity_stream_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 = reverse('api:organization_notifiers_list', args=(obj.pk,)),
notifiers_any = reverse('api:organization_notifications_any_list', args=(obj.pk,)), notifiers_any = reverse('api:organization_notifiers_any_list', args=(obj.pk,)),
notifiers_success = reverse('api:organization_notifications_success_list', args=(obj.pk,)), notifiers_success = reverse('api:organization_notifiers_success_list', args=(obj.pk,)),
notifiers_error = reverse('api:organization_notifications_error_list', args=(obj.pk,)), notifiers_error = reverse('api:organization_notifiers_error_list', args=(obj.pk,)),
)) ))
return res return res
@@ -851,9 +851,9 @@ class ProjectSerializer(UnifiedJobTemplateSerializer, ProjectOptionsSerializer):
project_updates = reverse('api:project_updates_list', args=(obj.pk,)), project_updates = reverse('api:project_updates_list', args=(obj.pk,)),
schedules = reverse('api:project_schedules_list', args=(obj.pk,)), schedules = reverse('api:project_schedules_list', args=(obj.pk,)),
activity_stream = reverse('api:project_activity_stream_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_any = reverse('api:project_notifiers_any_list', args=(obj.pk,)),
notifiers_success = reverse('api:project_notifications_success_list', args=(obj.pk,)), notifiers_success = reverse('api:project_notifiers_success_list', args=(obj.pk,)),
notifiers_error = reverse('api:project_notifications_error_list', args=(obj.pk,)), notifiers_error = reverse('api:project_notifiers_error_list', args=(obj.pk,)),
)) ))
# Backwards compatibility. # Backwards compatibility.
if obj.current_update: if obj.current_update:
@@ -1298,9 +1298,9 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt
activity_stream = reverse('api:inventory_activity_stream_list', args=(obj.pk,)), activity_stream = reverse('api:inventory_activity_stream_list', args=(obj.pk,)),
hosts = reverse('api:inventory_source_hosts_list', args=(obj.pk,)), hosts = reverse('api:inventory_source_hosts_list', args=(obj.pk,)),
groups = reverse('api:inventory_source_groups_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_any = reverse('api:inventory_source_notifiers_any_list', args=(obj.pk,)),
notifiers_success = reverse('api:inventory_source_notifications_success_list', args=(obj.pk,)), notifiers_success = reverse('api:inventory_source_notifiers_success_list', args=(obj.pk,)),
notifiers_error = reverse('api:inventory_source_notifications_error_list', args=(obj.pk,)), notifiers_error = reverse('api:inventory_source_notifiers_error_list', args=(obj.pk,)),
)) ))
if obj.inventory and obj.inventory.active: if obj.inventory and obj.inventory.active:
res['inventory'] = reverse('api:inventory_detail', args=(obj.inventory.pk,)) 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,)), schedules = reverse('api:job_template_schedules_list', args=(obj.pk,)),
activity_stream = reverse('api:job_template_activity_stream_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,)), launch = reverse('api:job_template_launch', args=(obj.pk,)),
notifiers_any = reverse('api:job_template_notifications_any_list', args=(obj.pk,)), notifiers_any = reverse('api:job_template_notifiers_any_list', args=(obj.pk,)),
notifiers_success = reverse('api:job_template_notifications_success_list', args=(obj.pk,)), notifiers_success = reverse('api:job_template_notifiers_success_list', args=(obj.pk,)),
notifiers_error = reverse('api:job_template_notifications_error_list', args=(obj.pk,)), notifiers_error = reverse('api:job_template_notifiers_error_list', args=(obj.pk,)),
)) ))
if obj.host_config_key: if obj.host_config_key:
res['callback'] = reverse('api:job_template_callback', args=(obj.pk,)) res['callback'] = reverse('api:job_template_callback', args=(obj.pk,))
@@ -2053,22 +2053,24 @@ class JobLaunchSerializer(BaseSerializer):
attrs = super(JobLaunchSerializer, self).validate(attrs) attrs = super(JobLaunchSerializer, self).validate(attrs)
return attrs return attrs
class NotificationTemplateSerializer(BaseSerializer): class NotifierSerializer(BaseSerializer):
class Meta: class Meta:
model = NotificationTemplate model = Notifier
fields = ('*', 'organization', 'notification_type', 'notification_configuration') fields = ('*', 'organization', 'notification_type', 'notification_configuration')
def get_related(self, obj): def get_related(self, obj):
res = super(NotificationTemplateSerializer, self).get_related(obj) res = super(NotifierSerializer, self).get_related(obj)
res.update(dict( res.update(dict(
test = reverse('api:notification_template_test', args=(obj.pk,)), test = reverse('api:notifier_test', args=(obj.pk,)),
notifications = reverse('api:notification_template_notification_list', 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 return res
def validate(self, attrs): 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 = [] missing_fields = []
for field in notification_class.init_parameters: for field in notification_class.init_parameters:
if field not in attrs['notification_configuration']: if field not in attrs['notification_configuration']:
@@ -2088,7 +2090,7 @@ class NotificationSerializer(BaseSerializer):
def get_related(self, obj): def get_related(self, obj):
res = super(NotificationSerializer, self).get_related(obj) res = super(NotificationSerializer, self).get_related(obj)
res.update(dict( 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 return res

View File

@@ -21,9 +21,9 @@ organization_urls = patterns('awx.api.views',
url(r'^(?P<pk>[0-9]+)/teams/$', 'organization_teams_list'), url(r'^(?P<pk>[0-9]+)/teams/$', 'organization_teams_list'),
url(r'^(?P<pk>[0-9]+)/activity_stream/$', 'organization_activity_stream_list'), url(r'^(?P<pk>[0-9]+)/activity_stream/$', 'organization_activity_stream_list'),
url(r'^(?P<pk>[0-9]+)/notifiers/$', 'organization_notifiers_list'), url(r'^(?P<pk>[0-9]+)/notifiers/$', 'organization_notifiers_list'),
url(r'^(?P<pk>[0-9]+)/notifications_any/$', 'organization_notifications_any_list'), url(r'^(?P<pk>[0-9]+)/notifiers_any/$', 'organization_notifiers_any_list'),
url(r'^(?P<pk>[0-9]+)/notifications_error/$', 'organization_notifications_error_list'), url(r'^(?P<pk>[0-9]+)/notifiers_error/$', 'organization_notifiers_error_list'),
url(r'^(?P<pk>[0-9]+)/notifications_success/$', 'organization_notifications_success_list'), url(r'^(?P<pk>[0-9]+)/notifiers_success/$', 'organization_notifiers_success_list'),
) )
user_urls = patterns('awx.api.views', user_urls = patterns('awx.api.views',
@@ -48,9 +48,9 @@ project_urls = patterns('awx.api.views',
url(r'^(?P<pk>[0-9]+)/project_updates/$', 'project_updates_list'), url(r'^(?P<pk>[0-9]+)/project_updates/$', 'project_updates_list'),
url(r'^(?P<pk>[0-9]+)/activity_stream/$', 'project_activity_stream_list'), url(r'^(?P<pk>[0-9]+)/activity_stream/$', 'project_activity_stream_list'),
url(r'^(?P<pk>[0-9]+)/schedules/$', 'project_schedules_list'), url(r'^(?P<pk>[0-9]+)/schedules/$', 'project_schedules_list'),
url(r'^(?P<pk>[0-9]+)/notifications_any/$', 'project_notifications_any_list'), url(r'^(?P<pk>[0-9]+)/notifiers_any/$', 'project_notifiers_any_list'),
url(r'^(?P<pk>[0-9]+)/notifications_error/$', 'project_notifications_error_list'), url(r'^(?P<pk>[0-9]+)/notifiers_error/$', 'project_notifiers_error_list'),
url(r'^(?P<pk>[0-9]+)/notifications_success/$', 'project_notifications_success_list'), url(r'^(?P<pk>[0-9]+)/notifiers_success/$', 'project_notifiers_success_list'),
) )
project_update_urls = patterns('awx.api.views', project_update_urls = patterns('awx.api.views',
@@ -128,9 +128,9 @@ inventory_source_urls = patterns('awx.api.views',
url(r'^(?P<pk>[0-9]+)/schedules/$', 'inventory_source_schedules_list'), url(r'^(?P<pk>[0-9]+)/schedules/$', 'inventory_source_schedules_list'),
url(r'^(?P<pk>[0-9]+)/groups/$', 'inventory_source_groups_list'), url(r'^(?P<pk>[0-9]+)/groups/$', 'inventory_source_groups_list'),
url(r'^(?P<pk>[0-9]+)/hosts/$', 'inventory_source_hosts_list'), url(r'^(?P<pk>[0-9]+)/hosts/$', 'inventory_source_hosts_list'),
url(r'^(?P<pk>[0-9]+)/notifications_any/$', 'inventory_source_notifications_any_list'), url(r'^(?P<pk>[0-9]+)/notifiers_any/$', 'inventory_source_notifiers_any_list'),
url(r'^(?P<pk>[0-9]+)/notifications_error/$', 'inventory_source_notifications_error_list'), url(r'^(?P<pk>[0-9]+)/notifiers_error/$', 'inventory_source_notifiers_error_list'),
url(r'^(?P<pk>[0-9]+)/notifications_success/$', 'inventory_source_notifications_success_list'), url(r'^(?P<pk>[0-9]+)/notifiers_success/$', 'inventory_source_notifiers_success_list'),
) )
inventory_update_urls = patterns('awx.api.views', inventory_update_urls = patterns('awx.api.views',
@@ -165,9 +165,9 @@ job_template_urls = patterns('awx.api.views',
url(r'^(?P<pk>[0-9]+)/schedules/$', 'job_template_schedules_list'), url(r'^(?P<pk>[0-9]+)/schedules/$', 'job_template_schedules_list'),
url(r'^(?P<pk>[0-9]+)/survey_spec/$', 'job_template_survey_spec'), url(r'^(?P<pk>[0-9]+)/survey_spec/$', 'job_template_survey_spec'),
url(r'^(?P<pk>[0-9]+)/activity_stream/$', 'job_template_activity_stream_list'), url(r'^(?P<pk>[0-9]+)/activity_stream/$', 'job_template_activity_stream_list'),
url(r'^(?P<pk>[0-9]+)/notifications_any/$', 'job_template_notifications_any_list'), url(r'^(?P<pk>[0-9]+)/notifiers_any/$', 'job_template_notifiers_any_list'),
url(r'^(?P<pk>[0-9]+)/notifications_error/$', 'job_template_notifications_error_list'), url(r'^(?P<pk>[0-9]+)/notifiers_error/$', 'job_template_notifiers_error_list'),
url(r'^(?P<pk>[0-9]+)/notifications_success/$', 'job_template_notifications_success_list'), url(r'^(?P<pk>[0-9]+)/notifiers_success/$', 'job_template_notifiers_success_list'),
) )
job_urls = patterns('awx.api.views', job_urls = patterns('awx.api.views',
@@ -225,11 +225,11 @@ system_job_urls = patterns('awx.api.views',
url(r'^(?P<pk>[0-9]+)/cancel/$', 'system_job_cancel'), url(r'^(?P<pk>[0-9]+)/cancel/$', 'system_job_cancel'),
) )
notification_template_urls = patterns('awx.api.views', notifier_urls = patterns('awx.api.views',
url(r'^$', 'notification_template_list'), url(r'^$', 'notifier_list'),
url(r'^(?P<pk>[0-9]+)/$', 'notification_template_detail'), url(r'^(?P<pk>[0-9]+)/$', 'notifier_detail'),
url(r'^(?P<pk>[0-9]+)/test/$', 'notification_template_test'), url(r'^(?P<pk>[0-9]+)/test/$', 'notifier_test'),
url(r'^(?P<pk>[0-9]+)/notifications/$', 'notification_template_notification_list'), url(r'^(?P<pk>[0-9]+)/notifications/$', 'notifier_notification_list'),
) )
notification_urls = patterns('awx.api.views', 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'^ad_hoc_command_events/', include(ad_hoc_command_event_urls)),
url(r'^system_job_templates/', include(system_job_template_urls)), url(r'^system_job_templates/', include(system_job_template_urls)),
url(r'^system_jobs/', include(system_job_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'^notifications/', include(notification_urls)),
url(r'^unified_job_templates/$', 'unified_job_template_list'), url(r'^unified_job_templates/$', 'unified_job_template_list'),
url(r'^unified_jobs/$', 'unified_job_list'), url(r'^unified_jobs/$', 'unified_job_list'),

View File

@@ -135,7 +135,7 @@ class ApiV1RootView(APIView):
data['system_job_templates'] = reverse('api:system_job_template_list') data['system_job_templates'] = reverse('api:system_job_template_list')
data['system_jobs'] = reverse('api:system_job_list') data['system_jobs'] = reverse('api:system_job_list')
data['schedules'] = reverse('api:schedule_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['notifications'] = reverse('api:notification_list')
data['unified_job_templates'] = reverse('api:unified_job_template_list') data['unified_job_templates'] = reverse('api:unified_job_template_list')
data['unified_jobs'] = reverse('api:unified_job_list') data['unified_jobs'] = reverse('api:unified_job_list')
@@ -687,32 +687,32 @@ class OrganizationActivityStreamList(SubListAPIView):
class OrganizationNotifiersList(SubListCreateAttachDetachAPIView): class OrganizationNotifiersList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = Organization parent_model = Organization
relationship = 'notification_templates' relationship = 'notifiers'
parent_key = 'organization' parent_key = 'organization'
class OrganizationNotificationsAnyList(SubListCreateAttachDetachAPIView): class OrganizationNotifiersAnyList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = Organization parent_model = Organization
relationship = 'notification_any' relationship = 'notifiers_any'
class OrganizationNotificationsErrorList(SubListCreateAttachDetachAPIView): class OrganizationNotifiersErrorList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = Organization parent_model = Organization
relationship = 'notification_erros' relationship = 'notifiers_error'
class OrganizationNotificationsSuccessList(SubListCreateAttachDetachAPIView): class OrganizationNotifiersSuccessList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = Organization parent_model = Organization
relationship = 'notification_success' relationship = 'notifiers_success'
class TeamList(ListCreateAPIView): class TeamList(ListCreateAPIView):
@@ -879,26 +879,26 @@ class ProjectActivityStreamList(SubListAPIView):
return qs.filter(project=parent) return qs.filter(project=parent)
return qs.filter(Q(project=parent) | Q(credential__in=parent.credential)) return qs.filter(Q(project=parent) | Q(credential__in=parent.credential))
class ProjectNotificationsAnyList(SubListCreateAttachDetachAPIView): class ProjectNotifiersAnyList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = Project parent_model = Project
relationship = 'notification_any' relationship = 'notifiers_any'
class ProjectNotificationsErrorList(SubListCreateAttachDetachAPIView): class ProjectNotifiersErrorList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = Project parent_model = Project
relationship = 'notification_errors' relationship = 'notifiers_error'
class ProjectNotificationsSuccessList(SubListCreateAttachDetachAPIView): class ProjectNotifiersSuccessList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = Project parent_model = Project
relationship = 'notification_success' relationship = 'notifiers_success'
class ProjectUpdatesList(SubListAPIView): class ProjectUpdatesList(SubListAPIView):
@@ -1781,26 +1781,26 @@ class InventorySourceActivityStreamList(SubListAPIView):
# Okay, let it through. # Okay, let it through.
return super(type(self), self).get(request, *args, **kwargs) return super(type(self), self).get(request, *args, **kwargs)
class InventorySourceNotificationsAnyList(SubListCreateAttachDetachAPIView): class InventorySourceNotifiersAnyList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = InventorySource parent_model = InventorySource
relationship = 'notification_any' relationship = 'notifiers_any'
class InventorySourceNotificationsErrorList(SubListCreateAttachDetachAPIView): class InventorySourceNotifiersErrorList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = InventorySource parent_model = InventorySource
relationship = 'notification_errors' relationship = 'notifiers_error'
class InventorySourceNotificationsSuccessList(SubListCreateAttachDetachAPIView): class InventorySourceNotifiersSuccessList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = InventorySource parent_model = InventorySource
relationship = 'notification_success' relationship = 'notifiers_success'
class InventorySourceHostsList(SubListAPIView): class InventorySourceHostsList(SubListAPIView):
@@ -2027,26 +2027,26 @@ class JobTemplateActivityStreamList(SubListAPIView):
# Okay, let it through. # Okay, let it through.
return super(type(self), self).get(request, *args, **kwargs) return super(type(self), self).get(request, *args, **kwargs)
class JobTemplateNotificationsAnyList(SubListCreateAttachDetachAPIView): class JobTemplateNotifiersAnyList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = JobTemplate parent_model = JobTemplate
relationship = 'notification_any' relationship = 'notifiers_any'
class JobTemplateNotificationsErrorList(SubListCreateAttachDetachAPIView): class JobTemplateNotifiersErrorList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = JobTemplate parent_model = JobTemplate
relationship = 'notification_errors' relationship = 'notifiers_error'
class JobTemplateNotificationsSuccessList(SubListCreateAttachDetachAPIView): class JobTemplateNotifiersSuccessList(SubListCreateAttachDetachAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
parent_model = JobTemplate parent_model = JobTemplate
relationship = 'notification_success' relationship = 'notifiers_success'
class JobTemplateCallback(GenericAPIView): class JobTemplateCallback(GenericAPIView):
@@ -3032,22 +3032,22 @@ class AdHocCommandStdout(UnifiedJobStdout):
model = AdHocCommand model = AdHocCommand
new_in_220 = True new_in_220 = True
class NotificationTemplateList(ListCreateAPIView): class NotifierList(ListCreateAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
new_in_300 = True new_in_300 = True
class NotificationTemplateDetail(RetrieveUpdateDestroyAPIView): class NotifierDetail(RetrieveUpdateDestroyAPIView):
model = NotificationTemplate model = Notifier
serializer_class = NotificationTemplateSerializer serializer_class = NotifierSerializer
new_in_300 = True new_in_300 = True
class NotificationTemplateTest(GenericAPIView): class NotifierTest(GenericAPIView):
view_name = 'Notification Template Test' view_name = 'Notifier Test'
model = NotificationTemplate model = Notifier
serializer_class = EmptySerializer serializer_class = EmptySerializer
new_in_300 = True new_in_300 = True
@@ -3063,11 +3063,11 @@ class NotificationTemplateTest(GenericAPIView):
headers=headers, headers=headers,
status=status.HTTP_202_ACCEPTED) status=status.HTTP_202_ACCEPTED)
class NotificationTemplateNotificationList(SubListAPIView): class NotifierNotificationList(SubListAPIView):
model = Notification model = Notification
serializer_class = NotificationSerializer serializer_class = NotificationSerializer
parent_model = NotificationTemplate parent_model = Notifier
relationship = 'notifications' relationship = 'notifications'
parent_key = 'notifier' parent_key = 'notifier'
@@ -3079,7 +3079,7 @@ class NotificationList(ListAPIView):
class NotificationDetail(RetrieveAPIView): class NotificationDetail(RetrieveAPIView):
model = NotificationTemplate model = Notification
serializer_class = NotificationSerializer serializer_class = NotificationSerializer
new_in_300 = True new_in_300 = True

View File

@@ -1484,11 +1484,11 @@ class ScheduleAccess(BaseAccess):
else: else:
return False 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): def get_queryset(self):
qs = self.model.objects.filter(active=True).distinct() qs = self.model.objects.filter(active=True).distinct()
@@ -1708,5 +1708,5 @@ register_access(UnifiedJob, UnifiedJobAccess)
register_access(ActivityStream, ActivityStreamAccess) register_access(ActivityStream, ActivityStreamAccess)
register_access(CustomInventoryScript, CustomInventoryScriptAccess) register_access(CustomInventoryScript, CustomInventoryScriptAccess)
register_access(TowerSettings, TowerSettingsAccess) register_access(TowerSettings, TowerSettingsAccess)
register_access(NotificationTemplate, NotificationTemplateAccess) register_access(Notifier, NotifierAccess)
register_access(Notification, NotificationAccess) register_access(Notification, NotificationAccess)

View File

@@ -61,5 +61,5 @@ activity_stream_registrar.connect(AdHocCommand)
activity_stream_registrar.connect(Schedule) activity_stream_registrar.connect(Schedule)
activity_stream_registrar.connect(CustomInventoryScript) activity_stream_registrar.connect(CustomInventoryScript)
activity_stream_registrar.connect(TowerSettings) activity_stream_registrar.connect(TowerSettings)
activity_stream_registrar.connect(NotificationTemplate) activity_stream_registrar.connect(Notifier)
activity_stream_registrar.connect(Notification) activity_stream_registrar.connect(Notification)

View File

@@ -53,7 +53,7 @@ class ActivityStream(models.Model):
ad_hoc_command = models.ManyToManyField("AdHocCommand", blank=True) ad_hoc_command = models.ManyToManyField("AdHocCommand", blank=True)
schedule = models.ManyToManyField("Schedule", blank=True) schedule = models.ManyToManyField("Schedule", blank=True)
custom_inventory_script = models.ManyToManyField("CustomInventoryScript", 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) notification = models.ManyToManyField("Notification", blank=True)
def get_absolute_url(self): def get_absolute_url(self):

View File

@@ -343,20 +343,20 @@ class NotificationFieldsModel(BaseModel):
class Meta: class Meta:
abstract = True abstract = True
notification_errors = models.ManyToManyField( notifiers_error = models.ManyToManyField(
"NotificationTemplate", "Notifier",
blank=True, blank=True,
related_name='%(class)s_notifications_for_errors' related_name='%(class)s_notifiers_for_errors'
) )
notification_success = models.ManyToManyField( notifiers_success = models.ManyToManyField(
"NotificationTemplate", "Notifier",
blank=True, blank=True,
related_name='%(class)s_notifications_for_success' related_name='%(class)s_notifiers_for_success'
) )
notification_any = models.ManyToManyField( notifiers_any = models.ManyToManyField(
"NotificationTemplate", "Notifier",
blank=True, blank=True,
related_name='%(class)s_notifications_for_any' related_name='%(class)s_notifiers_for_any'
) )

View File

@@ -23,7 +23,7 @@ from awx.main.managers import HostManager
from awx.main.models.base import * # noqa from awx.main.models.base import * # noqa
from awx.main.models.jobs import Job from awx.main.models.jobs import Job
from awx.main.models.unified_jobs 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 ignore_inventory_computed_fields, _inventory_updates from awx.main.utils import ignore_inventory_computed_fields, _inventory_updates
__all__ = ['Inventory', 'Host', 'Group', 'InventorySource', 'InventoryUpdate', 'CustomInventoryScript'] __all__ = ['Inventory', 'Host', 'Group', 'InventorySource', 'InventoryUpdate', 'CustomInventoryScript']
@@ -1184,10 +1184,10 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions):
@property @property
def notifiers(self): def notifiers(self):
# Return all notifiers defined on the Project, and on the Organization for each trigger type # Return all notifiers defined on the Project, and on the Organization for each trigger type
base_notifiers = NotificationTemplate.objects.filter(active=True) base_notifiers = Notifier.objects.filter(active=True)
error_notifiers = list(base_notifiers.filter(organization_notifications_for_errors__in=self)) error_notifiers = list(base_notifiers.filter(organization_notifiers_for_errors__in=self))
success_notifiers = list(base_notifiers.filter(organization_notifications_for_success__in=self)) success_notifiers = list(base_notifiers.filter(organization_notifiers_for_success__in=self))
any_notifiers = list(base_notifiers.filter(organization_notifications_for_any__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) return dict(error=error_notifiers, success=success_notifiers, any=any_notifiers)
def clean_source(self): def clean_source(self):

View File

@@ -22,7 +22,7 @@ from jsonfield import JSONField
from awx.main.constants import CLOUD_PROVIDERS from awx.main.constants import CLOUD_PROVIDERS
from awx.main.models.base import * # noqa from awx.main.models.base import * # noqa
from awx.main.models.unified_jobs 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 decrypt_field, ignore_inventory_computed_fields
from awx.main.utils import emit_websocket_notification from awx.main.utils import emit_websocket_notification
from awx.main.redact import PlainTextCleaner 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 # 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 # TODO: Currently there is no org fk on project so this will need to be added once that is
# available after the rbac pr # 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(unifiedjobtemplate_notifications_for_errors__in=[self, self.project])) error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_errors__in=[self, self.project]))
success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_success__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_notifications_for_any__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) return dict(error=error_notifiers, success=success_notifiers, any=any_notifiers)
class Job(UnifiedJob, JobOptions): class Job(UnifiedJob, JobOptions):

View File

@@ -23,9 +23,9 @@ from jsonfield import JSONField
logger = logging.getLogger('awx.main.models.notifications') logger = logging.getLogger('awx.main.models.notifications')
__all__ = ['NotificationTemplate', 'Notification'] __all__ = ['Notifier', 'Notification']
class NotificationTemplate(CommonModel): class Notifier(CommonModel):
NOTIFICATION_TYPES = [('email', _('Email'), CustomEmailBackend), NOTIFICATION_TYPES = [('email', _('Email'), CustomEmailBackend),
('slack', _('Slack'), SlackBackend), ('slack', _('Slack'), SlackBackend),
@@ -45,7 +45,7 @@ class NotificationTemplate(CommonModel):
blank=False, blank=False,
null=True, null=True,
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
related_name='notification_templates', related_name='notifiers',
) )
notification_type = models.CharField( notification_type = models.CharField(
@@ -56,7 +56,7 @@ class NotificationTemplate(CommonModel):
notification_configuration = JSONField(blank=False) notification_configuration = JSONField(blank=False)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('api:notification_template_detail', args=(self.pk,)) return reverse('api:notifier_detail', args=(self.pk,))
@property @property
def notification_class(self): def notification_class(self):
@@ -100,7 +100,7 @@ class Notification(CreatedModifiedModel):
ordering = ('pk',) ordering = ('pk',)
notifier = models.ForeignKey( notifier = models.ForeignKey(
'NotificationTemplate', 'Notifier',
related_name='notifications', related_name='notifications',
on_delete=models.CASCADE, on_delete=models.CASCADE,
editable=False editable=False
@@ -122,7 +122,7 @@ class Notification(CreatedModifiedModel):
) )
notification_type = models.CharField( notification_type = models.CharField(
max_length = 32, max_length = 32,
choices=NotificationTemplate.NOTIFICATION_TYPE_CHOICES, choices=Notifier.NOTIFICATION_TYPE_CHOICES,
) )
recipients = models.TextField( recipients = models.TextField(
blank=True, blank=True,

View File

@@ -21,7 +21,7 @@ from django.utils.timezone import now, make_aware, get_default_timezone
from awx.lib.compat import slugify from awx.lib.compat import slugify
from awx.main.models.base import * # noqa from awx.main.models.base import * # noqa
from awx.main.models.jobs import Job 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.models.unified_jobs import * # noqa
from awx.main.utils import update_scm_url 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 # 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 # 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 # 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) | # error_notifiers = list(base_notifiers.filter(Q(project_notifications_for_errors__in=self) |
# Q(organization_notifications_for_errors__in=self.organization))) # Q(organization_notifications_for_errors__in=self.organization)))
# success_notifiers = list(base_notifiers.filter(Q(project_notifications_for_success__in=self) | # success_notifiers = list(base_notifiers.filter(Q(project_notifications_for_success__in=self) |
# Q(organization_notifications_for_success__in=self.organization))) # Q(organization_notifications_for_success__in=self.organization)))
# any_notifiers = list(base_notifiers.filter(Q(project_notifications_for_any__in=self) | # any_notifiers = list(base_notifiers.filter(Q(project_notifications_for_any__in=self) |
# Q(organization_notifications_for_any__in=self.organization))) # Q(organization_notifications_for_any__in=self.organization)))
error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_errors=self)) error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_errors=self))
success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_success=self)) success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_success=self))
any_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifications_for_any=self)) any_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_any=self))
return dict(error=error_notifiers, success=success_notifiers, any=any_notifiers) return dict(error=error_notifiers, success=success_notifiers, any=any_notifiers)
def get_absolute_url(self): def get_absolute_url(self):

View File

@@ -304,7 +304,7 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio
Return notifiers relevant to this Unified Job Template Return notifiers relevant to this Unified Job Template
''' '''
# NOTE: Derived classes should implement # NOTE: Derived classes should implement
return NotificationTemplate.objects.none() return Notifier.objects.none()
def create_unified_job(self, **kwargs): def create_unified_job(self, **kwargs):
''' '''

View File

@@ -17,12 +17,14 @@ class WebhookBackend(BaseEmailBackend):
sender_parameter = None sender_parameter = None
def __init__(self, headers, fail_silently=False, **kwargs): def __init__(self, headers, fail_silently=False, **kwargs):
self.headers = headers
super(WebhookBackend, self).__init__(fail_silently=fail_silently) super(WebhookBackend, self).__init__(fail_silently=fail_silently)
def send_messages(self, messages): def send_messages(self, messages):
sent_messages = 0 sent_messages = 0
for m in messages: for m in messages:
r = requests.post("{}".format(m.recipients()[0]), r = requests.post("{}".format(m.recipients()[0]),
data=m.body,
headers=self.headers) headers=self.headers)
if r.status_code >= 400: if r.status_code >= 400:
logger.error("Error sending notification webhook: {}".format(r.text)) logger.error("Error sending notification webhook: {}".format(r.text))

View File

@@ -307,7 +307,7 @@ model_serializer_mapping = {
Job: JobSerializer, Job: JobSerializer,
AdHocCommand: AdHocCommandSerializer, AdHocCommand: AdHocCommandSerializer,
TowerSettings: TowerSettingsSerializer, TowerSettings: TowerSettingsSerializer,
NotificationTemplate: NotificationTemplateSerializer, Notifier: NotifierSerializer,
Notification: NotificationSerializer, Notification: NotificationSerializer,
} }