nopaque/app/services/routes.py

81 lines
2.9 KiB
Python
Raw Normal View History

from flask import (abort, current_app, 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 bp
2021-02-19 12:00:52 +00:00
from . import SERVICES
from .. import db
from .forms import AddJobForms
from ..models import Job, JobInput
import json
2019-08-09 09:48:43 +00:00
import os
2019-07-19 11:28:17 +00:00
@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'])
2019-08-05 13:35:18 +00:00
@login_required
2019-11-08 11:21:59 +00:00
def service(service):
# Check if the requested service exist
if service not in SERVICES or service not in AddJobForms:
2019-09-24 14:55:24 +00:00
abort(404)
2021-11-30 15:22:16 +00:00
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)
2019-11-08 11:21:59 +00:00
service_args = []
if service == 'nlp':
2021-12-08 13:45:05 +00:00
service_args.append(f'-l {form.language.data}')
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-12-08 13:45:05 +00:00
service_args.append(f'-l {form.language.data}')
if form.binarization.data:
service_args.append('--binarize')
2021-11-30 15:22:16 +00:00
job = Job(user=current_user,
description=form.description.data,
2019-11-08 11:21:59 +00:00
service=service, service_args=json.dumps(service_args),
service_version=form.version.data,
status='preparing', title=form.title.data)
2019-09-24 14:55:24 +00:00
db.session.add(job)
db.session.flush()
db.session.refresh(job)
2019-08-05 13:35:18 +00:00
try:
os.makedirs(job.path)
2019-08-05 13:35:18 +00:00
except OSError:
2021-12-08 13:45:05 +00:00
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)
2019-08-05 13:35:18 +00:00
else:
for file in form.files.data:
2019-10-16 14:52:05 +00:00
filename = secure_filename(file.filename)
2021-11-30 15:22:16 +00:00
job_input = JobInput(
filename=filename, job=job, mimetype=file.mimetype)
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'
db.session.commit()
2021-12-08 13:45:05 +00:00
flash(f'Job "{job.title}" added', 'job')
return make_response(
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
2021-12-08 13:45:05 +00:00
return render_template(
f'services/{service.replace("-", "_")}.html.j2',
form=form,
title=title,
versions=versions
)