mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-26 03:14:19 +00:00
81 lines
2.9 KiB
Python
81 lines
2.9 KiB
Python
from flask import (abort, current_app, flash, make_response, render_template,
|
|
request, url_for)
|
|
from flask_login import current_user, login_required
|
|
from werkzeug.utils import secure_filename
|
|
from . import bp
|
|
from . import SERVICES
|
|
from .. import db
|
|
from .forms import AddJobForms
|
|
from ..models import Job, JobInput
|
|
import json
|
|
import os
|
|
|
|
|
|
@bp.route('/corpus-analysis')
|
|
@login_required
|
|
def corpus_analysis():
|
|
return render_template('services/corpus_analysis.html.j2',
|
|
title='Corpus analysis')
|
|
|
|
|
|
@bp.route('/<service>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def service(service):
|
|
# Check if the requested service exist
|
|
if service not in SERVICES or service not in AddJobForms:
|
|
abort(404)
|
|
version = request.args.get(
|
|
'version', SERVICES[service]['versions']['latest'])
|
|
if version not in SERVICES[service]['versions']:
|
|
abort(404)
|
|
form = AddJobForms[service](prefix='add-job-form', version=version)
|
|
form.version.data = version
|
|
title = SERVICES[service]['name']
|
|
versions = SERVICES[service]['versions']
|
|
if form.is_submitted():
|
|
if not form.validate():
|
|
return make_response(form.errors, 400)
|
|
service_args = []
|
|
if service == 'nlp':
|
|
service_args.append(f'-l {form.language.data}')
|
|
if form.check_encoding.data:
|
|
service_args.append('--check-encoding')
|
|
if service == 'ocr':
|
|
service_args.append(f'-l {form.language.data}')
|
|
if form.binarization.data:
|
|
service_args.append('--binarize')
|
|
job = Job(user=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:
|
|
current_app.logger.error(f'Make dir {job.path} led to an OSError!')
|
|
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, mimetype=file.mimetype)
|
|
file.save(job_input.path)
|
|
db.session.add(job_input)
|
|
job.status = 'submitted'
|
|
db.session.commit()
|
|
flash(f'Job "{job.title}" added', 'job')
|
|
return make_response(
|
|
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
|
|
return render_template(
|
|
f'services/{service.replace("-", "_")}.html.j2',
|
|
form=form,
|
|
title=title,
|
|
versions=versions
|
|
)
|