From 0993e0d24f6cdb08f644db4efe766bb3facabeb8 Mon Sep 17 00:00:00 2001
From: vermeul <swen@ethz.ch>
Date: Fri, 12 Jul 2019 12:22:55 +0200
Subject: [PATCH] all recent changes

---
 CHANGELOG.md                                  |  5 +--
 jupyter-openbis-extension/connection.py       | 16 ++++-----
 jupyter-openbis-extension/dataset.py          |  2 +-
 jupyter-openbis-extension/server.py           | 11 ++----
 jupyter-openbis-extension/static/common.js    | 11 +++++-
 .../static/connectionDialog.js                | 35 ++++++++++++++++++-
 .../static/downloadDialog.js                  | 31 ++++++++++------
 jupyter-openbis-extension/static/state.js     |  4 ++-
 .../static/uploadDialog.js                    | 25 ++++++-------
 9 files changed, 96 insertions(+), 44 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index bf65abb..550e809 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,8 +1,9 @@
-## new in jupyter-openbis-extension 0.2.8
+## new in jupyter-openbis-extension 0.3.0
 
 - removed search-as-you-type feature, as it is not mature yet
+- fixed notebook path problem in special environments
 - connection dialog: show when connection is being established
-- upload dialog: better and faster file chooser
+- upload dialog: better (and faster) file chooser
 - upload dialog: save notebook automatically before upload
 
 ## new in jupyter-openbis-extension 0.2.4
diff --git a/jupyter-openbis-extension/connection.py b/jupyter-openbis-extension/connection.py
index f6974e0..d256581 100644
--- a/jupyter-openbis-extension/connection.py
+++ b/jupyter-openbis-extension/connection.py
@@ -96,9 +96,9 @@ class OpenBISConnections(IPythonHandler):
             connections.append(conn.get_info())
 
         self.write({
-            'status'     : 200,
-            'connections': connections,
-            'cwd'        : os.getcwd()
+            'status'       : 200,
+            'connections'  : connections,
+            'notebook_dir' : self.config.NotebookApp.notebook_dir
         })
         return
 
@@ -145,8 +145,8 @@ class OpenBISConnectionHandler(IPythonHandler):
 
         self.write({
             'status'     : 200,
-            'connection': conn.get_info(),
-            'cwd'        : os.getcwd()
+            'connection' : conn.get_info(),
+            ''        : self.config.NotebookApp.notebook_dir
         })
 
     def get(self, connection_name):
@@ -165,9 +165,9 @@ class OpenBISConnectionHandler(IPythonHandler):
         conn.check_status()
 
         self.write({
-            'status'     : 200,
-            'connection': conn.get_info(),
-            'cwd'        : os.getcwd()
+            'status'        : 200,
+            'connection'    : conn.get_info(),
+            'noteboook_dir' : self.config.NotebookApp.notebook_dir
         })
         return
 
diff --git a/jupyter-openbis-extension/dataset.py b/jupyter-openbis-extension/dataset.py
index 8b7713d..97bb085 100644
--- a/jupyter-openbis-extension/dataset.py
+++ b/jupyter-openbis-extension/dataset.py
@@ -6,7 +6,6 @@ from .connection import openbis_connections
 class DataSetDownloadHandler(IPythonHandler):
     """Handle the requests for /openbis/dataset/connection/permId"""
 
-
     def download_data(self, conn, permId, downloadPath=None):
         if not conn.is_session_active():
             try:
@@ -46,6 +45,7 @@ class DataSetDownloadHandler(IPythonHandler):
             'dataStore' : dataset.dataStore,
             'location'  : dataset.physicalData.location,
             'size'      : dataset.physicalData.size,
+            'files'     : dataset.file_list,
             'statusText': 'Data for DataSet {} was successfully downloaded to: {}.'.format(dataset.permId, path)
         })
 
diff --git a/jupyter-openbis-extension/server.py b/jupyter-openbis-extension/server.py
index 310a1ce..6d95bd5 100644
--- a/jupyter-openbis-extension/server.py
+++ b/jupyter-openbis-extension/server.py
@@ -3,7 +3,7 @@ import os
 import yaml
 
 from .connection import OpenBISConnections, OpenBISConnectionHandler, register_connection
-from .dataset import DataSetTypesHandler, DataSetDownloadHandler, DataSetUploadHandler, FileListHandler
+from .dataset import DataSetTypesHandler, DataSetDownloadHandler, DataSetUploadHandler
 from .sample import SampleHandler
 
 
@@ -48,6 +48,7 @@ def load_jupyter_server_extension(nb_server_app):
         filename = 'openbis-connections.yaml'
     )
 
+
     for connection_info in connections:
         conn = register_connection(connection_info)
         print("Registered: {}".format(conn.url))
@@ -66,13 +67,6 @@ def load_jupyter_server_extension(nb_server_app):
     host_pattern = '.*$'
     base_url = web_app.settings['base_url']
 
