Some renaming and moving

This commit is contained in:
Patrick Jentsch 2019-11-08 12:21:59 +01:00
parent e0a1514084
commit 4b0ef837ad
9 changed files with 90 additions and 100 deletions

View File

@ -3,4 +3,4 @@ from flask import Blueprint
jobs = Blueprint('jobs', __name__)
from . import views
from . import forms, views

View File

@ -4,7 +4,7 @@ from wtforms import (BooleanField, MultipleFileField, SelectField, StringField,
from wtforms.validators import DataRequired, Length
class NewNLPJobForm(FlaskForm):
class AddNLPJobForm(FlaskForm):
description = StringField('Description',
validators=[DataRequired(), Length(1, 255)])
files = MultipleFileField('Files', validators=[DataRequired()])
@ -34,7 +34,7 @@ class NewNLPJobForm(FlaskForm):
)
class NewOCRJobForm(FlaskForm):
class AddOCRJobForm(FlaskForm):
binarization = BooleanField('Binarazation')
description = StringField('Description',
validators=[DataRequired(), Length(1, 255)])

View File

@ -1,9 +1,9 @@
from app.models import Job, JobInput, JobResult
from app.utils import background_delete_job
from flask import (abort, current_app, flash, redirect, render_template,
send_from_directory, url_for)
from flask_login import current_user, login_required
from . import jobs
from ..models import Job, JobInput, JobResult
import os
import threading

View File

@ -1,72 +1,65 @@
from app import db
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_login import current_user, login_required
from werkzeug.utils import secure_filename
from . import services
from .forms import NewNLPJobForm, NewOCRJobForm
from .. import db
from ..models import Job, JobInput
import json
import os
SERVICES = {'nlp': {'name': 'Natural Language Processing',
'resources': {'mem_mb': 4096, 'n_cores': 2},
'new_job_form': NewNLPJobForm},
'add_job_form': AddNLPJobForm},
'ocr': {'name': 'Optical Character Recognition',
'resources': {'mem_mb': 8192, 'n_cores': 4},
'new_job_form': NewOCRJobForm}}
'add_job_form': AddOCRJobForm}}
@services.route('/<service_handle>', methods=['GET', 'POST'])
@services.route('/<service>', methods=['GET', 'POST'])
@login_required
def service(service_handle):
if service_handle not in SERVICES:
def service(service):
if service not in SERVICES:
abort(404)
new_job_form = SERVICES[service_handle]['new_job_form']()
if new_job_form.validate_on_submit():
_service_args = ['-l {}'.format(new_job_form.language.data)]
if service_handle == 'ocr':
if not new_job_form.binarization.data:
_service_args.append('--skip-binarisation')
job = Job(
creator=current_user,
description=new_job_form.description.data,
mem_mb=SERVICES[service_handle]['resources']['mem_mb'],
n_cores=SERVICES[service_handle]['resources']['n_cores'],
service=service_handle,
service_args=json.dumps(_service_args),
service_version=new_job_form.version.data,
status='preparing',
title=new_job_form.title.data
)
add_job_form = SERVICES[service]['add_job_form']()
if add_job_form.validate_on_submit():
service_args = []
if service == 'nlp':
service_args.append('-l {}'.format(add_job_form.language.data))
if service == 'ocr':
service_args.append('-l {}'.format(add_job_form.language.data))
if not add_job_form.binarization.data:
service_args.append('--skip-binarisation')
job = Job(creator=current_user,
description=add_job_form.description.data,
mem_mb=SERVICES[service]['resources']['mem_mb'],
n_cores=SERVICES[service]['resources']['n_cores'],
service=service, service_args=json.dumps(service_args),
service_version=add_job_form.version.data,
status='preparing', title=add_job_form.title.data)
db.session.add(job)
db.session.commit()
dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
str(job.user_id),
'jobs',
str(job.id))
relative_dir = os.path.join(str(job.user_id), 'jobs', str(job.id))
absolut_dir = os.path.join(
current_app.config['OPAQUE_STORAGE_DIRECTORY'], relative_dir)
try:
os.makedirs(dir)
os.makedirs(absolut_dir)
except OSError:
flash('OSError!')
db.session.remove(job)
flash('[OSError] Could not add job!')
db.session.delete(job)
db.session.commit()
else:
for file in new_job_form.files.data:
for file in add_job_form.files.data:
filename = secure_filename(file.filename)
file.save(os.path.join(dir, filename))
job_input = JobInput(
dir=os.path.join(str(job.user_id), 'jobs', str(job.id)),
filename=filename,
job=job
)
file.save(os.path.join(absolut_dir, filename))
job_input = JobInput(dir=relative_dir, filename=filename,
job=job)
db.session.add(job_input)
job.status = 'submitted'
db.session.commit()
flash('Job created!')
return redirect(
url_for('services.service', service_handle=service_handle)
)
return render_template('services/{}.html.j2'.format(service_handle),
title=SERVICES[service_handle]['name'],
new_job_form=new_job_form)
flash('Job added!')
return redirect(url_for('jobs.job', job_id=job.id))
return render_template('services/{}.html.j2'.format(service),
title=SERVICES[service]['name'],
add_job_form=add_job_form)

