From 0275a684b3c764a64ae47e9bd2c50cd5ea4f3e41 Mon Sep 17 00:00:00 2001
From: root <root@petrating-01.utu.fi>
Date: Mon, 6 Nov 2023 13:07:17 +0200
Subject: [PATCH] Deprecated attribute in flask send_file

---
 .../templates/experiment_statistics.html           |  3 ++-
 app/experiment/views.py                            | 14 +++++++++++---
 app/static/js/getCSV.js                            |  7 +++++--
 app/static/js/urls.js                              |  4 ++--
 app/utils.py                                       |  4 ++--
 requirements.txt                                   |  2 +-
 6 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/app/experiment/templates/experiment_statistics.html b/app/experiment/templates/experiment_statistics.html
index d3a8aae..084feb9 100644
--- a/app/experiment/templates/experiment_statistics.html
+++ b/app/experiment/templates/experiment_statistics.html
@@ -219,8 +219,9 @@
 
 <!-- <script type=module src="{{ url_for('static', filename='lib/js/socket.io.esm.min.js') }}"></script> -->
 <script src="{{ url_for('static', filename='lib/js/socket.io.min.js') }}"></script>
+<script src="{{ url_for('static', filename='lib/js/socket.io.min.js.map') }}"></script>
 <script src="{{ url_for('static', filename='js/urls.js') }}"></script>
 <script type=module src="{{ url_for('static', filename='js/getDrawing.js') }}"></script>
 <script src="{{ url_for('static', filename='js/getCSV.js') }}"></script>
 
-{% endblock %}
\ No newline at end of file
+{% endblock %}
diff --git a/app/experiment/views.py b/app/experiment/views.py
index 05350fb..4df2d0b 100644
--- a/app/experiment/views.py
+++ b/app/experiment/views.py
@@ -206,8 +206,14 @@ def remove():
                 db.session.commit()
 
                 # Remove empty directories
-                os.rmdir(APP_ROOT + '/static/embody_images/' + str(exp_id))
-                os.rmdir(APP_ROOT + '/static/experiment_stimuli/' + str(exp_id))
+                try:
+                    os.rmdir(APP_ROOT + '/static/embody_images/' + str(exp_id))
+                except FileNotFoundError as err:
+                    app.logger.info(err)
+                try:
+                    os.rmdir(APP_ROOT + '/static/experiment_stimuli/' + str(exp_id))
+                except FileNotFoundError as err:
+                    app.logger.info(err)
 
                 flash("Experiment was removed from database!")
                 return redirect(url_for('index'))
@@ -1023,7 +1029,7 @@ def download_csv():
         return send_file(path,
                          mimetype='text/csv',
                          as_attachment=True,
-                         attachment_filename=filename)
+                         download_name=filename)
 
     finally:
         os.remove(path)
@@ -1061,11 +1067,13 @@ def download_csv(meta):
 
     # create temporary file
     fd, path = mkstemp()
+
     with os.fdopen(fd, 'w', buffering=1) as tmp:
         if generate_csv(exp_id, tmp):
             # return path and filename to front so user can start downloading
             filename = "experiment_{}_{}".format(
                 exp_id, date.today().strftime("%Y-%m-%d"))
+
             path = path.split('/')[-1]
             emit('file_ready', {'path': path, 'filename': filename})
         else:
diff --git a/app/static/js/getCSV.js b/app/static/js/getCSV.js
index 4b0313b..33762b0 100644
--- a/app/static/js/getCSV.js
+++ b/app/static/js/getCSV.js
@@ -40,7 +40,6 @@ $(document).ready(function()  {
         });
 
         socket.on('file_ready', function(file) {
-
             socket.emit('end')
             socket.disconnect()            
 
@@ -59,6 +58,10 @@ $(document).ready(function()  {
             progressBarContainer.addClass("hidden")
             progressBar.width('0%')
         });
+
+	    socket.on("connect_error", (err) => {
+		      console.log(`connect_error due to ${err.message}`);
+	    });
     }
 
 
@@ -66,7 +69,7 @@ $(document).ready(function()  {
         event.preventDefault()
 
         // Init socket
-        var socket = io.connect(exportURL);
+        const socket = io(exportURL);
         initConnection(socket)
 
         // start generating csv file...
diff --git a/app/static/js/urls.js b/app/static/js/urls.js
index 86a9b1e..bc2e223 100644
--- a/app/static/js/urls.js
+++ b/app/static/js/urls.js
@@ -1,5 +1,5 @@
-const baseURI = "localhost:8000/";
-//const baseURI = 'http://onni.utu.fi/';
+//const baseURI = 'https://onni.utu.fi/';
+const baseURI = 'https://petrating-01.utu.fi/';
 
 var exportURL = baseURI + "download_csv";
 var getDrawingURI = baseURI + "create_embody";
diff --git a/app/utils.py b/app/utils.py
index 9fc8802..2c0043d 100644
--- a/app/utils.py
+++ b/app/utils.py
@@ -169,9 +169,9 @@ def generate_csv(exp_id, file_handle):
                 # to ensure that all internal buffers associated with fd are written to disk
                 file_handle.flush()
             except Exception as exc:
-                print('generated an exception: {}'.format(exc))
+                app.logger.info('generated an exception: {}'.format(exc))
                 # return False
-    
+
     return True
 
 
diff --git a/requirements.txt b/requirements.txt
index b2a1ee2..6f0d513 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -12,7 +12,7 @@ Flask-WTF==1.2.1
 gevent==23.9.1
 greenlet==3.0.1
 gunicorn==19.9.0
-matplotlib==3.0.3
+matplotlib==3.8.1
 networkx==2.2
 numpy==1.24.4
 Pillow
-- 
GitLab