Parcourir la source

Merge branch 'release/v0.1.0'

Jérôme BUISINE il y a 4 ans
Parent
commit
45bce19d2b

+ 1 - 1
ThesisWebExpeDjango/urls.py

@@ -17,6 +17,6 @@ from django.contrib import admin
 from django.urls import include, path
 
 urlpatterns = [
-    path('', include('expe.urls')),
+    path('', include('expe.urls', namespace='expe')),
     path('admin/', admin.site.urls),
 ]

+ 1 - 1
expe/admin.py

@@ -1,3 +1,3 @@
 from django.contrib import admin
 
-# Register your models here.
+# Register your models here.

+ 8 - 7
expe/config.py

@@ -1,13 +1,14 @@
 # api variables
-DIRAN_DOMAIN_NAME           = "https://diran.univ-littoral.fr/"
-GET_SCENE_QUALITIES_API_URL = DIRAN_DOMAIN_NAME + "api/listSceneQualities?sceneName={0}"
-GET_SCENE_IMAGE_API_URL     = DIRAN_DOMAIN_NAME + "api/getImage?sceneName={0}&imageQuality={1}"
-GET_SCENES_API_URL          = DIRAN_DOMAIN_NAME + "api/listScenes"
+DIRAN_DOMAIN_NAME            = "https://diran.univ-littoral.fr/"
+GET_SCENE_QUALITIES_API_URL  = DIRAN_DOMAIN_NAME + "api/listSceneQualities?sceneName={0}"
+GET_SCENE_IMAGE_API_URL      = DIRAN_DOMAIN_NAME + "api/getImage?sceneName={0}&imageQuality={1}"
+GET_SCENES_API_URL           = DIRAN_DOMAIN_NAME + "api/listScenes"
 
 # folder variables
-model_expe_folder           = "expes_models/{0}/"
-output_expe_folder          = "expes_results/{0}/"
-output_tmp_folder           = "tmp"
+model_expe_folder            = "expes_models/{0}/{1}"
+output_expe_folder           = "expes_results"
+output_expe_folder_name_day  = "expes_results/{0}/{1}"
+output_tmp_folder            = "tmp"
 
 # expes list
 expe_name_list              = ["quest_one_image"]

+ 2 - 0
expe/templates/base.html

@@ -12,6 +12,7 @@
     {% block stylesheets %}
         <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
         <link rel="stylesheet" type="text/css" href="{% static "css/expe.css" %}">
+        <script src="https://kit.fontawesome.com/ee9d97bd14.js" crossorigin="anonymous"></script>
     {% endblock %}
 </head>
 <body>
@@ -23,5 +24,6 @@
     </div>
 
     {% block javascripts %}
+    
     {% endblock %}
 </body>

+ 85 - 0
expe/templates/expe/expe_results.html

