From 716ebdfa7c200f85f5a5eb4c73a2c3496424ba9f Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 1 Aug 2019 11:47:14 +0200 Subject: [PATCH] Add create corpus form to dashboard. --- app/main/views.py | 34 +++++++++--------- app/services/views.py | 38 ++++++++++---------- app/templates/main/dashboard.html.j2 | 52 +++++++++++++++++++--------- 3 files changed, 71 insertions(+), 53 deletions(-) diff --git a/app/main/views.py b/app/main/views.py index ccd1b755..b3543975 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,10 +1,12 @@ -from flask import current_app, flash, redirect, render_template, request, url_for +from datetime import datetime +from flask import current_app, flash, redirect, render_template, url_for from flask_login import current_user, login_required from ..models import User from ..tables import AdminUserTable, AdminUserItem from . import main from .forms import CreateCorpusForm from ..decorators import admin_required +import hashlib import os @@ -15,25 +17,23 @@ def dashboard(): if create_corpus_form.validate_on_submit(): app = current_app._get_current_object() - files = request.FILES - print(files) - corpus = { - 'description': create_corpus_form.description.data, - 'files': [], - 'owner': current_user.id, - 'title': create_corpus_form.title.data - } - corpus_dir = os.path.join( - app.config['OPAQUE_FILES'], - 'corpora', - corpus['title'] - ) + id = hashlib.md5( + (current_user.username + '_' + datetime.now().isoformat()).encode() + ).hexdigest() + corpus = {'description': create_corpus_form.description.data, + 'id': id, + 'creator': current_user.id, + 'title': create_corpus_form.title.data + } + dir = os.path.join(app.config['OPAQUE_FILES'], 'corpora', id) try: - os.mkdir(corpus_dir) - except FileExistsError: - flash('FileExistsError') + os.makedirs(dir) + except OSError: + flash('OSError!') else: + for file in create_corpus_form.files.data: + file.save(os.path.join(dir, file.filename)) flash('Corpus created!') return redirect(url_for('main.dashboard')) diff --git a/app/services/views.py b/app/services/views.py index 75d494dd..0e08c3d6 100644 --- a/app/services/views.py +++ b/app/services/views.py @@ -18,32 +18,29 @@ def ocr(): id = hashlib.md5( (current_user.username + '_' + datetime.now().isoformat()).encode() ).hexdigest() + ''' + ' TODO: Implement a Job class. For now a dictionary representation + ' is enough. + ''' + job = {'creator': current_user.id, + 'id': id, + 'requested_cpus': 2, + 'requested_memory': 2048, + 'service': 'ocr', + 'service_args': {'lang': ocr_job_form.language.data, + 'version': 'latest' + }, + 'status': 'queued' + } dir = os.path.join(app.config['OPAQUE_FILES'], 'jobs', id) try: - os.mkdir(dir) - except FileExistsError: - # Possible MD5 hash collision occurred. - flash('Internal error occurred, please try again!') + os.makedirs(dir) + except OSError: + flash('OSError!') else: file = ocr_job_form.file.data file.save(os.path.join(dir, file.filename)) - - ''' - ' TODO: Implement a Job class. For now a dictionary representation - ' is enough. - ''' - job = {'worker': None, - 'creator': current_user.id, - 'id': id, - 'requested_cpus': 2, - 'requested_memory': 2048, - 'service': 'ocr', - 'service_args': {'lang': ocr_job_form.language.data, - 'version': 'latest' - }, - 'status': 'queued' - } ''' ' TODO: Let the scheduler run this job in the background. ' @@ -52,6 +49,7 @@ def ocr(): ''' thread = Thread(target=swarm.run, args=(job,)) thread.start() + flash('Job created!') return redirect(url_for('services.ocr')) return render_template( diff --git a/app/templates/main/dashboard.html.j2 b/app/templates/main/dashboard.html.j2 index 62152441..86002922 100644 --- a/app/templates/main/dashboard.html.j2 +++ b/app/templates/main/dashboard.html.j2 @@ -37,23 +37,43 @@
-
+ {{ create_corpus_form.hidden_tag() }} -
- title - {{ create_corpus_form.title() }} - {{ create_corpus_form.title.label }} - {% for error in create_corpus_form.title.errors %} - {{ error }} - {% endfor %} -
-
- description - {{ create_corpus_form.description() }} - {{ create_corpus_form.description.label }} - {% for error in create_corpus_form.description.errors %} - {{ error }} - {% endfor %} +
+
+
+ title + {{ create_corpus_form.title() }} + {{ create_corpus_form.title.label }} + {% for error in create_corpus_form.title.errors %} + {{ error }} + {% endfor %} +
+
+
+
+ description + {{ create_corpus_form.description() }} + {{ create_corpus_form.description.label }} + {% for error in create_corpus_form.description.errors %} + {{ error }} + {% endfor %} +
+
+
+
+
+ {{ create_corpus_form.files.label.text }} + {{ create_corpus_form.files(accept='.vrt') }} +
+
+ +
+ {% for error in create_corpus_form.files.errors %} + {{ error }} + {% endfor %} +
+
{{ create_corpus_form.submit(class='btn') }}