Explorar o código

Merge branch '119-show-incorrect-storage-paths-in-create-project-dialog' into 'master'

Resolve "show incorrect storage paths in create project dialog"

Closes #119

See merge request troebs/pycs!117
Eric Tröbs %!s(int64=3) %!d(string=hai) anos
pai
achega
d9b0642a2b

+ 7 - 0
pycs/frontend/WebServer.py

@@ -11,6 +11,7 @@ from pycs.frontend.endpoints.ListJobs import ListJobs
 from pycs.frontend.endpoints.ListLabelProviders import ListLabelProviders
 from pycs.frontend.endpoints.ListModels import ListModels
 from pycs.frontend.endpoints.ListProjects import ListProjects
+from pycs.frontend.endpoints.additional.FolderInformation import FolderInformation
 from pycs.frontend.endpoints.data.GetFile import GetFile
 from pycs.frontend.endpoints.data.GetPreviousAndNextFile import GetPreviousAndNextFile
 from pycs.frontend.endpoints.data.GetResizedFile import GetResizedFile
@@ -113,6 +114,12 @@ class WebServer:
         jobs.on_finish(notifications.edit_job)
         jobs.on_remove(notifications.remove_job)
 
+        # additional
+        self.__flask.add_url_rule(
+            '/folder',
+            view_func=FolderInformation.as_view('folder_information')
+        )
+
         # jobs
         self.__flask.add_url_rule(
             '/jobs',

+ 32 - 0
pycs/frontend/endpoints/additional/FolderInformation.py

@@ -0,0 +1,32 @@
+from os import path, listdir
+
+from flask import request, abort, jsonify
+from flask.views import View
+
+
+class FolderInformation(View):
+    """
+    receive a directory, check if it exists and return this as a boolean value
+    """
+    methods = ['POST']
+
+    def dispatch_request(self):
+        # extract request data
+        data = request.get_json(force=True)
+
+        if 'folder' not in data:
+            return abort(400)
+
+        folder = data['folder']
+
+        # check if directory exists
+        result = {
+            'exists': path.exists(folder)
+        }
+
+        # count files
+        if result['exists']:
+            result['count'] = len(listdir(folder))
+
+        # send result
+        return jsonify(result)

+ 5 - 2
pycs/frontend/endpoints/projects/CreateProject.py

@@ -32,6 +32,9 @@ class CreateProject(View):
         # extract request data
         data = request.get_json(force=True)
 
+        if 'name' not in data or 'description' not in data:
+            return abort(400)
+
         name = data['name']
         description = data['description']
 
@@ -42,7 +45,7 @@ class CreateProject(View):
             model = self.db.model(model_id)
 
             if model is None:
-                abort(404)
+                return abort(404)
 
             # find label provider
             if data['label'] is None:
@@ -52,7 +55,7 @@ class CreateProject(View):
                 label_provider = self.db.label_provider(label_provider_id)
 
                 if label_provider is None:
-                    abort(404)
+                    return abort(404)
 
             # create project folder
             project_folder = path.join('projects', str(uuid1()))

+ 26 - 3
webui/src/components/projects/project-creation-window.vue

@@ -30,17 +30,27 @@
       </select-input>
 
       <text-input placeholder="path to folder (leave empty to disable)"
-                  v-model="external">
+                  v-model="external"
+                  @change="checkDirectory">
         External Storage
       </text-input>
 
       <div class="external">
+        <template v-if="externalPathInformation !== null">
+          <template v-if="externalPathInformation.exists">
+            The given directory contains {{ externalPathInformation.count }} files.<br>
+          </template>
+          <template v-else>
+            The given directory is not valid.<br>
+          </template>
+        </template>
         The external storage mode disables file uploads and loads them from the given directory instead.
       </div>
     </div>
 
     <button-row class="footer">
-      <button-input @click="createProject" type="primary">
+      <button-input @click="createProject" type="primary"
+                    :disabled="externalPathInformation !== null && !externalPathInformation.exists">
         Create Project
       </button-input>
 
@@ -95,7 +105,8 @@ export default {
       labels: [],
       label: null,
 
-      external: ''
+      external: '',
+      externalPathInformation: null
     }
   },
   methods: {
@@ -129,6 +140,18 @@ export default {
           .then(response => response.json())
           .then(labels => this.labels = labels);
     },
+    checkDirectory: function (value) {
+      if (!value) {
+        this.externalPathInformation = null;
+        return;
+      }
+
+      this.$root.socket.post('/folder', {
+        'folder': this.external
+      })
+          .then(response => response.json())
+          .then(data => this.externalPathInformation = data);
+    },
     createProject: function () {
       // check input
       this.nameError = !this.name;