From a1be95ff68e1c6d8121ea3c7b4f17ac3de7dc536 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Mon, 5 Aug 2019 08:36:29 +0200 Subject: [PATCH] Rename ocr job form and add nlp job form --- app/services/forms.py | 35 +++++++++++++++++++++++++++++- app/services/views.py | 12 +++++----- app/templates/services/ocr.html.j2 | 28 ++++++++++++------------ 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/app/services/forms.py b/app/services/forms.py index d0c0146b..c1e174cf 100644 --- a/app/services/forms.py +++ b/app/services/forms.py @@ -3,7 +3,7 @@ from wtforms import MultipleFileField, SelectField, StringField, SubmitField, Va from wtforms.validators import DataRequired, Length -class CreateOCRJobForm(FlaskForm): +class NewOCRJobForm(FlaskForm): description = StringField( 'Description', validators=[DataRequired(), Length(1, 64)] @@ -37,3 +37,36 @@ class CreateOCRJobForm(FlaskForm): 'File does not have an approved extension: ' '.pdf | .tif | .tiff' ) + + +class NewNLPJobForm(FlaskForm): + description = StringField( + 'Description', + validators=[DataRequired(), Length(1, 64)] + ) + files = MultipleFileField('Files', validators=[DataRequired()]) + language = SelectField( + 'Language', + choices=[('', 'Choose your option'), + ('en', 'English'), + ('fr', 'French'), + ('de', 'German'), + ('it', 'Italian'), + ('pt', 'Portuguese'), + ('es', 'Spanish') + ], + validators=[DataRequired()] + ) + submit = SubmitField('Create OCR job') + title = StringField( + 'Title', + validators=[DataRequired(), Length(1, 32)] + ) + + def validate_files(form, field): + for file in field.data: + if not file.filename.lower().endswith('.txt'): + raise ValidationError( + 'File does not have an approved extension: ' + '.txt' + ) diff --git a/app/services/views.py b/app/services/views.py index e5da12c2..ce4b0e69 100644 --- a/app/services/views.py +++ b/app/services/views.py @@ -2,7 +2,7 @@ from datetime import datetime from flask import current_app, flash, redirect, render_template, url_for from . import services from flask_login import current_user, login_required -from .forms import CreateOCRJobForm +from .forms import NewOCRJobForm from ..import swarm from threading import Thread import hashlib @@ -12,8 +12,8 @@ import os @services.route('/ocr', methods=['GET', 'POST']) @login_required def ocr(): - create_ocr_job_form = CreateOCRJobForm() - if create_ocr_job_form.validate_on_submit(): + new_ocr_job_form = NewOCRJobForm() + if new_ocr_job_form.validate_on_submit(): app = current_app._get_current_object() id = hashlib.md5( (current_user.username + '_' + datetime.now().isoformat()).encode() @@ -27,7 +27,7 @@ def ocr(): 'requested_cpus': 2, 'requested_memory': 2048, 'service': 'ocr', - 'service_args': {'lang': create_ocr_job_form.language.data, + 'service_args': {'lang': new_ocr_job_form.language.data, 'version': 'latest' }, 'status': 'queued' @@ -39,7 +39,7 @@ def ocr(): except OSError: flash('OSError!') else: - for file in create_ocr_job_form.files.data: + for file in new_ocr_job_form.files.data: file.save(os.path.join(dir, file.filename)) ''' ' TODO: Let the scheduler run this job in the background. @@ -55,5 +55,5 @@ def ocr(): return render_template( 'services/ocr.html.j2', title='Optical Character Recognition', - create_ocr_job_form=create_ocr_job_form + new_ocr_job_form=new_ocr_job_form ) diff --git a/app/templates/services/ocr.html.j2 b/app/templates/services/ocr.html.j2 index 8736faab..9f8a03b8 100644 --- a/app/templates/services/ocr.html.j2 +++ b/app/templates/services/ocr.html.j2 @@ -80,14 +80,14 @@
- {{ create_ocr_job_form.hidden_tag() }} + {{ new_ocr_job_form.hidden_tag() }}
title - {{ create_ocr_job_form.title() }} - {{ create_ocr_job_form.title.label }} - {% for error in create_ocr_job_form.title.errors %} + {{ new_ocr_job_form.title() }} + {{ new_ocr_job_form.title.label }} + {% for error in new_ocr_job_form.title.errors %} {{ error }} {% endfor %}
@@ -95,9 +95,9 @@
description - {{ create_ocr_job_form.description() }} - {{ create_ocr_job_form.description.label }} - {% for error in create_ocr_job_form.description.errors %} + {{ new_ocr_job_form.description() }} + {{ new_ocr_job_form.description.label }} + {% for error in new_ocr_job_form.description.errors %} {{ error }} {% endfor %}
@@ -105,13 +105,13 @@
- {{ create_ocr_job_form.files.label.text }} - {{ create_ocr_job_form.files(accept='application/pdf, image/tiff') }} + {{ new_ocr_job_form.files.label.text }} + {{ new_ocr_job_form.files(accept='application/pdf, image/tiff') }}
- {% for error in create_ocr_job_form.files.errors %} + {% for error in new_ocr_job_form.files.errors %} {{ error }} {% endfor %}
@@ -119,15 +119,15 @@
language - {{ create_ocr_job_form.language() }} - {{ create_ocr_job_form.language.label }} - {% for error in create_ocr_job_form.language.errors %} + {{ new_ocr_job_form.language() }} + {{ new_ocr_job_form.language.label }} + {% for error in new_ocr_job_form.language.errors %} {{ error }} {% endfor %}
- {{ create_ocr_job_form.submit(class='btn') }} + {{ new_ocr_job_form.submit(class='btn') }}