mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 09:15:41 +00:00
86 lines
3.7 KiB
Python
86 lines
3.7 KiB
Python
from flask import (abort, 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
|
|
from . import SERVICES
|
|
from .forms import AddFileSetupJobForm, AddNLPJobForm, AddOCRJobForm
|
|
from .. import db, socketio
|
|
from ..models import Job, JobInput
|
|
import json
|
|
import logging
|
|
import os
|
|
|
|
|
|
@services.route('/<service>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def service(service):
|
|
if service not in SERVICES:
|
|
abort(404)
|
|
if service == 'corpus_analysis':
|
|
return render_template('services/{}.html.j2'.format(service),
|
|
title=SERVICES[service]['name'])
|
|
elif service == 'file_setup':
|
|
form = AddFileSetupJobForm(prefix='add-file-setup-job-form')
|
|
elif service == 'nlp':
|
|
version = request.args.get('version')
|
|
if version is None or version not in SERVICES[service]['versions']:
|
|
form = AddNLPJobForm(prefix='add-nlp-job-form')
|
|
else:
|
|
form = AddNLPJobForm(prefix='add-nlp-job-form', version=version)
|
|
form.version.data = version
|
|
elif service == 'ocr':
|
|
version = request.args.get('version')
|
|
if version is None or version not in SERVICES[service]['versions']:
|
|
form = AddOCRJobForm(prefix='add-ocr-job-form')
|
|
else:
|
|
form = AddOCRJobForm(prefix='add-ocr-job-form', version=version)
|
|
form.version.data = version
|
|
if form.is_submitted():
|
|
if not form.validate():
|
|
logging.error(form.errors)
|
|
return make_response(form.errors, 400)
|
|
service_args = []
|
|
if service == 'nlp':
|
|
service_args.append('-l {}'.format(form.language.data))
|
|
if form.check_encoding.data:
|
|
service_args.append('--check-encoding')
|
|
if service == 'ocr':
|
|
service_args.append('-l {}'.format(form.language.data))
|
|
if form.binarization.data:
|
|
service_args.append('--binarize')
|
|
job = Job(creator=current_user,
|
|
description=form.description.data,
|
|
service=service, service_args=json.dumps(service_args),
|
|
service_version=form.version.data,
|
|
status='preparing', title=form.title.data)
|
|
db.session.add(job)
|
|
db.session.flush()
|
|
db.session.refresh(job)
|
|
try:
|
|
os.makedirs(job.path)
|
|
except OSError:
|
|
logging.error('Make dir {} led to an OSError!'.format(job.path))
|
|
db.session.rollback()
|
|
flash('Internal Server Error', 'error')
|
|
return make_response(
|
|
{'redirect_url': url_for('.service', service=service)}, 500)
|
|
else:
|
|
for file in form.files.data:
|
|
filename = secure_filename(file.filename)
|
|
job_input = JobInput(filename=filename, job=job)
|
|
file.save(job_input.path)
|
|
db.session.add(job_input)
|
|
job.status = 'submitted'
|
|
db.session.commit()
|
|
flash('Job "{}" added'.format(job.title), 'job')
|
|
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)
|
|
return make_response(
|
|
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
|
|
return render_template('services/{}.html.j2'.format(service),
|
|
form=form, title=SERVICES[service]['name'],
|
|
versions=SERVICES[service]['versions'])
|