Change nopaque upload forms. Add corpus file form has an upload progress bar now.

This commit is contained in:
Patrick Jentsch 2020-04-16 09:40:38 +02:00
parent 4992ac73a8
commit fede774196
7 changed files with 39 additions and 43 deletions

View File

@ -30,10 +30,10 @@ class AddCorpusFileForm(FlaskForm):
if not field.data.filename.lower().endswith('.vrt'):
raise ValidationError('File does not have an approved extension: '
'.vrt')
filename = secure_filename(field.data.filename)
field.data.filename = secure_filename(field.data.filename)
for corpus_file in self.corpus.files:
if filename == corpus_file.filename:
raise ValidationError('File already registered to this corpus.')
if field.data.filename == corpus_file.filename:
raise ValidationError('File already registered to corpus.')
class EditCorpusFileForm(FlaskForm):

View File

@ -93,30 +93,32 @@ def add_corpus_file(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.creator == current_user or current_user.is_administrator()):
abort(403)
add_corpus_file_form = AddCorpusFileForm(corpus, prefix='add-job-form')
add_corpus_file_form = AddCorpusFileForm(corpus,
prefix='add-corpus-file-form')
if add_corpus_file_form.is_submitted():
if not add_corpus_file_form.validate():
logger.warning(add_corpus_file_form.errors)
return make_response(add_corpus_file_form.errors, 400)
file = add_corpus_file_form.file.data
filename = secure_filename(file.filename)
# Save the file
dir = os.path.join(str(corpus.user_id), 'corpora', str(corpus.id))
file.save(os.path.join(current_app.config['NOPAQUE_STORAGE'],
dir, filename))
ids = [field.id for field in add_corpus_file_form if not
(field.id == 'submit'
or field.id == "csrf_token"
or field.id == "file")]
data = [field.data for field in add_corpus_file_form if not
(field.id == 'submit'
or field.id == "csrf_token"
or field.id == "file")]
field_dict = dict(zip(ids, data))
corpus_file = CorpusFile(**field_dict,
corpus=corpus,
dir=dir,
filename=filename)
add_corpus_file_form.file.data.save(
os.path.join(current_app.config['NOPAQUE_STORAGE'], dir,
add_corpus_file_form.file.data.filename))
corpus_file = CorpusFile(
address=add_corpus_file_form.address.data,
author=add_corpus_file_form.author.data,
booktitle=add_corpus_file_form.booktitle.data,
chapter=add_corpus_file_form.chapter.data,
corpus=corpus,
dir=dir,
editor=add_corpus_file_form.editor.data,
filename=add_corpus_file_form.file.data.filename,
institution=add_corpus_file_form.institution.data,
journal=add_corpus_file_form.journal.data,
pages=add_corpus_file_form.pages.data,
publisher=add_corpus_file_form.publisher.data,
publishing_year=add_corpus_file_form.publishing_year.data,
school=add_corpus_file_form.school.data,
title=add_corpus_file_form.title.data)
db.session.add(corpus_file)
db.session.commit()
thread = Thread(target=edit_corpus_file_,

View File

@ -87,7 +87,7 @@ nopaque.Forms.init = function() {
var abortRequestElement, parentElement, progressElement, progressModal,
progressModalElement, request, submitElement;
for (let form of document.querySelectorAll(".nopaque-job-form")) {
for (let form of document.querySelectorAll(".nopaque-submit-form")) {
submitElement = form.querySelector('button[type="submit"]');
submitElement.addEventListener("click", function() {
for (let selectElement of form.querySelectorAll('select')) {
@ -119,7 +119,12 @@ nopaque.Forms.init = function() {
// Initialize progress modal
if (progressModalElement) {
progressModalTitleElement = progressModalElement.querySelector(".title");
progressModalTitleElement.innerText = formData.get("title");
for(let entry of formData.entries()) {
if (entry[0].endsWith("title")) {
progressModalTitleElement.innerText = entry[1];
break;
}
}
progressElement.style.width = "0%";
progressModal.open();
}
@ -135,7 +140,7 @@ nopaque.Forms.init = function() {
if (request.status === 400) {
console.log(request);
for (let [field, errors] of Object.entries(JSON.parse(this.responseText))) {
fieldElement = form.querySelector(`input[name="add-job-form-${field}"]`).closest(".input-field");
fieldElement = form.querySelector(`input[name$="${field}"]`).closest(".input-field");
for (let error of errors) {
fieldElement.insertAdjacentHTML("beforeend", `<span class="helper-text red-text">${error}</span>`);
}

View File

@ -8,7 +8,7 @@
</div>
<div class="col s12 m8">
<form class="nopaque-job-form" data-progress-modal="progress-modal">
<form class="nopaque-submit-form" data-progress-modal="progress-modal">
{{ add_corpus_file_form.hidden_tag() }}
<div class="card">
<div class="card-content">
@ -69,20 +69,9 @@
<li>
<div class="collapsible-header"><i class="material-icons">add</i>Add additional metadata</div>
<div class="collapsible-body">
<span>
<div class="row">
<div class="col s12">
{% for field in add_corpus_file_form if not (field.name == "file"
or field.name == "author"
or field.name == "submit"
or field.name == "csrf_token"
or field.name == "title"
or field.name == "publishing_year") %}
{{ macros.render_field(field)}}
{% endfor %}
</div>
</div>
</span>
{% for field in add_corpus_file_form if not (field.name.endswith(('author', 'csrf_token', 'file', 'publishing_year', 'submit', 'title'))) %}
{{ macros.render_field(field)}}
{% endfor %}
</div>
</li>
</ul>

View File

@ -25,7 +25,7 @@
<div class="col s12">
<h3>Submit a job</h3>
<div class="card">
<form class="nopaque-job-form" data-progress-modal="progress-modal">
<form class="nopaque-submit-form" data-progress-modal="progress-modal">
{{ add_job_form.hidden_tag() }}
<div class="card-content">
<div class="row">

View File

@ -43,7 +43,7 @@
<div class="col s12">
<h3>Submit a job</h3>
<div class="card">
<form class="nopaque-job-form" data-progress-modal="progress-modal">
<form class="nopaque-submit-form" data-progress-modal="progress-modal">
{{ add_job_form.hidden_tag() }}
<div class="card-content">
<div class="row">

View File

@ -25,7 +25,7 @@
<div class="col s12">
<h3>Submit a job</h3>
<div class="card">
<form class="nopaque-job-form" data-progress-modal="progress-modal">
<form class="nopaque-submit-form" data-progress-modal="progress-modal">
{{ add_job_form.hidden_tag() }}
<div class="card-content">
<div class="row">