From c427ebf8b59f4caab63f49c9e3fc7135730420b1 Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Tue, 26 Sep 2017 12:45:00 -0500
Subject: [PATCH] adding safeguard which has all alerts (phone, email, XMPP)
 only turned on for production servers

---
 gracedb/alert.py       | 21 +++++++++++++++++----
 settings/base.py       |  6 ++++--
 settings/production.py |  5 +++++
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/gracedb/alert.py b/gracedb/alert.py
index 4626a86cc..95fb51402 100644
--- a/gracedb/alert.py
+++ b/gracedb/alert.py
@@ -147,7 +147,7 @@ def prepareSummary(event):
 def issueAlertForUpdate(event, description, doxmpp, filename="", serialized_object=None):
     if doxmpp:
         issueXMPPAlert(event, filename, "update", description, serialized_object)
-    # XXX No emails for this.  Argh.
+    # XXX No emails or phone calls for this.  Argh.
 
 # The only kind of serialized object relevant for a Label is an event.
 def issueAlertForLabel(event, label, doxmpp, serialized_event=None, event_url=None):
@@ -199,18 +199,24 @@ def issueAlertForLabel(event, label, doxmpp, serialized_event=None, event_url=No
         toaddresses =  []
         bccaddresses = profileRecips
 
-    if toaddresses or bccaddresses:
+    if settings.SEND_EMAIL_ALERTS and (toaddresses or bccaddresses):
         if not toaddresses:
             toaddresses = ["(undisclosed recipients)"]
         email = EmailMessage(subject, message, fromaddress, toaddresses, bccaddresses)
         email.send()
 
     # Make phone calls.
-    if phoneRecips:
+    if settings.SEND_PHONE_ALERTS and phoneRecips:
         make_twilio_calls(event, phoneRecips, "label", label=label)
 
 def issueEmailAlert(event, event_url):
 
+    # Check settings switch for turning off email alerts
+    if not settings.SEND_EMAIL_ALERTS:
+        log.debug(("Email alert for event {gid} not sent because email alerts "
+            "are turned off").format(gid=event.graceid()))
+        return
+
     # The right way of doing this is to make the email alerts filter-able
     # by search. But this is a low priority dev task. For now, we simply 
     # short-circuit in case this is an MDC event.
@@ -264,13 +270,20 @@ Event Summary:
     email.send()
 
 def issuePhoneAlert(event):
+
+    # Check settings switch for turning off phone alerts
+    if not settings.SEND_PHONE_ALERTS:
+        log.debug(("Phone alert for event {gid} not sent because phone alerts "
+            "are turned off").format(gid=event.graceid()))
+        return
+
     # The right way of doing this is to make the email alerts filter-able
     # by search. But this is a low priority dev task. For now, we simply 
     # short-circuit in case this is an MDC event.
     if event.search and event.search.name == 'MDC':
         return
 
-    # Gather Recipients
+    # Gather recipients
     phoneRecips = []
     if event.group.name != 'Test':
         pipeline = event.pipeline
diff --git a/settings/base.py b/settings/base.py
index b780f1ece..bfc4fd4d2 100644
--- a/settings/base.py
+++ b/settings/base.py
@@ -40,8 +40,10 @@ ALLOWED_HOSTS = ['localhost', '127.0.0.1', SERVER_FQDN,
     '{0}.ligo.org'.format(socket.gethostname())]
 
 # LVAlert and LVAlert Overseer settings ---------------------------------------
-# Set to False to prevent XMPP alerts from being sent out.
-SEND_XMPP_ALERTS = True
+# Switches which control whether alerts are sent out
+SEND_XMPP_ALERTS = False
+SEND_PHONE_ALERTS = False
+SEND_EMAIL_ALERTS = False
 # Use LVAlert Overseer?
 USE_LVALERT_OVERSEER = True
 # LVAlert servers
diff --git a/settings/production.py b/settings/production.py
index 92401b60b..f5d976d50 100644
--- a/settings/production.py
+++ b/settings/production.py
@@ -15,3 +15,8 @@ ALERT_XMPP_SERVERS = ["lvalert.cgca.uwm.edu"]
 LVALERT_OVERSEER_PORTS = {
     "lvalert.cgca.uwm.edu": 8000,
 }
+
+# Turn on alerts
+SEND_XMPP_ALERTS = True
+SEND_PHONE_ALERTS = True
+SEND_EMAIL_ALERTS = True
-- 
GitLab