Re-wirte ad-hoc notification templates to organization to match docs

This commit is contained in:
Matthew Jones
2017-01-11 11:25:08 -05:00
parent 8796af7e28
commit 23f4898eea

View File

@@ -20,7 +20,7 @@ from django.core.urlresolvers import reverse
# AWX # AWX
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 JobNotificationMixin from awx.main.models.notifications import JobNotificationMixin, NotificationTemplate
from awx.main.fields import JSONField from awx.main.fields import JSONField
logger = logging.getLogger('awx.main.models.ad_hoc_commands') logger = logging.getLogger('awx.main.models.ad_hoc_commands')
@@ -157,18 +157,20 @@ class AdHocCommand(UnifiedJob, JobNotificationMixin):
@property @property
def notification_templates(self): def notification_templates(self):
all_inventory_sources = set() all_orgs = set()
for h in self.hosts.all(): for h in self.hosts.all():
for invsrc in h.inventory_sources.all(): all_orgs.add(h.inventory.organization)
all_inventory_sources.add(invsrc)
active_templates = dict(error=set(), active_templates = dict(error=set(),
success=set(), success=set(),
any=set()) any=set())
for invsrc in all_inventory_sources: base_notification_templates = NotificationTemplate.objects
notifications_dict = invsrc.notification_templates for org in all_orgs:
for notification_type in active_templates.keys(): for templ in base_notification_templates.filter(organization_notification_templates_for_errors=org):
for templ in notifications_dict[notification_type]: active_templates['error'].add(templ)
active_templates[notification_type].add(templ) for templ in base_notification_templates.filter(organization_notification_templates_for_success=org):
active_templates['success'].add(templ)
for templ in base_notification_templates.filter(organization_notification_templates_for_any=org):
active_templates['any'].add(templ)
active_templates['error'] = list(active_templates['error']) active_templates['error'] = list(active_templates['error'])
active_templates['any'] = list(active_templates['any']) active_templates['any'] = list(active_templates['any'])
active_templates['success'] = list(active_templates['success']) active_templates['success'] = list(active_templates['success'])