From ab0e79ac3b15a5fac771257808720b2765cdb302 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Mon, 18 Nov 2019 14:23:53 +0100
Subject: [PATCH] Change add job handling (preparation for api processing)
---
app/services/views.py | 17 ++++++++++++-----
app/static/js/add_job.js | 12 +++++++++---
2 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/app/services/views.py b/app/services/views.py
index 9744b450..81d4781d 100644
--- a/app/services/views.py
+++ b/app/services/views.py
@@ -1,7 +1,8 @@
-from app import db
+from app import db, logger
from app.jobs.forms import AddNLPJobForm, AddOCRJobForm
from app.models import Job, JobInput
-from flask import abort, current_app, flash, redirect, render_template, url_for
+from flask import (abort, current_app, flash, make_response, render_template,
+ request, url_for)
from flask_login import current_user, login_required
from werkzeug.utils import secure_filename
from . import services
@@ -23,7 +24,9 @@ def service(service):
if service not in SERVICES:
abort(404)
add_job_form = SERVICES[service]['add_job_form']()
- if add_job_form.validate_on_submit():
+ if add_job_form.is_submitted():
+ if not add_job_form.validate():
+ return make_response(add_job_form.errors, 400)
service_args = []
if service == 'nlp':
service_args.append('-l {}'.format(add_job_form.language.data))
@@ -46,8 +49,11 @@ def service(service):
try:
os.makedirs(absolut_dir)
except OSError:
- flash('[ERROR]: Could not add job!')
job.delete()
+ flash('Internal Server Error')
+ return make_response(
+ {'redirect_url': url_for('services.service', service='ocr')},
+ 500)
else:
for file in add_job_form.files.data:
filename = secure_filename(file.filename)
@@ -58,7 +64,8 @@ def service(service):
job.status = 'submitted'
db.session.commit()
flash('Job added!')
- return redirect(url_for('jobs.job', job_id=job.id))
+ return make_response(
+ {'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
return render_template('services/{}.html.j2'.format(service),
title=SERVICES[service]['name'],
add_job_form=add_job_form)
diff --git a/app/static/js/add_job.js b/app/static/js/add_job.js
index f77ae78f..f0338044 100644
--- a/app/static/js/add_job.js
+++ b/app/static/js/add_job.js
@@ -14,9 +14,15 @@ function SubmitAddJobForm(newJobFormElement, progressModalElement, request) {
progressModalElement.querySelector(".determinate").style.width = progressInPercent;
});
request.addEventListener("load", function(event) {
- console.log(request.response);
- newJobFormElement.reset();
- location.reload();
+ if (request.status === 201) {
+ window.location.href = JSON.parse(this.responseText)['redirect_url'];
+ }
+ if (request.status === 400) {
+ console.log(JSON.parse(this.responseText));
+ }
+ if (request.status === 500) {
+ location.reload();
+ }
});
request.addEventListener("abort", function(event) {
progressModalElement.querySelector(".progress-in-percent").innerHTML = "0%";