main.py 2.54 KB
Newer Older
Tanner Prestegard's avatar
Tanner Prestegard committed
1
2
from __future__ import absolute_import
import logging
3

Tanner Prestegard's avatar
Tanner Prestegard committed
4
from django.conf import settings
5

6
from events.shortcuts import is_event
7
from .email import issue_email_alerts
8
from .phone import issue_phone_alerts
9
from .recipients import ALERT_TYPE_RECIPIENT_GETTERS
10
from .xmpp import issue_xmpp_alerts
11

12

Tanner Prestegard's avatar
Tanner Prestegard committed
13
# Set up logger
14
logger = logging.getLogger(__name__)
15
16


17
def issue_alerts(event_or_superevent, alert_type, serialized_object,
18
    serialized_parent=None, **kwargs):
19
20
21

    # Send XMPP alert
    if settings.SEND_XMPP_ALERTS:
22
        issue_xmpp_alerts(event_or_superevent, alert_type, serialized_object,
23
            serialized_parent=serialized_parent)
24
25
26

    # Below here, we only do processing for email and phone alerts ------------

27
28
    # A few checks on whether we should issue a phone and/or email alert ------
    if not (settings.SEND_EMAIL_ALERTS or settings.SEND_PHONE_ALERTS):
29
30
        return

31
32
33
    # Phone and email alerts are only issued for certain alert types
    # We check this by looking at the keys of ALERT_TYPE_RECIPIENT_GETTERS
    if (alert_type not in ALERT_TYPE_RECIPIENT_GETTERS):
34
35
        return

36
    # Don't send phone or email alerts for MDC or Test cases, or offline
37
    if is_event(event_or_superevent):
38
        # Test/MDC events
39
40
41
42
43
        event = event_or_superevent
        if ((event.search and event.search.name == 'MDC') \
            or event.group.name == 'Test'):
            return

44
        # Offline events
45
46
        if event.offline:
            return
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    else:
        # Test/MDC superevents
        s = event_or_superevent
        if (s.category in [s.__class__.SUPEREVENT_CATEGORY_TEST,
            s.__class__.SUPEREVENT_CATEGORY_MDC]):
            return

        # Superevents with offline preferred events
        if s.preferred_event.offline:
            return

    # Looks like we're going to issue phone and/or email alerts ---------------

    # Get recipient getter class
    rg_class = ALERT_TYPE_RECIPIENT_GETTERS[alert_type]

    # Instantiate recipient getter
    rg = rg_class(event_or_superevent, **kwargs)

    # Get recipients
    email_recipients, phone_recipients = rg.get_recipients()
68

69
70
    # Try to get label explicitly from kwargs
    label = kwargs.get('label', None)
71

72
73
74
75
    # Issue phone alerts
    if settings.SEND_PHONE_ALERTS and phone_recipients.exists():
        issue_phone_alerts(event_or_superevent, alert_type, phone_recipients,
            label=label)
76
77
78
79
80

    # Issue email alerts
    if settings.SEND_EMAIL_ALERTS and email_recipients.exists():
        issue_email_alerts(event_or_superevent, alert_type, email_recipients,
            label=label)