@@ -0,0 +1,85 @@
+{% extends 'base.html' %}
+
+{% load staticfiles %}
+
+{% block title %}
+    Expe {{ expe_name }}
+{% endblock %}
+
+{% block content %}
+    
+    {% if not expe %}
+        <h3>Results files of experiences</h3>
+        <br />
+    {% endif %}
+
+    <div class="results-expe">
+
+        {% if expe %}
+            <div class="alert alert-info" role="alert">
+                <h4 class="alert-heading"><strong>Experience:</strong> {{expe}}</h4>
+                <hr>
+                <p><strong>Question:</strong> {{infos.question}} </p>
+                <p class="mb-0"><strong>Indication:</strong> {{infos.indication}} </p>
+            </div>
+
+            <br/>
+            {% if folders %}
+
+                <ul class="list-group">
+
+                {% for name, folder in folders.items %} 
+                    
+                        <li class="list-group-item">
+                            
+                            <div class="row">
+                                <div class="col-md-10">{{name}} </div>
+                                <div class="col-md-1 download-list" data-download-path="{{expe}}/{{name}}"><i class="fas fa-download"></i></div>
+                                <div class="col-md-1 date-folder-list"><i class="fas fa-arrow-circle-right"></i></div>
+                            </div>
+                            
+                            <ul class="list-group files-list" style="display: none">
+
+                                <hr />
+                                {% for file in folder %}
+                                    <li class="list-group-item">
+                                    
+                                        <div class="row">
+                                            <div class="col-md-11">{{file}} </div>
+                                            <div class="col-md-1 download-list" data-download-path="{{expe}}/{{name}}/{{file}}"><i class="fas fa-download"></i></div>
+                                        </div>
+                                                
+                                    </li>
+                                {% endfor %}
+                            </ul>
+                        </li>
+                {% endfor %}
+
+                </ul>
+            {% csrf_token %}
+
+            {% else %}
+                <div class="alert alert-warning" role="alert">
+                    <i>Expe folder is empty</i>
+                </div>
+            {% endif %}
+        {% else %}
+             {% for f in folders %}
+                <ul class="list-group">
+                    <a href="{% url 'expe:results_expe' expe=f %}">
+                        <li class="list-group-item">{{f}}</li>
+                    </a>
+                </ul>
+            {% endfor %}
+        {% endif %}
+    </div>
+
+    {% block javascripts %}
+         <script type="text/javascript"> 
+            // Utils informations
+            var expe_name = "{{expe}}"
+        </script>
+        <script src="{% static "js/results.js" %}"></script>
+        <script src="{% static "js/FileSaver.js" %}"></script>
+    {% endblock %}
+{% endblock %}

+ 5 - 0
expe/urls.py

@@ -5,10 +5,15 @@ from django.conf.urls.static import static
 
 from . import views
 
+app_name = 'expe'
+
 urlpatterns = [
     path('', views.expe_list, name='expe_list'),
     path('expe', views.expe, name='expe'),
     path('indications', views.indications, name='indications'),
+    path('admin/results', views.list_results, name='results'),
+    path('admin/results/<str:expe>', views.list_results, name='results_expe'),
+    path('admin/download', views.download_result, name='download')
 ]
 
 if settings.DEBUG is True:

+ 114 - 5
expe/views.py

@@ -2,6 +2,8 @@
 from django.shortcuts import render
 from django.http import HttpResponse
 from django.conf import settings
+from django.contrib.auth.decorators import login_required
+from django.http import Http404
 
 # main imports
 import os
@@ -12,6 +14,9 @@ import numpy as np
 from datetime import datetime
 import pickle 
 import time
+import zipfile
+from io import BytesIO
+
 
 # expe imports
 from .expes.quest_plus import QuestPlus
@@ -39,6 +44,9 @@ def expe_list(request):
     # get list of experiences
     expes = cfg.expe_name_list
 
+    # by default user restart expe
+    request.session['expe_started'] = False
+
     return render(request, 'expe/expe_list.html', {'scenes': scenes, 'expes': expes})
 
 def indications(request):
@@ -80,12 +88,12 @@ def expe(request):
 
     # create output folder for expe_result
     current_day = datetime.strftime(datetime.utcnow(), "%Y-%m-%d")
-    results_folder = os.path.join(settings.MEDIA_ROOT, cfg.output_expe_folder.format(current_day))
+    results_folder = os.path.join(settings.MEDIA_ROOT, cfg.output_expe_folder_name_day.format(expe_name, current_day))
 
     if not os.path.exists(results_folder):
         os.makedirs(results_folder)
 
-    result_filename = expe_name + '_' + scene_name + '_' + request.session.get('id') + '_' + request.session.get('timestamp') +".csv"
+    result_filename = scene_name + '_' + request.session.get('id') + '_' + request.session.get('timestamp') +".csv"
     results_filepath = os.path.join(results_folder, result_filename)
 
     if not os.path.exists(results_filepath):
@@ -95,7 +103,7 @@ def expe(request):
         output_file = open(results_filepath, 'a')
 
     # create `quest` object if not exists    
