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

View File

@ -1,8 +1,7 @@
function sendNewJobFormData(newJobFormElement, progressModalElement, request) { function SubmitAddJobForm(newJobFormElement, progressModalElement, request) {
var formData; var formData;
var progress; var progress;
var progressModal; var progressModal;
var request;
formData = new FormData(newJobFormElement); formData = new FormData(newJobFormElement);
progressModal = M.Modal.getInstance(progressModalElement); progressModal = M.Modal.getInstance(progressModalElement);
@ -15,6 +14,7 @@ function sendNewJobFormData(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);
newJobFormElement.reset(); newJobFormElement.reset();
location.reload(); location.reload();
}); });
@ -22,22 +22,20 @@ function sendNewJobFormData(newJobFormElement, progressModalElement, request) {
progressModalElement.querySelector(".progress-in-percent").innerHTML = "0%"; progressModalElement.querySelector(".progress-in-percent").innerHTML = "0%";
progressModalElement.querySelector(".determinate").style.width = "0%"; progressModalElement.querySelector(".determinate").style.width = "0%";
}); });
progressModal.open(); progressModal.open();
request.open("POST", window.location.href); request.open("POST", window.location.href);
request.send(formData); request.send(formData);
} }
function initNewJobForm(newJobFormElement, progressModalElement) { function initAddJobForm(addJobFormElement, progressModalElement) {
var request; var request;
request = new XMLHttpRequest(); request = new XMLHttpRequest();
newJobFormElement.addEventListener("submit", function(event) { addJobFormElement.addEventListener("submit", function(event) {
event.preventDefault(); event.preventDefault();
sendNewJobFormData(newJobFormElement, progressModalElement, request); SubmitAddJobForm(addJobFormElement, progressModalElement, request);
}); });
progressModalElement.querySelector(".cancel").addEventListener("click", function(event) { progressModalElement.querySelector(".cancel").addEventListener("click", function(event) {
request.abort(); 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='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/materialize.min.css') }}" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/opaque.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/add_job.js') }}"></script>
<script src="{{ url_for('static', filename='js/opaque.js') }}"></script>
<script src="{{ url_for('static', filename='js/jsonpatch.min.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/socket.io.js') }}"></script>
<script src="{{ url_for('static', filename='js/list.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><a href="{{ url_for('main.dashboard') }}"><i class="material-icons">dashboard</i>Dashboard</a></li>
<li><div class="divider"></div></li> <li><div class="divider"></div></li>
<li><a class="subheader">Services</a></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='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='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
{% if current_user.is_administrator() %} {% if current_user.is_administrator() %}
<li><div class="divider"></div></li> <li><div class="divider"></div></li>
<li><a class="subheader">Administration</a></li> <li><a class="subheader">Administration</a></li>

View File

@ -86,7 +86,7 @@
</script> </script>
<ul id='new-job-dropdown' class='dropdown-content'> <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='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='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
</ul> </ul>
{% endblock %} {% endblock %}

View File

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

View File

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