From f3aa1a3fb6f7e52d6f1cee64246f18c2865a5ad9 Mon Sep 17 00:00:00 2001
From: Branson Stephens <branson.stephens@ligo.org>
Date: Fri, 3 Oct 2014 09:40:45 -0500
Subject: [PATCH] Added a JS button for viewing skymap.json files in the
 skymapViewer hosted externally.

---
 gracedb/views.py                    |  6 +++++
 settings/default.py                 |  2 ++
 templates/gracedb/event_detail.html | 38 +++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)

diff --git a/gracedb/views.py b/gracedb/views.py
index 6fe9636dc..177997b13 100644
--- a/gracedb/views.py
+++ b/gracedb/views.py
@@ -624,7 +624,11 @@ def neighbors(request, graceid, delta1, delta2=None):
         context,
         context_instance=RequestContext(request))
 
+import logging
+
 def view(request, graceid):
+    logger = logging.getLogger(__name__)
+    logger.debug("Hi from inside the view.")
     context = {}
     try:
         a = Event.getByGraceid(graceid)
@@ -639,6 +643,8 @@ def view(request, graceid):
     context['blessed_tags'] = settings.BLESSED_TAGS
     context['single_inspiral_events'] = list(a.singleinspiral_set.all())
     context['neighbor_delta'] = "[%+d,%+d]" % (-5,5)
+    context['SKYMAP_VIEWER_SERVICE_URL'] = settings.SKYMAP_VIEWER_SERVICE_URL
+
     return render_to_response(
         [ 'gracedb/event_detail_{0}.html'.format(a.analysisType),
           'gracedb/event_detail.html'],
diff --git a/settings/default.py b/settings/default.py
index 889fd042f..09eeaa135 100644
--- a/settings/default.py
+++ b/settings/default.py
@@ -40,6 +40,8 @@ XMPP_ALERT_CHANNELS = [
                         'external_grb',
                       ]
 
+SKYMAP_VIEWER_SERVICE_URL = "https://losc-gracedb.ligo.org/cgi-bin/skymapViewer"
+
 BLESSED_TAGS = [
                  'analyst_comments',
                  'psd',
diff --git a/templates/gracedb/event_detail.html b/templates/gracedb/event_detail.html
index 5ea75b0aa..628f103d7 100644
--- a/templates/gracedb/event_detail.html
+++ b/templates/gracedb/event_detail.html
@@ -93,6 +93,10 @@
                     dc += '         <td>{{log.comment|sanitize|escapejs}} \n';
                                {% if log.fileurl %}
                     dc += '             <a href="{{log.fileurl}}">{{log.filename}}</a> \n';
+                                    // If this is a skymap.json file, we want a button.
+                                    {% if log.filename == "skymap.json" %}
+                    dc += '             <button type="button" data-dojo-type="dijit/form/Button" id="sV_button" class="modButtonClass">View in skymapViewer!</button> \n';                                   
+                                    {% endif %}
                                {% endif %}
                     dc += '         </td> \n';
                     dc += '    </tr> \n';
@@ -491,6 +495,28 @@
                 new Tooltip({ connectId: tag.button_id, label: "delete this tag"});
             }
 
+
+            // Handle the post to skymapViewer button.
+            var sV_button = dom.byId("sV_button");
+            if (sV_button) {
+                on(sV_button, "click", function() {
+                    // construct skymap.json url.
+                    var skymap_json_url = "{% url "file" object.graceid "skymap.json" %}"
+                    // fetch JSON content.
+                    dojo.xhrGet({
+                        url: skymap_json_url,
+                        load: function(result) { 
+                            // Find the form and set its value to the appropriate JSON        
+                            sV_form = dom.byId("sV_form");
+                            // Shove the skymap.json contents into the value for the second form field.
+                            sV_form.elements[1].value = result; 
+                            // Submit the form, which takes the user to the skymapViewer server.
+                            sV_form.submit();
+                        }
+                    });
+                });
+            }
+
         });
 
     </script>
@@ -596,6 +622,18 @@
 {% endblock %}
 </div>
 
+{# Enable post to skymap viewer #}
+<div id="sV_form_div" style="display: none">
+
+<!-- <form action="https://losc-gracedb.ligo.org/cgi-bin/skymapViewer" id="sV_form"  method="post"> -->
+<form action="{{ SKYMAP_VIEWER_SERVICE_URL }}" id="sV_form"  method="post">
+    <input type="hidden" name="skymapid" value="{{ object.graceid }}">
+    <!-- <input type="hidden" name="json" value="{{ skymap_json }}"> -->
+    <input type="hidden" name="json" value="blaargh">
+    <input type="submit" value="View in skymapViewer!">
+</form>
+</div>
+
 <!-- Neighbors Support refresh_neighbors() function -->
 <script type="text/javascript">
     var refresh_neighbors= function() { alert("NOT SET YET"); };
-- 
GitLab