View File

@ -1,8 +1,7 @@
function sendNewJobFormData(newJobFormElement, progressModalElement, request) {
function SubmitAddJobForm(newJobFormElement, progressModalElement, request) {
var formData;
var progress;
var progressModal;
var request;
formData = new FormData(newJobFormElement);
progressModal = M.Modal.getInstance(progressModalElement);
@ -15,6 +14,7 @@ function sendNewJobFormData(newJobFormElement, progressModalElement, request) {
progressModalElement.querySelector(".determinate").style.width = progressInPercent;
});
request.addEventListener("load", function(event) {
console.log(request.response);
newJobFormElement.reset();
location.reload();
});
@ -22,22 +22,20 @@ function sendNewJobFormData(newJobFormElement, progressModalElement, request) {
progressModalElement.querySelector(".progress-in-percent").innerHTML = "0%";
progressModalElement.querySelector(".determinate").style.width = "0%";
});
progressModal.open();
request.open("POST", window.location.href);
request.send(formData);
}
function initNewJobForm(newJobFormElement, progressModalElement) {
function initAddJobForm(addJobFormElement, progressModalElement) {
var request;
request = new XMLHttpRequest();
newJobFormElement.addEventListener("submit", function(event) {
addJobFormElement.addEventListener("submit", function(event) {
event.preventDefault();
sendNewJobFormData(newJobFormElement, progressModalElement, request);
SubmitAddJobForm(addJobFormElement, progressModalElement, request);
});
progressModalElement.querySelector(".cancel").addEventListener("click", function(event) {
request.abort();
});

View File

@ -11,8 +11,7 @@
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='fonts/material-icons/material-icons.css') }}">
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/materialize.min.css') }}" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/opaque.css') }}" media="screen,projection"/>
<script src="{{ url_for('static', filename='js/Animations.js') }}"></script>
<script src="{{ url_for('static', filename='js/opaque.js') }}"></script>
<script src="{{ url_for('static', filename='js/add_job.js') }}"></script>
<script src="{{ url_for('static', filename='js/jsonpatch.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/socket.io.js') }}"></script>
<script src="{{ url_for('static', filename='js/list.js') }}"></script>
@ -137,8 +136,8 @@
<li><a href="{{ url_for('main.dashboard') }}"><i class="material-icons">dashboard</i>Dashboard</a></li>
<li><div class="divider"></div></li>
<li><a class="subheader">Services</a></li>
<li><a href="{{ url_for('services.service', service_handle='nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.service', service_handle='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
<li><a href="{{ url_for('services.service', service='nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.service', service='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
{% if current_user.is_administrator() %}
<li><div class="divider"></div></li>
<li><a class="subheader">Administration</a></li>

View File

@ -86,7 +86,7 @@
</script>
<ul id='new-job-dropdown' class='dropdown-content'>
<li><a href="{{ url_for('services.service', service_handle='nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.service', service_handle='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
<li><a href="{{ url_for('services.service', service='nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.service', service='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
</ul>
{% endblock %}

View File

