Add create corpus form to dashboard.

This commit is contained in:
Patrick Jentsch 2019-08-01 11:47:14 +02:00
parent a5b7ccaeb7
commit 716ebdfa7c
3 changed files with 71 additions and 53 deletions

View File

@ -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 flask_login import current_user, login_required
from ..models import User from ..models import User
from ..tables import AdminUserTable, AdminUserItem from ..tables import AdminUserTable, AdminUserItem
from . import main from . import main
from .forms import CreateCorpusForm from .forms import CreateCorpusForm
from ..decorators import admin_required from ..decorators import admin_required
import hashlib
import os import os
@ -15,25 +17,23 @@ def dashboard():
if create_corpus_form.validate_on_submit(): if create_corpus_form.validate_on_submit():
app = current_app._get_current_object() app = current_app._get_current_object()
files = request.FILES id = hashlib.md5(
print(files) (current_user.username + '_' + datetime.now().isoformat()).encode()
corpus = { ).hexdigest()
'description': create_corpus_form.description.data, corpus = {'description': create_corpus_form.description.data,
'files': [], 'id': id,
'owner': current_user.id, 'creator': current_user.id,
'title': create_corpus_form.title.data 'title': create_corpus_form.title.data
} }
corpus_dir = os.path.join( dir = os.path.join(app.config['OPAQUE_FILES'], 'corpora', id)
app.config['OPAQUE_FILES'],
'corpora',
corpus['title']
)
try: try:
os.mkdir(corpus_dir) os.makedirs(dir)
except FileExistsError: except OSError:
flash('FileExistsError') flash('OSError!')
else: else:
for file in create_corpus_form.files.data:
file.save(os.path.join(dir, file.filename))
flash('Corpus created!') flash('Corpus created!')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))

View File

@ -18,23 +18,11 @@ def ocr():
id = hashlib.md5( id = hashlib.md5(
(current_user.username + '_' + datetime.now().isoformat()).encode() (current_user.username + '_' + datetime.now().isoformat()).encode()
).hexdigest() ).hexdigest()
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!')
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 ' TODO: Implement a Job class. For now a dictionary representation
' is enough. ' is enough.
''' '''
job = {'worker': None, job = {'creator': current_user.id,
'creator': current_user.id,
'id': id, 'id': id,
'requested_cpus': 2, 'requested_cpus': 2,
'requested_memory': 2048, 'requested_memory': 2048,
@ -44,6 +32,15 @@ def ocr():
}, },
'status': 'queued' 'status': 'queued'
} }
dir = os.path.join(app.config['OPAQUE_FILES'], 'jobs', id)
try:
os.makedirs(dir)
except OSError:
flash('OSError!')
else:
file = ocr_job_form.file.data
file.save(os.path.join(dir, file.filename))
''' '''
' TODO: Let the scheduler run this job in the background. ' TODO: Let the scheduler run this job in the background.
' '
@ -52,6 +49,7 @@ def ocr():
''' '''
thread = Thread(target=swarm.run, args=(job,)) thread = Thread(target=swarm.run, args=(job,))
thread.start() thread.start()
flash('Job created!')
return redirect(url_for('services.ocr')) return redirect(url_for('services.ocr'))
return render_template( return render_template(

View File

@ -37,8 +37,10 @@
<div class="col s12"> <div class="col s12">
<div class="card small"> <div class="card small">
<div class="card-content"> <div class="card-content">
<form method="POST"> <form method="POST" enctype="multipart/form-data">
{{ create_corpus_form.hidden_tag() }} {{ create_corpus_form.hidden_tag() }}
<div class="row">
<div class="col s12 m4">
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">title</i> <i class="material-icons prefix">title</i>
{{ create_corpus_form.title() }} {{ create_corpus_form.title() }}
@ -47,6 +49,8 @@
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
</div>
<div class="col s12 m8">
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">description</i> <i class="material-icons prefix">description</i>
{{ create_corpus_form.description() }} {{ create_corpus_form.description() }}
@ -55,6 +59,22 @@
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
</div>
<div class="col s12">
<div class="file-field input-field">
<div class="btn">
<span>{{ create_corpus_form.files.label.text }}</span>
{{ create_corpus_form.files(accept='.vrt') }}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
{% for error in create_corpus_form.files.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
</div>
</div>
<div class="card-action right-align"> <div class="card-action right-align">
{{ create_corpus_form.submit(class='btn') }} {{ create_corpus_form.submit(class='btn') }}
</div> </div>