-    # get the file list
-    web_app.add_handlers(
-        host_pattern,
-        [(url_path_join( base_url, '/general/filelist/?(?P<path>.*)'),
-            FileListHandler
-        )]
-    )
 
     # DataSet download
     web_app.add_handlers(
@@ -138,3 +132,4 @@ def load_jupyter_server_extension(nb_server_app):
     )
 
 
+
diff --git a/jupyter-openbis-extension/static/common.js b/jupyter-openbis-extension/static/common.js
index 15280a1..edcb4af 100644
--- a/jupyter-openbis-extension/static/common.js
+++ b/jupyter-openbis-extension/static/common.js
@@ -39,9 +39,18 @@ define([
             return "";
         }
 
+        function createErrorElement() {
+            var element = document.createElement("STRONG")
+            element.textContent = ""
+            element.style.marginLeft = "8px"
+            element.style.color = "red"
+            return element
+        }
+
         return {
             createFeedback: createFeedback,
-            getCookie: getCookie
+            getCookie: getCookie,
+            createErrorElement: createErrorElement
         }
     }
 )
\ No newline at end of file
diff --git a/jupyter-openbis-extension/static/connectionDialog.js b/jupyter-openbis-extension/static/connectionDialog.js
index cbdd45c..81eed5f 100644
--- a/jupyter-openbis-extension/static/connectionDialog.js
+++ b/jupyter-openbis-extension/static/connectionDialog.js
@@ -10,6 +10,7 @@ define(
         let currentDownloadPath = null;
 
         function show_available_connections(env, data, conn_table) {
+
             if (!currentDownloadPath) {
                 currentDownloadPath = data.cwd
             }
@@ -214,10 +215,41 @@ define(
             uname_pw_cell.appendChild(create_btn)
 
             conn_table.innerHTML = ""
-            table_title = document.createElement("STRONG")
+            let table_title = document.createElement("STRONG")
             table_title.textContent = "Please choose a connection"
+
+            let working_dir_title = document.createElement("STRONG")
+            working_dir_title.textContent = "Your working directory "
+            let working_dir_in = document.createElement("INPUT")
+            working_dir_in.type = "text"
+            working_dir_in.name = "working_dir"
+            working_dir_in.autocomplete = "on"
+            working_dir_in.style.width = "100%"
+
+
+            // calculate the default working directory
+            // by combining the notebook_dir (from the jupyter configuration) and the relative notebook_path
+            let re = new RegExp(env.notebook.notebook_name+"$")
+            rel_path = env.notebook.notebook_path.replace(re, "")
+            let default_working_dir = data.notebook_dir + "/" + rel_path
+
+            working_dir_in.value = state.working_dir ? state.working_dir : default_working_dir
+            state.working_dir_element = working_dir_in
+
+            let working_dir_reset = document.createElement("A")
+            working_dir_reset.innerText = "reset to default"
+            working_dir_reset.onclick = function() {
+                working_dir_in.value = default_working_dir
+            }
+
             conn_table.appendChild(table_title)
             conn_table.appendChild(table)
+
+            conn_table.appendChild(working_dir_title)
+            conn_table.appendChild(working_dir_reset)
+            conn_table.appendChild(document.createElement("BR"))
+            conn_table.append(working_dir_in)
+
         }
 
         return {
@@ -247,6 +279,7 @@ define(
                 function onOk() {
                     state.connection.name = state.connection.candidateName
                     state.connection.dto = state.connection.candidateDTO
+                    state.working_dir = state.working_dir_element.value
                 }
 
                 function onCancel() {
diff --git a/jupyter-openbis-extension/static/downloadDialog.js b/jupyter-openbis-extension/static/downloadDialog.js
index 7a9c399..bd251b8 100644
--- a/jupyter-openbis-extension/static/downloadDialog.js
+++ b/jupyter-openbis-extension/static/downloadDialog.js
@@ -31,16 +31,13 @@ define([
                     "dataStore": data.dataStore,
                     "location": data.location,
                     "size": data.size,
+                    "files": data.files,
                     "status": data.statusText
                 }
             }
         }
 
         function show_datasets_table(env, data, datasets_table, pagingContainer, downloadPath, entityIdentifier) {
-            if (downloadPath.value === '') {
-                downloadPath.value = data.cwd
-            }
-
             var table = document.createElement("TABLE")
             table.className = "table-bordered table-striped table-condensed text-nowrap"
             table.style.width = "100%"
@@ -183,6 +180,7 @@ define([
             icon: 'fa-download',
             help_index: '',
             handler: function (env) {
+                console.log(env);
                 state.selectedDatasets = new Set([])
 
                 conn_table = document.createElement("DIV")
@@ -232,7 +230,7 @@ define([
                 downloadPath.type = "text"
                 downloadPath.name = "downloadPath"
                 downloadPath.size = "90"
-                downloadPath.value = state.workingDirectory
+                downloadPath.value = state.download_dir ? state.download_dir : state.working_dir
 
                 show_datasets_btn.onclick =
                     () => getDatasets(env, 0, 5, entityIdentifier, datasets_table, pagingContainer, downloadPath)
@@ -243,16 +241,28 @@ define([
                 path.appendChild(downloadPath)
 
                 var download_dialog_box = document.createElement("DIV")
+
+                let mainError = common.createErrorElement()
+                if (!state.connection.name) {
+                    mainError.textContent = "Please choose a connection first."
+                    //mainError.textContent = "Network problem: please check your connection first."
+                }
+                download_dialog_box.appendChild(mainError)
                 download_dialog_box.appendChild(spinner)
                 download_dialog_box.appendChild(conn_table)
                 download_dialog_box.appendChild(showDataSets)
                 download_dialog_box.appendChild(dataset_direct)
                 download_dialog_box.appendChild(path)
 
-                function saveState() {
+                function onOk() {
+                    state.entityIdentifier = entityIdentifier.value
+                    state.directPermId = datasetPermId.value
+                    state.download_dir = downloadPath.value
+                }
+
+                function onCancel() {
                     state.entityIdentifier = entityIdentifier.value
                     state.directPermId = datasetPermId.value
-                    state.workingDirectory = downloadPath.value
                 }
 
                 function downloadDataset(connection_name, selectedPermIds, downloadPath) {
@@ -282,7 +292,8 @@ define([
                                             // keep current download path for later use
                                             currentDownloadPath = downloadPath
                                         })
-                                } else {
+                                }
+                                else {
                                     response.json()
                                         .then(function (error) {
                                             console.log(error.reason)
@@ -322,7 +333,7 @@ define([
 
                     downloadDataset(selected_conn, selectedPermIds, downloadPath.value)
                     showSpinner()
-                    saveState()
+                    onOk()
                     return false
                 }
 
@@ -331,7 +342,7 @@ define([
                     title: 'Download openBIS DataSets',
                     buttons: {
                         'Cancel': {
-                            click: () => saveState()
+                            click: () => onCancel()
                         },
                         'Download': {
                             class: 'btn-primary btn-large',
diff --git a/jupyter-openbis-extension/static/state.js b/jupyter-openbis-extension/static/state.js
index 71ea8e2..bcbe83f 100644
--- a/jupyter-openbis-extension/static/state.js
+++ b/jupyter-openbis-extension/static/state.js
@@ -8,6 +8,9 @@ define([],
                 candidateName: null,
                 candidateDTO: null
             },
+            working_dir: null,
+            working_dir_element: null,
+            download_dir: null,
 
             // upload dialog
             uploadDataSetType: null,
@@ -21,7 +24,6 @@ define([],
             // download dialog
             selectedDatasets: new Set([]),
             entity: null,
-            workingDirectory: '',
 
             // openBIS v3 connection
             openbisService : null
diff --git a/jupyter-openbis-extension/static/uploadDialog.js b/jupyter-openbis-extension/static/uploadDialog.js
index a8bdb9a..5cbfae4 100644
--- a/jupyter-openbis-extension/static/uploadDialog.js
+++ b/jupyter-openbis-extension/static/uploadDialog.js
@@ -355,7 +355,10 @@ define([
                 var fileListContainer = document.createElement("DIV")
                 fileListContainer.style.height="200px"
                 fileListContainer.style.overflow="auto"
-                get_file_list(env, fileListContainer, "")
+                // get the relative path
+                let re = new RegExp(env.notebook.notebook_name+"$")
+                rel_path = env.notebook.notebook_path.replace(re, "")
+                get_file_list(env, fileListContainer, rel_path)
                 
                 var inputs = document.createElement("DIV")
                 inputs.style.marginTop = '10px'
@@ -398,13 +401,11 @@ define([
                     }
 
                     var uploadUrl = env.notebook.base_url + 'openbis/dataset/' + connection_name
-                    var notebook = IPython.notebook
-                    //var files = state.fileCheckboxes.filter(cb => cb.checked).map(cb => cb.value)
+
+                    // add this notebook to the list of files
                     var files = state.selectedFiles
-                    var re = /\/notebooks\/(.*?)$/
-                    var filepath = window.location.pathname.match(re)[1]
-                    if (! files.includes(filepath)) {
-                        files.push(filepath)
+                    if (! files.includes(env.notebook.notebook_path)) {
+                        files.push(env.notebook.notebook_path)
                     }
                     console.log(files)
 
@@ -436,14 +437,14 @@ define([
                             common.createFeedback('success', data.statusText)
 
                             // write statusText from returned data to notebooks metadata
-                            if (typeof notebook.metadata.openbis === 'undefined') {
-                                notebook.metadata.openbis = {}
+                            if (typeof env.notebook.metadata.openbis === 'undefined') {
+                                env.notebook.metadata.openbis = {}
                             }
-                            if (typeof notebook.metadata.openbis.permIds === 'undefined') {
-                                notebook.metadata.openbis.permIds = {}
+                            if (typeof env.notebook.metadata.openbis.permIds === 'undefined') {
+                                env.notebook.metadata.openbis.permIds = {}
                             }
                             if (data.permId) {
-                                notebook.metadata.openbis.permIds[data.permId] = data.statusText
+                                env.notebook.metadata.openbis.permIds[data.permId] = data.statusText
                             }
                         },
                         error: function (data) {
-- 
GitLab