@ -53,16 +53,16 @@
<div class="col s12">
<div class="card">
<form method="POST" enctype="multipart/form-data" id="new-nlp-job-form">
<form method="POST" enctype="multipart/form-data" id="add-job-form">
<div class="card-content">
{{ new_job_form.hidden_tag() }}
{{ add_job_form.hidden_tag() }}
<div class="row">
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">title</i>
{{ new_job_form.title(data_length='32') }}
{{ new_job_form.title.label }}
{% for error in new_job_form.title.errors %}
{{ add_job_form.title(data_length='32') }}
{{ add_job_form.title.label }}
{% for error in add_job_form.title.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -70,9 +70,9 @@
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">language</i>
{{ new_job_form.language() }}
{{ new_job_form.language.label }}
{% for error in new_job_form.language.errors %}
{{ add_job_form.language() }}
{{ add_job_form.language.label }}
{% for error in add_job_form.language.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -80,9 +80,9 @@
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">language</i>
{{ new_job_form.version() }}
{{ new_job_form.version.label }}
{% for error in new_job_form.version.errors %}
{{ add_job_form.version() }}
{{ add_job_form.version.label }}
{% for error in add_job_form.version.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -92,13 +92,13 @@
<div class="col s12 m6">
<div class="file-field input-field">
<div class="btn">
<span>{{ new_job_form.files.label.text }}</span>
{{ new_job_form.files(accept='text/plain') }}
<span>{{ add_job_form.files.label.text }}</span>
{{ add_job_form.files(accept='text/plain') }}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
{% for error in new_job_form.files.errors %}
{% for error in add_job_form.files.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -106,9 +106,9 @@
<div class="col s12 m6">
<div class="input-field">
<i class="material-icons prefix">description</i>
{{ new_job_form.description(data_length='255') }}
{{ new_job_form.description.label }}
{% for error in new_job_form.description.errors %}
{{ add_job_form.description(data_length='255') }}
{{ add_job_form.description.label }}
{% for error in add_job_form.description.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -136,7 +136,7 @@
</div>
<script>
initNewJobForm(document.getElementById("new-nlp-job-form"),
initAddJobForm(document.getElementById("add-job-form"),
document.getElementById("progress-modal"));
</script>
{% endblock %}

View File

@ -54,16 +54,16 @@
<div class="col s12">
<div class="card">
<form method="POST" enctype="multipart/form-data" id="new-ocr-job-form">
<form method="POST" enctype="multipart/form-data" id="add-job-form">
<div class="card-content">
{{ new_job_form.hidden_tag() }}
{{ add_job_form.hidden_tag() }}
<div class="row">
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">title</i>
{{ new_job_form.title(data_length='32') }}
{{ new_job_form.title.label }}
{% for error in new_job_form.title.errors %}
{{ add_job_form.title(data_length='32') }}
{{ add_job_form.title.label }}
{% for error in add_job_form.title.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -71,9 +71,9 @@
<div class="col s12 m8">
<div class="input-field">
<i class="material-icons prefix">description</i>
{{ new_job_form.description(data_length='255') }}
{{ new_job_form.description.label }}
{% for error in new_job_form.description.errors %}
{{ add_job_form.description(data_length='255') }}
{{ add_job_form.description.label }}
{% for error in add_job_form.description.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -81,13 +81,13 @@
<div class="col s12 m5">
<div class="file-field input-field">
<div class="btn">
<span>{{ new_job_form.files.label.text }}</span>
{{ new_job_form.files(accept='application/pdf, image/tiff') }}
<span>{{ add_job_form.files.label.text }}</span>
{{ add_job_form.files(accept='application/pdf, image/tiff') }}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
{% for error in new_job_form.files.errors %}
{% for error in add_job_form.files.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -95,9 +95,9 @@
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">language</i>
{{ new_job_form.language() }}
{{ new_job_form.language.label }}
{% for error in new_job_form.language.errors %}
{{ add_job_form.language() }}
{{ add_job_form.language.label }}
{% for error in add_job_form.language.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -105,9 +105,9 @@
<div class="col s12 m3">
<div class="input-field">
<i class="material-icons prefix">apps</i>
{{ new_job_form.version() }}
{{ new_job_form.version.label }}
{% for error in new_job_form.version.errors %}
{{ add_job_form.version() }}
{{ add_job_form.version.label }}
{% for error in add_job_form.version.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -194,7 +194,7 @@
<div class="col s3">
<div class="switch">
<label>
{{ new_job_form.binarization() }}
{{ add_job_form.binarization() }}
<span class="lever"></span>
</label>
</div>
@ -224,7 +224,7 @@
</div>
<script>
initNewJobForm(document.getElementById("new-ocr-job-form"),
initAddJobForm(document.getElementById("add-job-form"),
document.getElementById("progress-modal"));
</script>
{% endblock %}