[sagenb] 94/157: Use the Jmol script URL, not the request URL

felix salfelder felix-guest at moszumanska.debian.org
Mon Dec 22 16:51:56 UTC 2014


This is an automated email from the git hooks/post-receive script.

felix-guest pushed a commit to branch master
in repository sagenb.

commit a47e9dc4e98b2a4c96384c7258c9292d3f97faa2
Author: Volker Braun <vbraun.name at gmail.com>
Date:   Sun Oct 5 13:00:41 2014 +0100

    Use the Jmol script URL, not the request URL
    
    The jsmol callback url is shared state between all applets on a page,
    so we cannot use it in worksheet_jsmol_data() to figure out the
    cell. Instead, pass the cell information through the script name
    parameter.
---
 sagenb/data/sage/js/jmol_lib.js   |  4 +---
 sagenb/flask_version/worksheet.py | 35 +++++++++++++++++++----------------
 sagenb/notebook/cell.py           |  6 +++---
 3 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/sagenb/data/sage/js/jmol_lib.js b/sagenb/data/sage/js/jmol_lib.js
index 5eb5738..f935ba3 100644
--- a/sagenb/data/sage/js/jmol_lib.js
+++ b/sagenb/data/sage/js/jmol_lib.js
@@ -19,6 +19,7 @@ SageJmolManager.prototype.default_info = function() {
     // Before adding anything here make sure it is not overwritten in
     // add_applet()
     return {
+        // actual size is controlled by the parent <div id='#sage_jmol_N'>
         width: "100%",
         height: "100%",
         // debug=true will pop up alert boxes
@@ -81,9 +82,6 @@ SageJmolManager.prototype.add_applet =
     info.serverURL = server_url;
     info.readyFunction = this.ready_callback.bind(this, applet_name);
     info.deferApplet = !jQuery('#3D_check').prop('checked');
-    if (size != 500)
-        // 500 is the hardcoded (and ill-chosen) default, ignore it
-        info.width = info.height = size;
 
     // append container to dom
     jQuery('#sage_jmol_' + cell_num).append(
diff --git a/sagenb/flask_version/worksheet.py b/sagenb/flask_version/worksheet.py
index 604166c..40c3d54 100644
--- a/sagenb/flask_version/worksheet.py
+++ b/sagenb/flask_version/worksheet.py
@@ -1,3 +1,4 @@
+import re
 import os, threading, collections
 from functools import wraps
 from flask import Module, make_response, url_for, render_template, request, session, redirect, g, current_app
@@ -654,8 +655,8 @@ def worksheet_cells(worksheet, filename):
 ########################################################
 # Jmol/JSmol callback to read data files
 ########################################################
- at worksheet_command('jsmol/<celldir>')
-def worksheet_jsmol_data(worksheet, celldir):
+ at worksheet_command('jsmol')
+def worksheet_jsmol_data(worksheet):
     """
     Jmol/JSmol callback
 
@@ -663,19 +664,14 @@ def worksheet_jsmol_data(worksheet, celldir):
     this URI to get one or more base64-encoded data files.
     """
     # Defaults taken from upstream jsmol.php
-    query = request.values.get('query', "http://cactus.nci.nih.gov/chemical/structure/ethanol/file?format=sdf&get3d=True")
+    query = request.values.get('query', 
+        "http://cactus.nci.nih.gov/chemical/structure/ethanol/file?format=sdf&get3d=True")
     call = request.values.get('call', u'getRawDataFromDatabase')
     database = request.values.get('database', '_')
     encoding = request.values.get('encoding', None)
 
-    if True:
-        print('---- JSmol callback: worksheet_jsmol_data() ---------')
-        print('request url: {0}, method: {1}'.format(request.url, request.method))
-        print('query: ' + query)
-        print('call: ' + call)
-        print('database: ' + database)
-        print('encoding: ' + str(encoding))
-
+    current_app.logger.debug('JSmol call:  %s', call)
+    current_app.logger.debug('JSmol query: %s', query)
     if encoding == None:
         def encoder(x): 
             return x
@@ -685,20 +681,27 @@ def worksheet_jsmol_data(worksheet, celldir):
             # JSmol expects the magic ';base64,' in front of output
             return ';base64,' + base64.encodestring(x)
     else:
+        current_app.logger.error('Invalid JSmol encoding %s', encoding)
         return current_app.message(_('Invalid JSmol encoding: ' + str(encoding)))
 
     if call == u'getRawDataFromDatabase':
         # Annoyingly, JMol prepends the worksheet url (not: the
         # request url) to the query. Strip off:
-        pos = query.rfind('/')
-        if pos >= 0:
-            query = query[pos+1:]
-        query = secure_filename(query)   # never trust input
-        filename = os.path.join(worksheet.cells_directory(), celldir, query)
+        worksheet_url = request.base_url[:-len('/jsmol')]
+        pattern = worksheet_url + '/cells/(?P<cell_id>[0-9]*)/(?P<filename>.*)'
+        match = re.match(pattern, query)
+        if match is None:
+            current_app.logger.error('Invalid JSmol query %s, does not match %s', query, pattern)
+            return current_app.message(_('Invalid JSmol query: ' + query))
+        cell_id = match.group('cell_id')
+        filename = match.group('filename')
+        filename = secure_filename(filename)   # never trust input
+        filename = os.path.join(worksheet.cells_directory(), cell_id, filename)
         with open(filename, 'r') as f:
             data = f.read()
             response = make_response(encoder(data))
     else:
+        current_app.logger.error('Invalid JSmol request %s', call)
         return current_app.message(_('Invalid JSmol request: ' + str(call)))
 
     # Taken from upstream jsmol.php
diff --git a/sagenb/notebook/cell.py b/sagenb/notebook/cell.py
index 28934fa..7c322c9 100755
--- a/sagenb/notebook/cell.py
+++ b/sagenb/notebook/cell.py
@@ -2344,6 +2344,7 @@ class Cell(Cell_generic):
                 f.write(jmol_script)
 
         image_name = os.path.join(self.url_to_self(),'.jmol_images',F)
+        script_name = os.path.join(self.url_to_self(), F)
         return textwrap.dedent("""
         <div id="sage_jmol_{id}" class="3DPlotDiv">
             <div id="loadJmol" style="display:none;">{id}</div>
@@ -2358,9 +2359,8 @@ class Cell(Cell_generic):
             size=size,
             image_name=image_name,
             timestamp=time.time(),
-            filename=F,
-            url=os.path.join(self.url_to_self(), F),
-            callback=os.path.join(self.url_to_worksheet(), 'jsmol', str(self._id)),
+            filename=script_name,
+            callback=os.path.join(self.url_to_worksheet(), 'jsmol'),
         )
 
     def files_html(self, out):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/sagenb.git



More information about the debian-science-commits mailing list