Change add job handling (preparation for api processing)

This commit is contained in:
Patrick Jentsch 2019-11-18 14:23:53 +01:00
parent 8d47d16319
commit ab0e79ac3b
2 changed files with 21 additions and 8 deletions

View File

@ -1,7 +1,8 @@
from app import db from app import db, logger
from app.jobs.forms import AddNLPJobForm, AddOCRJobForm from app.jobs.forms import AddNLPJobForm, AddOCRJobForm
from app.models import Job, JobInput 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 flask_login import current_user, login_required
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from . import services from . import services
@ -23,7 +24,9 @@ def service(service):
if service not in SERVICES: if service not in SERVICES:
abort(404) abort(404)
add_job_form = SERVICES[service]['add_job_form']() 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 = [] service_args = []
if service == 'nlp': if service == 'nlp':
service_args.append('-l {}'.format(add_job_form.language.data)) service_args.append('-l {}'.format(add_job_form.language.data))
@ -46,8 +49,11 @@ def service(service):
try: try:
os.makedirs(absolut_dir) os.makedirs(absolut_dir)
except OSError: except OSError:
flash('[ERROR]: Could not add job!')
job.delete() job.delete()
flash('Internal Server Error')
return make_response(
{'redirect_url': url_for('services.service', service='ocr')},
500)
else: else:
for file in add_job_form.files.data: for file in add_job_form.files.data:
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
@ -58,7 +64,8 @@ def service(service):
job.status = 'submitted' job.status = 'submitted'
db.session.commit() db.session.commit()
flash('Job added!') 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), return render_template('services/{}.html.j2'.format(service),
title=SERVICES[service]['name'], title=SERVICES[service]['name'],
add_job_form=add_job_form) add_job_form=add_job_form)

View File

@ -14,9 +14,15 @@ function SubmitAddJobForm(newJobFormElement, progressModalElement, request) {
progressModalElement.querySelector(".determinate").style.width = progressInPercent; progressModalElement.querySelector(".determinate").style.width = progressInPercent;
}); });
request.addEventListener("load", function(event) { request.addEventListener("load", function(event) {
console.log(request.response); if (request.status === 201) {
newJobFormElement.reset(); 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(); location.reload();
}
}); });
request.addEventListener("abort", function(event) { request.addEventListener("abort", function(event) {
progressModalElement.querySelector(".progress-in-percent").innerHTML = "0%"; progressModalElement.querySelector(".progress-in-percent").innerHTML = "0%";