-    models_folder = os.path.join(settings.MEDIA_ROOT, cfg.model_expe_folder.format(current_day))
+    models_folder = os.path.join(settings.MEDIA_ROOT, cfg.model_expe_folder.format(expe_name, current_day))
 
     if not os.path.exists(models_folder):
         os.makedirs(models_folder)
@@ -134,8 +142,107 @@ def expe(request):
     return render(request, 'expe/expe.html', data)
 
 
-def refresh_data(request, expe_name, scene_name):
+@login_required(login_url="login/")
+def list_results(request, expe=None):
+    """
+    Return all results obtained from experiences
+    """
+
+    if expe is None:
+        folders = cfg.expe_name_list
+
+        return render(request, 'expe/expe_results.html', {'expe': expe, 'folders': folders})
+
+    else:
+        if expe in cfg.expe_name_list:
+            folder_path = os.path.join(settings.MEDIA_ROOT, cfg.output_expe_folder, expe)
+
+            # init folder dictionnary
+            folders = {}
+
+            if os.path.exists(folder_path):
+            
+                days = os.listdir(folder_path)
+
+                for day in days:
+                    day_path = os.path.join(folder_path, day)
+                    filenames = os.listdir(day_path)
+                    folders[day] = filenames
+        else:
+            raise Http404("Expe does not exists")
+
+    return render(request, 'expe/expe_results.html', {'expe': expe, 'folders': folders, 'infos': cfg.expes_configuration[expe]['text']})
+
+
+@login_required(login_url="login/")
+def download_result(request):
+    
+    path = request.POST.get('path')
+    folder_path = os.path.join(settings.MEDIA_ROOT, cfg.output_expe_folder, path)
+
+    # Folder is required
+    if os.path.exists(folder_path):
+
+        # Open BytesIO to grab in-memory ZIP contents
+        s = BytesIO()
+
+        # check if file or folder
+        if os.path.isdir(folder_path):
+            
+            # get files from a specific day
+            filenames = os.listdir(folder_path)
 
+            # Folder name in ZIP archive which contains the above files
+            # E.g [thearchive.zip]/somefiles/file2.txt
+            # FIXME: Set this to something better
+            zip_subdir = folder_path.split('/')[-1]
+            zip_filename = "%s.zip" % zip_subdir
+
+            # The zip compressor
+            zf = zipfile.ZipFile(s, "w")
+
+            for fpath in filenames:
+                
+                fpath = os.path.join(folder_path, fpath)
+
+                # Calculate path for file in zip
+                fdir, fname = os.path.split(fpath)
+                zip_path = os.path.join(zip_subdir, fname)
+
+                # Add file, at correct path
+                zf.write(fpath, zip_path)
+
+            # Must close zip for all contents to be written
+            zf.close()
+
+            output_filename = zip_filename
+            content = s.getvalue()
+
+        else:
+            
+            with open(folder_path, 'rb') as f:
+                content = f.readlines()
+
+            # filename only
+            fdir, fname = os.path.split(path)
+            output_filename = fname
+
+        # Grab ZIP file from in-memory, make response with correct MIME-type
+        resp = HttpResponse(content, content_type="application/gzip")
+        # ..and correct content-disposition
+        resp['Content-Disposition'] = 'attachment; filename=%s' % output_filename
+
+        return resp
+
+    else:
+        return Http404("Path does not exist")
+
+
+
+def refresh_data(request, expe_name, scene_name):
+    '''
+    Utils method to refresh data from session
+    '''
     request.session['expe'] = expe_name
     request.session['scene'] = scene_name
 
@@ -150,4 +257,6 @@ def refresh_data(request, expe_name, scene_name):
     # get reference image
     #ref_image = api.get_image(scene_name, 'max')
     # save ref image as list (can't save python object)
-    #request.session['ref_img'] = np.array(ref_image).tolist()
+    #request.session['ref_img'] = np.array(ref_image).tolist()
+
+

+ 15 - 0
static/css/expe.css

