From 787bf4c3772846b2375e898e57d22b52f683757a Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Tue, 24 Sep 2019 16:55:24 +0200 Subject: [PATCH] Unify service pages. --- app/services/views.py | 119 +++++++++------------------ app/templates/base.html.j2 | 4 +- app/templates/main/dashboard.html.j2 | 4 +- app/templates/services/nlp.html.j2 | 34 ++++---- app/templates/services/ocr.html.j2 | 36 ++++---- 5 files changed, 80 insertions(+), 117 deletions(-) diff --git a/app/services/views.py b/app/services/views.py index 6d785bfb..d60d7120 100644 --- a/app/services/views.py +++ b/app/services/views.py @@ -1,4 +1,4 @@ -from flask import current_app, flash, redirect, render_template, url_for +from flask import abort, current_app, flash, redirect, render_template, url_for from . import services from flask_login import current_user, login_required from .forms import NewOCRJobForm, NewNLPJobForm @@ -8,93 +8,56 @@ import json import os -@services.route('/nlp', methods=['GET', 'POST']) +SERVICE_REQUIREMENTS = {'nlp': {'mem_mb': 4096, 'n_cores': 2}, + 'ocr': {'mem_mb': 8192, 'n_cores': 4}} + + +@services.route('/', methods=['GET', 'POST']) @login_required -def nlp(): - new_nlp_job_form = NewNLPJobForm() - if new_nlp_job_form.validate_on_submit(): - nlp_job = Job(creator=current_user, - description=new_nlp_job_form.description.data, - mem_mb=4096, - n_cores=2, - service='nlp', - service_args=json.dumps( - ['-l {}'.format(new_nlp_job_form.language.data)] - ), - service_version=new_nlp_job_form.version.data, - status='preparing', - title=new_nlp_job_form.title.data) - - db.session.add(nlp_job) +def service(service_handle): + if service_handle not in ['nlp', 'ocr']: + abort(404) + elif service_handle == 'nlp': + page_title = 'Natural Language Processing' + new_job_form = NewNLPJobForm() + elif service_handle == 'ocr': + page_title = 'Optical Character Recognition' + new_job_form = NewOCRJobForm() + if new_job_form.validate_on_submit(): + _service_args = ['-l {}'.format(new_job_form.language.data)] + if service_handle == 'ocr': + if not new_job_form.binarization.data: + _service_args.append('--skip-binarisation') + job = Job( + creator=current_user, + description=new_job_form.description.data, + mem_mb=SERVICE_REQUIREMENTS[service_handle]['mem_mb'], + n_cores=SERVICE_REQUIREMENTS[service_handle]['n_cores'], + service=service_handle, + service_args=json.dumps(_service_args), + service_version=new_job_form.version.data, + status='preparing', + title=new_job_form.title.data + ) + db.session.add(job) db.session.commit() - dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'], - str(nlp_job.user_id), + str(job.user_id), 'jobs', - str(nlp_job.id)) - + str(job.id)) try: os.makedirs(dir) except OSError: flash('OSError!') - db.session.remove(nlp_job) + db.session.remove(job) db.session.commit() else: - for file in new_nlp_job_form.files.data: + for file in new_job_form.files.data: file.save(os.path.join(dir, file.filename)) - nlp_job.status = 'submitted' + job.status = 'submitted' db.session.commit() flash('Job created!') - return redirect(url_for('services.nlp')) - - return render_template('services/nlp.html.j2', - title='Natrual Language Processing', - new_nlp_job_form=new_nlp_job_form) - - -@services.route('/ocr', methods=['GET', 'POST']) -@login_required -def ocr(): - new_ocr_job_form = NewOCRJobForm() - if new_ocr_job_form.validate_on_submit(): - ocr_job = Job(creator=current_user, - description=new_ocr_job_form.description.data, - mem_mb=8192, - n_cores=4, - service='ocr', - service_args=json.dumps( - [ - '-l {}'.format(new_ocr_job_form.language.data), - '' if new_ocr_job_form.binarization.data - else '--skip-binarisation' - ] - ), - service_version=new_ocr_job_form.version.data, - status='preparing', - title=new_ocr_job_form.title.data) - - db.session.add(ocr_job) - db.session.commit() - - dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'], - str(ocr_job.user_id), - 'jobs', - str(ocr_job.id)) - - try: - os.makedirs(dir) - except OSError: - flash('OSError!') - db.session.remove(ocr_job) - db.session.commit() - else: - for file in new_ocr_job_form.files.data: - file.save(os.path.join(dir, file.filename)) - ocr_job.status = 'submitted' - db.session.commit() - flash('Job created!') - return redirect(url_for('services.ocr')) - - return render_template('services/ocr.html.j2', - title='Optical Character Recognition', - new_ocr_job_form=new_ocr_job_form) + return redirect(url_for('services.{}'.format(service_handle))) + return render_template('services/{}.html.j2'.format(service_handle), + title=page_title, + new_job_form=new_job_form) diff --git a/app/templates/base.html.j2 b/app/templates/base.html.j2 index 68a609e6..06d591d6 100644 --- a/app/templates/base.html.j2 +++ b/app/templates/base.html.j2 @@ -148,8 +148,8 @@
  • dashboardDashboard
  • Services
  • -
  • format_textdirection_l_to_rNLP
  • -
  • find_in_pageOCR
  • +
  • format_textdirection_l_to_rNLP
  • +
  • find_in_pageOCR
  • {% if current_user.is_administrator() %}
  • Administration
  • diff --git a/app/templates/main/dashboard.html.j2 b/app/templates/main/dashboard.html.j2 index 036384d2..d7f13b51 100644 --- a/app/templates/main/dashboard.html.j2 +++ b/app/templates/main/dashboard.html.j2 @@ -134,7 +134,7 @@ {% endblock %} diff --git a/app/templates/services/nlp.html.j2 b/app/templates/services/nlp.html.j2 index 6502df7c..c5353ab1 100644 --- a/app/templates/services/nlp.html.j2 +++ b/app/templates/services/nlp.html.j2 @@ -55,14 +55,14 @@
    - {{ new_nlp_job_form.hidden_tag() }} + {{ new_job_form.hidden_tag() }}
    title - {{ new_nlp_job_form.title(data_length='32') }} - {{ new_nlp_job_form.title.label }} - {% for error in new_nlp_job_form.title.errors %} + {{ new_job_form.title(data_length='32') }} + {{ new_job_form.title.label }} + {% for error in new_job_form.title.errors %} {{ error }} {% endfor %}
    @@ -70,9 +70,9 @@
    language - {{ new_nlp_job_form.language() }} - {{ new_nlp_job_form.language.label }} - {% for error in new_nlp_job_form.language.errors %} + {{ new_job_form.language() }} + {{ new_job_form.language.label }} + {% for error in new_job_form.language.errors %} {{ error }} {% endfor %}
    @@ -80,9 +80,9 @@
    language - {{ new_nlp_job_form.version() }} - {{ new_nlp_job_form.version.label }} - {% for error in new_nlp_job_form.version.errors %} + {{ new_job_form.version() }} + {{ new_job_form.version.label }} + {% for error in new_job_form.version.errors %} {{ error }} {% endfor %}
    @@ -92,13 +92,13 @@
    - {{ new_nlp_job_form.files.label.text }} - {{ new_nlp_job_form.files(accept='text/plain') }} + {{ new_job_form.files.label.text }} + {{ new_job_form.files(accept='text/plain') }}
    - {% for error in new_nlp_job_form.files.errors %} + {% for error in new_job_form.files.errors %} {{ error }} {% endfor %}
    @@ -106,9 +106,9 @@
    description - {{ new_nlp_job_form.description(data_length='255') }} - {{ new_nlp_job_form.description.label }} - {% for error in new_nlp_job_form.description.errors %} + {{ new_job_form.description(data_length='255') }} + {{ new_job_form.description.label }} + {% for error in new_job_form.description.errors %} {{ error }} {% endfor %}
    @@ -116,7 +116,7 @@
    - {{ new_nlp_job_form.submit(class='btn') }} + {{ new_job_form.submit(class='btn') }}
    diff --git a/app/templates/services/ocr.html.j2 b/app/templates/services/ocr.html.j2 index de6da236..c04c09c8 100644 --- a/app/templates/services/ocr.html.j2 +++ b/app/templates/services/ocr.html.j2 @@ -56,14 +56,14 @@
    - {{ new_ocr_job_form.hidden_tag() }} + {{ new_job_form.hidden_tag() }}
    title - {{ new_ocr_job_form.title(data_length='32') }} - {{ new_ocr_job_form.title.label }} - {% for error in new_ocr_job_form.title.errors %} + {{ new_job_form.title(data_length='32') }} + {{ new_job_form.title.label }} + {% for error in new_job_form.title.errors %} {{ error }} {% endfor %}
    @@ -71,9 +71,9 @@
    description - {{ new_ocr_job_form.description(data_length='255') }} - {{ new_ocr_job_form.description.label }} - {% for error in new_ocr_job_form.description.errors %} + {{ new_job_form.description(data_length='255') }} + {{ new_job_form.description.label }} + {% for error in new_job_form.description.errors %} {{ error }} {% endfor %}
    @@ -81,13 +81,13 @@
    - {{ new_ocr_job_form.files.label.text }} - {{ new_ocr_job_form.files(accept='application/pdf, image/tiff') }} + {{ new_job_form.files.label.text }} + {{ new_job_form.files(accept='application/pdf, image/tiff') }}
    - {% for error in new_ocr_job_form.files.errors %} + {% for error in new_job_form.files.errors %} {{ error }} {% endfor %}
    @@ -95,9 +95,9 @@
    language - {{ new_ocr_job_form.language() }} - {{ new_ocr_job_form.language.label }} - {% for error in new_ocr_job_form.language.errors %} + {{ new_job_form.language() }} + {{ new_job_form.language.label }} + {% for error in new_job_form.language.errors %} {{ error }} {% endfor %}
    @@ -105,9 +105,9 @@
    apps - {{ new_ocr_job_form.version() }} - {{ new_ocr_job_form.version.label }} - {% for error in new_ocr_job_form.version.errors %} + {{ new_job_form.version() }} + {{ new_job_form.version.label }} + {% for error in new_job_form.version.errors %} {{ error }} {% endfor %}
    @@ -194,7 +194,7 @@
    @@ -204,7 +204,7 @@
    - {{ new_ocr_job_form.submit(class='btn') }} + {{ new_job_form.submit(class='btn') }}