From 2cd7586e7a9a52d0135f02d973b67b1e5f612fb1 Mon Sep 17 00:00:00 2001
From: Branson Stephens <branson@gravity.phys.uwm.edu>
Date: Tue, 3 Nov 2015 09:43:17 -0600
Subject: [PATCH] Aligned buildVOEvent with Scott's requirements.

---
 gracedb/buildVOEvent.py | 48 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 45 insertions(+), 3 deletions(-)

diff --git a/gracedb/buildVOEvent.py b/gracedb/buildVOEvent.py
index 407d31e03..c79717045 100755
--- a/gracedb/buildVOEvent.py
+++ b/gracedb/buildVOEvent.py
@@ -41,6 +41,12 @@ def get_url(request, graceid, view_name, file_name=None):
 
 VOEVENT_TYPE_DICT = dict(GraceDBVOEvent.VOEVENT_TYPE_CHOICES)
 
+def get_voevent_type(short_name):
+    for t in GraceDBVOEvent.VOEVENT_TYPE_CHOICES:
+        if short_name in t:
+            return t[1]
+    return None
+
 def buildVOEvent(event, serial_number, voevent_type, request=None, skymap_filename=None,
     skymap_type=None, skymap_image_filename = None, internal=1):
 
@@ -60,7 +66,22 @@ def buildVOEvent(event, serial_number, voevent_type, request=None, skymap_filena
     objid = event.graceid()
 
     # Now build the IVORN. 
-    event_id = "%s-%d-%s" % (objid, serial_number, voevent_type.capitalize())
+    # XXX This will have the string '-Retraction' appended if it is a retraction,
+    # and the voevent_type will refer to the type of the *previous* voevent.
+    # This is highly objectionable.
+    type_string = voevent_type.capitalize()
+    if voevent_type == 'retraction':
+        try:
+            last_voevent = event.voevent_set.order_by('-N')[1] 
+            type_string = get_voevent_type(last_voevent.voevent_type).capitalize()
+            type_string += '-Retraction'
+        except:
+            # XXX Somehow failed to get the previous VOEvent. This is a bad situation.
+            # But we can't just error out, because sending out the retraction is pretty
+            # important. 
+            type_string = 'Preliminary-Retraction'
+        
+    event_id = "%s-%d-%s" % (objid, serial_number, type_string)
     ivorn = settings.SKYALERT_IVORN_PATTERN % event_id
 
     ############ VOEvent header ############################
@@ -138,14 +159,35 @@ def buildVOEvent(event, serial_number, voevent_type, request=None, skymap_filena
         value=objid, 
         Description=["Identifier in GraceDB"]))
 
-    # The alert type
+    # XXX if voevent_type == 'retraction' the AlertType will be the type of the
+    # last VOEvent sent out. This is highly objectionable.
+    alert_type = voevent_type
+
+    if voevent_type == 'retraction':
+        try:
+            last_voevent = event.voevent_set.order_by('-N')[1] 
+            alert_type = get_voevent_type(last_voevent.voevent_type)
+        except:
+            # XXX We have failed to obtain the last voevent for some reason, so 
+            # we don't know what the alert type should be. Let's just set it to
+            # preliminary, since we need to try not to error out of sending the
+            # retraction
+            alert_type = 'preliminary'
+
     w.add_Param(Param(name="AlertType",
         dataType="string",
         ucd="meta.version",
         unit="",
-        value = voevent_type.capitalize(),
+        value = alert_type.capitalize(),
         Description=["VOEvent alert type"]))
 
+    w.add_Param(Param(name="Retraction",
+        dataType="string",
+        ucd="meta.code",
+        unit="",
+        value= "true" if voevent_type == 'retraction' else "false",
+        Description=["Set to true if the event is retracted."]))
+
     # Shib protected event page
     w.add_Param(Param(name="EventPage",
         ucd="meta.ref.url",
-- 
GitLab