From fede7741968a3f7dac26002a9c1a1ffd195c6380 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 16 Apr 2020 09:40:38 +0200 Subject: [PATCH] Change nopaque upload forms. Add corpus file form has an upload progress bar now. --- app/corpora/forms.py | 6 +-- app/corpora/views.py | 40 ++++++++++--------- app/static/js/nopaque.js | 11 +++-- app/templates/corpora/add_corpus_file.html.j2 | 19 ++------- app/templates/services/file-setup.html.j2 | 2 +- app/templates/services/nlp.html.j2 | 2 +- app/templates/services/ocr.html.j2 | 2 +- 7 files changed, 39 insertions(+), 43 deletions(-) diff --git a/app/corpora/forms.py b/app/corpora/forms.py index 1391d6d9..c050d6ba 100644 --- a/app/corpora/forms.py +++ b/app/corpora/forms.py @@ -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): diff --git a/app/corpora/views.py b/app/corpora/views.py index a0654ff6..5f0cdf61 100644 --- a/app/corpora/views.py +++ b/app/corpora/views.py @@ -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_, diff --git a/app/static/js/nopaque.js b/app/static/js/nopaque.js index d39d80ff..e5f3ef65 100644 --- a/app/static/js/nopaque.js +++ b/app/static/js/nopaque.js @@ -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", `${error}`); } diff --git a/app/templates/corpora/add_corpus_file.html.j2 b/app/templates/corpora/add_corpus_file.html.j2 index beb46ba3..fb7e3616 100644 --- a/app/templates/corpora/add_corpus_file.html.j2 +++ b/app/templates/corpora/add_corpus_file.html.j2 @@ -8,7 +8,7 @@
-
+ {{ add_corpus_file_form.hidden_tag() }}
@@ -69,20 +69,9 @@
  • addAdd additional metadata
    - -
    -
    - {% 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 %} -
    -
    -
    + {% 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 %}
  • diff --git a/app/templates/services/file-setup.html.j2 b/app/templates/services/file-setup.html.j2 index 6fc86ef0..4bcd7020 100644 --- a/app/templates/services/file-setup.html.j2 +++ b/app/templates/services/file-setup.html.j2 @@ -25,7 +25,7 @@

    Submit a job

    - + {{ add_job_form.hidden_tag() }}
    diff --git a/app/templates/services/nlp.html.j2 b/app/templates/services/nlp.html.j2 index 22df6cfb..382af028 100644 --- a/app/templates/services/nlp.html.j2 +++ b/app/templates/services/nlp.html.j2 @@ -43,7 +43,7 @@

    Submit a job

    - + {{ add_job_form.hidden_tag() }}
    diff --git a/app/templates/services/ocr.html.j2 b/app/templates/services/ocr.html.j2 index 6986f7f7..3483c11c 100644 --- a/app/templates/services/ocr.html.j2 +++ b/app/templates/services/ocr.html.j2 @@ -25,7 +25,7 @@

    Submit a job

    - + {{ add_job_form.hidden_tag() }}