2021-02-19 12:00:52 +00:00
|
|
|
from flask import (abort, flash, make_response, render_template, request,
|
|
|
|
url_for)
|
2019-07-19 11:28:17 +00:00
|
|
|
from flask_login import current_user, login_required
|
2019-10-16 14:52:05 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from . import services
|
2021-02-19 12:00:52 +00:00
|
|
|
from . import SERVICES
|
|
|
|
from .forms import AddFileSetupJobForm, AddNLPJobForm, AddOCRJobForm
|
2021-02-01 11:51:07 +00:00
|
|
|
from .. import db, socketio
|
2020-07-07 13:08:15 +00:00
|
|
|
from ..models import Job, JobInput
|
2019-08-06 12:27:41 +00:00
|
|
|
import json
|
2020-11-13 09:01:51 +00:00
|
|
|
import logging
|
2019-08-09 09:48:43 +00:00
|
|
|
import os
|
2019-07-19 11:28:17 +00:00
|
|
|
|
|
|
|
|
2019-11-08 11:21:59 +00:00
|
|
|
@services.route('/<service>', methods=['GET', 'POST'])
|
2019-08-05 13:35:18 +00:00
|
|
|
@login_required
|
2019-11-08 11:21:59 +00:00
|
|
|
def service(service):
|
|
|
|
if service not in SERVICES:
|
2019-09-24 14:55:24 +00:00
|
|
|
abort(404)
|
2020-02-07 14:21:59 +00:00
|
|
|
if service == 'corpus_analysis':
|
|
|
|
return render_template('services/{}.html.j2'.format(service),
|
|
|
|
title=SERVICES[service]['name'])
|
2021-02-19 12:00:52 +00:00
|
|
|
elif service == 'file-setup':
|
|
|
|
form = AddFileSetupJobForm(prefix='add-file-setup-job-form')
|
|
|
|
elif service == 'nlp':
|
|
|
|
form = AddNLPJobForm(prefix='add-nlp-job-form')
|
|
|
|
elif service == 'ocr':
|
|
|
|
form = AddOCRJobForm(prefix='add-ocr-job-form')
|
2020-11-13 09:01:51 +00:00
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
|
|
|
return make_response(form.errors, 400)
|
2019-11-08 11:21:59 +00:00
|
|
|
service_args = []
|
|
|
|
if service == 'nlp':
|
2021-02-19 12:00:52 +00:00
|
|
|
service_args.append('-l {}'.format(form.model.data))
|
2020-11-13 09:01:51 +00:00
|
|
|
if form.check_encoding.data:
|
2020-02-13 13:41:02 +00:00
|
|
|
service_args.append('--check-encoding')
|
2019-11-08 11:21:59 +00:00
|
|
|
if service == 'ocr':
|
2021-02-19 12:00:52 +00:00
|
|
|
service_args.append('-l {}'.format(form.model.data))
|
2020-11-13 09:01:51 +00:00
|
|
|
if form.binarization.data:
|
2020-04-03 15:36:38 +00:00
|
|
|
service_args.append('--binarize')
|
2019-11-08 11:21:59 +00:00
|
|
|
job = Job(creator=current_user,
|
2020-11-13 09:01:51 +00:00
|
|
|
description=form.description.data,
|
2019-11-08 11:21:59 +00:00
|
|
|
service=service, service_args=json.dumps(service_args),
|
2020-11-13 09:01:51 +00:00
|
|
|
service_version=form.version.data,
|
|
|
|
status='preparing', title=form.title.data)
|
2019-09-24 14:55:24 +00:00
|
|
|
db.session.add(job)
|
2021-01-28 10:25:02 +00:00
|
|
|
db.session.flush()
|
|
|
|
db.session.refresh(job)
|
2019-08-05 13:35:18 +00:00
|
|
|
try:
|
2020-11-13 09:01:51 +00:00
|
|
|
os.makedirs(job.path)
|
2019-08-05 13:35:18 +00:00
|
|
|
except OSError:
|
2020-11-13 09:01:51 +00:00
|
|
|
logging.error('Make dir {} led to an OSError!'.format(job.path))
|
2021-01-28 10:25:02 +00:00
|
|
|
db.session.rollback()
|
2020-11-13 09:01:51 +00:00
|
|
|
flash('Internal Server Error', 'error')
|
|
|
|
return make_response(
|
|
|
|
{'redirect_url': url_for('.service', service=service)}, 500)
|
2019-08-05 13:35:18 +00:00
|
|
|
else:
|
2020-11-13 09:01:51 +00:00
|
|
|
for file in form.files.data:
|
2019-10-16 14:52:05 +00:00
|
|
|
filename = secure_filename(file.filename)
|
2020-11-13 12:33:32 +00:00
|
|
|
job_input = JobInput(filename=filename, job=job)
|
2020-11-13 09:01:51 +00:00
|
|
|
file.save(job_input.path)
|
2019-10-16 14:52:05 +00:00
|
|
|
db.session.add(job_input)
|
2019-09-24 14:55:24 +00:00
|
|
|
job.status = 'submitted'
|
2019-08-12 15:03:12 +00:00
|
|
|
db.session.commit()
|
2020-11-13 09:01:51 +00:00
|
|
|
flash('Job "{}" added'.format(job.title), 'job')
|
2021-02-01 11:51:07 +00:00
|
|
|
event = 'user_{}_patch'.format(job.user_id)
|
|
|
|
jsonpatch = [{'op': 'add', 'path': '/jobs/{}'.format(job.id), 'value': job.to_dict()}] # noqa
|
|
|
|
room = 'user_{}'.format(job.user_id)
|
|
|
|
socketio.emit(event, jsonpatch, room=room)
|
2019-11-18 13:23:53 +00:00
|
|
|
return make_response(
|
|
|
|
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
|
2019-11-08 11:21:59 +00:00
|
|
|
return render_template('services/{}.html.j2'.format(service),
|
2020-11-13 09:01:51 +00:00
|
|
|
form=form, title=SERVICES[service]['name'])
|