From 77f1bf6cc8dfb4208e4797d7cbf8ee21643ac8b1 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 23 Jan 2020 11:19:37 +0100 Subject: [PATCH] not working test --- app/static/js/nopaque.js | 24 ++++++++++++++++++ app/templates/test/index.html.j2 | 42 +++++++++++++++----------------- app/test/events.py | 17 ++++++------- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/app/static/js/nopaque.js b/app/static/js/nopaque.js index bde7cb62..89afcea0 100644 --- a/app/static/js/nopaque.js +++ b/app/static/js/nopaque.js @@ -16,6 +16,29 @@ nopaque["foreignJobsSubscribers"] = []; // nopaque functions +nopaque["forms"] = {}; +nopaque["forms"]["init"] = function() { + for (let form of document.querySelectorAll(".nopaque-form")) { + form.addEventListener("submit", function(event) { + event.preventDefault(); + if (form.dataset.hasOwnProperty('loadingModal')) { + let loadingModalElement = document.getElementById(form.dataset.loadingModal); + M.Modal.getInstance(loadingModalElement).open(); + } + let data = {}; + for (let input of form.querySelectorAll("input")) { + if (input.type === "file") { + file = input.files[0]; + data[`${input.name}-wrapper`] = {"bytes": file, "name": file.name}; + } else { + data[input.name] = input.value; + } + } + nopaque.socket.emit(`submit-${form.id}`, data); + }); + } +} + nopaque["navigation"] = {}; nopaque["navigation"]["init"] = function() { for (let entry of document.querySelectorAll("#slide-out a:not(.subheader)")) { @@ -112,6 +135,7 @@ document.addEventListener("DOMContentLoaded", function() { {"alignment": "right", "constrainWidth": false, "coverTrigger": false}); M.Dropdown.init(document.getElementById("nav-account"), {"alignment": "right", "constrainWidth": false, "coverTrigger": false}); + nopaque.forms.init(); nopaque.navigation.init(); nopaque.socket.emit("subscribe_user_ressources"); }); diff --git a/app/templates/test/index.html.j2 b/app/templates/test/index.html.j2 index f0ec41f1..3fb77913 100644 --- a/app/templates/test/index.html.j2 +++ b/app/templates/test/index.html.j2 @@ -7,7 +7,7 @@
-
+ {{ test_form.hidden_tag() }}
@@ -45,32 +45,27 @@
+ + {% endblock %} diff --git a/app/test/events.py b/app/test/events.py index 3400cd4f..c57b57b2 100644 --- a/app/test/events.py +++ b/app/test/events.py @@ -9,16 +9,15 @@ import io @socketio.on('submit-test-form') @login_required def recv_test_form(data): - filename = secure_filename(data['file']['name']) - stream = io.BytesIO(data['file']['bytes']) - file = FileStorage(stream=stream, filename=filename) + logger.warning("data_received") + filename = secure_filename(data['file-wrapper']['name']) + stream = io.BytesIO(data['file-wrapper']['bytes']) + file = FileStorage(filename=filename, stream=stream) data['file'] = file test_form = TestForm(data=data) if test_form.validate(): - logger.warning('test_form is ok!') - socketio.emit('submit-test-form', {'status': 'success', - 'msg': 'test_form is ok!'}) + socketio.emit('submit-test-form', {'msg': {}, + 'status': 201}) else: - logger.warning('test_form is not ok!') - socketio.emit('submit-test-form', {'status': 'error', - 'msg': test_form.errors}) + socketio.emit('submit-test-form', {'msg': test_form.errors, + 'status': 400})