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

@@ -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)

View File

@@ -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)

View File

@@ -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):

View File

@@ -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'
)

View File

@@ -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):

View File

@@ -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):

View File

@@ -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,

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.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):

View File

@@ -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):
'''

View File

@@ -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))

View File

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