@@ -13,4 +13,19 @@ body {
 
 #expeIndication{
     margin-top: 30%;
+}
+
+
+.results-expe{
+    margin-left: 20%;
+    margin-right: 20%;
+    text-align: left;
+}
+
+.results-expe a:hover{
+    text-decoration: none;
+}
+
+.results-expe a{
+    color:black;
 }

+ 166 - 0
static/js/FileSaver.js

@@ -0,0 +1,166 @@
+/*
+* FileSaver.js
+* A saveAs() FileSaver implementation.
+*
+* By Eli Grey, http://eligrey.com
+*
+* License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
+* source  : http://purl.eligrey.com/github/FileSaver.js
+*/
+
+// The one and only way of getting global scope in all environments
+// https://stackoverflow.com/q/3277182/1008999
+var _global = typeof window === 'object' && window.window === window
+  ? window : typeof self === 'object' && self.self === self
+  ? self : typeof global === 'object' && global.global === global
+  ? global
+  : this
+
+function bom (blob, opts) {
+  if (typeof opts === 'undefined') opts = { autoBom: false }
+  else if (typeof opts !== 'object') {
+    console.warn('Deprecated: Expected third argument to be a object')
+    opts = { autoBom: !opts }
+  }
+
+  // prepend BOM for UTF-8 XML and text/* types (including HTML)
+  // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
+  if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
+    return new Blob([String.fromCharCode(0xFEFF), blob], { type: blob.type })
+  }
+  return blob
+}
+
+function download (url, name, opts) {
+  var xhr = new XMLHttpRequest()
+  xhr.open('GET', url)
+  xhr.responseType = 'blob'
+  xhr.onload = function () {
+    saveAs(xhr.response, name, opts)
+  }
+  xhr.onerror = function () {
+    console.error('could not download file')
+  }
+  xhr.send()
+}
+
+function corsEnabled (url) {
+  var xhr = new XMLHttpRequest()
+  // use sync to avoid popup blocker
+  xhr.open('HEAD', url, false)
+  try {
+    xhr.send()
+  } catch (e) {}
+  return xhr.status >= 200 && xhr.status <= 299
+}
+
+// `a.click()` doesn't work for all browsers (#465)
+function click (node) {
+  try {
+    node.dispatchEvent(new MouseEvent('click'))
+  } catch (e) {
+    var evt = document.createEvent('MouseEvents')
+    evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
+                          20, false, false, false, false, 0, null)
+    node.dispatchEvent(evt)
+  }
+}
+
+var saveAs = _global.saveAs || (
+  // probably in some web worker
+  (typeof window !== 'object' || window !== _global)
+    ? function saveAs () { /* noop */ }
+
+  // Use download attribute first if possible (#193 Lumia mobile)
+  : 'download' in HTMLAnchorElement.prototype
+  ? function saveAs (blob, name, opts) {
+    var URL = _global.URL || _global.webkitURL
+    var a = document.createElement('a')
+    name = name || blob.name || 'download'
+
+    a.download = name
+    a.rel = 'noopener' // tabnabbing
+
+    // TODO: detect chrome extensions & packaged apps
+    // a.target = '_blank'
+
+    if (typeof blob === 'string') {
+      // Support regular links
+      a.href = blob
+      if (a.origin !== location.origin) {
+        corsEnabled(a.href)
+          ? download(blob, name, opts)
+          : click(a, a.target = '_blank')
+      } else {
+        click(a)
+      }
+    } else {
+      // Support blobs
+      a.href = URL.createObjectURL(blob)
+      setTimeout(function () { URL.revokeObjectURL(a.href) }, 4E4) // 40s
+      setTimeout(function () { click(a) }, 0)
+    }
+  }
+
+  // Use msSaveOrOpenBlob as a second approach
+  : 'msSaveOrOpenBlob' in navigator
+  ? function saveAs (blob, name, opts) {
+    name = name || blob.name || 'download'
+
+    if (typeof blob === 'string') {
+      if (corsEnabled(blob)) {
+        download(blob, name, opts)
+      } else {
+        var a = document.createElement('a')
+        a.href = blob
+        a.target = '_blank'
+        setTimeout(function () { click(a) })
+      }
+    } else {
+      navigator.msSaveOrOpenBlob(bom(blob, opts), name)
+    }
+  }
+
+  // Fallback to using FileReader and a popup
+  : function saveAs (blob, name, opts, popup) {
+    // Open a popup immediately do go around popup blocker
+    // Mostly only available on user interaction and the fileReader is async so...
+    popup = popup || open('', '_blank')
+    if (popup) {
+      popup.document.title =
+      popup.document.body.innerText = 'downloading...'
+    }
+
+    if (typeof blob === 'string') return download(blob, name, opts)
+
+    var force = blob.type === 'application/octet-stream'
+    var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari
+    var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent)
+
+    if ((isChromeIOS || (force && isSafari)) && typeof FileReader !== 'undefined') {
+      // Safari doesn't allow downloading of blob URLs
+      var reader = new FileReader()
+      reader.onloadend = function () {
+        var url = reader.result
+        url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;')
+        if (popup) popup.location.href = url
+        else location = url
+        popup = null // reverse-tabnabbing #460
+      }
+      reader.readAsDataURL(blob)
+    } else {
+      var URL = _global.URL || _global.webkitURL
+      var url = URL.createObjectURL(blob)
+      if (popup) popup.location = url
+      else location.href = url
+      popup = null // reverse-tabnabbing #460
+      setTimeout(function () { URL.revokeObjectURL(url) }, 4E4) // 40s
+    }
+  }
+)
+
+_global.saveAs = saveAs.saveAs = saveAs
+
+if (typeof module !== 'undefined') {
+  module.exports = saveAs;
+}

+ 82 - 0
static/js/results.js

@@ -0,0 +1,82 @@
+function toggle(elem) {
+    if (elem.style.display === "none") {
+        elem.style.display = "block";
+      } else {
+        elem.style.display = "none";
+      }
+}
+
+function toggleClass(elem, class1, class2) {
+    if (elem.className === class1) {
+        elem.className = class2;
+      } else {
+        elem.className = class1;
+      }
+}
+
+// use for call route to dowload content (as post request)
+function downloadContent(path){
+
+    const csrfToken = document.querySelectorAll('[name=csrfmiddlewaretoken]')[0].value
+
+    var xhttp = new XMLHttpRequest();
+    xhttp.open("POST", "/admin/download", true);
+    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+    xhttp.setRequestHeader("X-CSRFToken", csrfToken);
+
+    xhttp.onreadystatechange = function (){
+        if (xhttp.readyState == 4 && xhttp.status == 200) {
+
+            // Try to find out the filename from the content disposition `filename` value
+            var disposition = xhttp.getResponseHeader('content-disposition');
+
+            // expe is find from django
+            var filename = expe_name + "_" + disposition.split('=')[1]
+      
+            var blob = new Blob([xhttp.response], {type: "octet/stream"});
+            saveAs(blob, filename);
+        }
+    };
+    xhttp.responseType = "arraybuffer";
+    xhttp.send("path=" + path); 
+}
+
+
+window.onload = function () {
+
+    // Display list of files from day folder
+    elems = document.getElementsByClassName('date-folder-list')
+    
+    for (let item of elems) {
+
+        item.onclick = function(event){
+            event.preventDefault()
+            currentElem = event.currentTarget
+
+            // get list element
+            list = currentElem.parentElement.nextElementSibling
+            
+            // display or hide list elements
+            toggle(list)
+
+            // toggle arrow class for display effect
+            iconElem = currentElem.children[0]
+            toggleClass(iconElem, 'fas fa-arrow-circle-right', 'fas fa-arrow-circle-down')
+        }
+    }
+
+
+    elems = document.getElementsByClassName('download-list')
+
+    for (let downloadElem of elems) {
+
+        downloadElem.onclick = function(event){
+            event.preventDefault()
+
+            currentElem = event.currentTarget
+            pathDownload = currentElem.getAttribute('data-download-path')
+
+            downloadContent(pathDownload)
+        }
+    }
+}