From 3aafe664a39fcf911e5e1494a4e7e3a08c948cbf Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Fri, 17 Apr 2020 11:04:09 +0200 Subject: [PATCH 1/4] Add macros and use them --- app/corpora/views.py | 51 ++++++++----- app/models.py | 3 - app/static/js/nopaque.js | 9 +-- app/templates/admin/edit_user.html.j2 | 41 ++-------- app/templates/auth/login.html.j2 | 30 ++------ app/templates/auth/register.html.j2 | 40 ++-------- app/templates/auth/reset_password.html.j2 | 20 +---- .../auth/reset_password_request.html.j2 | 12 +-- app/templates/corpora/add_corpus.html.j2 | 22 +----- app/templates/corpora/add_corpus_file.html.j2 | 51 +++---------- app/templates/corpora/analyse_corpus.html.j2 | 23 +----- .../corpora/edit_corpus_file.html.j2 | 49 +++--------- app/templates/macros.jinja | 30 -------- app/templates/main/feedback.html.j2 | 2 +- app/templates/main/index.html.j2 | 28 +------ app/templates/main/poster.html.j2 | 2 +- app/templates/materialize.jinja | 76 +++++++++++++++++++ app/templates/nopaque.html.j2 | 2 +- app/templates/profile/settings.html.j2 | 48 +++--------- app/templates/services/file-setup.html.j2 | 46 ++--------- app/templates/services/nlp.html.j2 | 67 ++++------------ app/templates/services/ocr.html.j2 | 55 ++------------ 22 files changed, 205 insertions(+), 502 deletions(-) delete mode 100644 app/templates/macros.jinja create mode 100644 app/templates/materialize.jinja diff --git a/app/corpora/views.py b/app/corpora/views.py index 5f0cdf61..96665d50 100644 --- a/app/corpora/views.py +++ b/app/corpora/views.py @@ -1,9 +1,7 @@ -from app import logger from flask import (abort, current_app, flash, make_response, redirect, request, render_template, url_for, send_from_directory) from flask_login import current_user, login_required from threading import Thread -from werkzeug.utils import secure_filename from . import corpora from .background_functions import (delete_corpus_, delete_corpus_file_, edit_corpus_file_) @@ -129,10 +127,10 @@ def add_corpus_file(corpus_id): return make_response( {'redirect_url': url_for('corpora.corpus', corpus_id=corpus.id)}, 201) - # return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) return render_template('corpora/add_corpus_file.html.j2', + corpus=corpus, add_corpus_file_form=add_corpus_file_form, - corpus=corpus, title='Add corpus file') + title='Add corpus file') @corpora.route('//files//delete') @@ -170,25 +168,28 @@ def download_corpus_file(corpus_id, corpus_file_id): methods=['GET', 'POST']) @login_required def edit_corpus_file(corpus_id, corpus_file_id): + corpus = Corpus.query.get_or_404(corpus_id) corpus_file = CorpusFile.query.get_or_404(corpus_file_id) if not corpus_file.corpus_id == corpus_id: abort(404) if not (corpus_file.corpus.creator == current_user or current_user.is_administrator()): abort(403) - edit_corpus_file_form = EditCorpusFileForm() + edit_corpus_file_form = EditCorpusFileForm(prefix='edit-corpus-file-form') if edit_corpus_file_form.validate_on_submit(): - ids = [field.id for field in edit_corpus_file_form if not - (field.id == 'submit' - or field.id == "csrf_token" - or field.id == "file")] - data = [field.data for field in edit_corpus_file_form if not - (field.id == 'submit' - or field.id == "csrf_token" - or field.id == "file")] - field_dict = dict(zip(ids, data)) - stmt = db.update(CorpusFile).where(CorpusFile.id==corpus_file_id).values(**field_dict) - db.session.execute(stmt) + corpus_file.address = edit_corpus_file_form.address.data + corpus_file.author = edit_corpus_file_form.author.data + corpus_file.booktitle = edit_corpus_file_form.booktitle.data + corpus_file.chapter = edit_corpus_file_form.chapter.data + corpus_file.editor = edit_corpus_file_form.editor.data + corpus_file.institution = edit_corpus_file_form.institution.data + corpus_file.journal = edit_corpus_file_form.journal.data + corpus_file.pages = edit_corpus_file_form.pages.data + corpus_file.publisher = edit_corpus_file_form.publisher.data + corpus_file.publishing_year = \ + edit_corpus_file_form.publishing_year.data + corpus_file.school = edit_corpus_file_form.school.data + corpus_file.title = edit_corpus_file_form.title.data db.session.commit() thread = Thread(target=edit_corpus_file_, args=(current_app._get_current_object(), @@ -196,9 +197,23 @@ def edit_corpus_file(corpus_id, corpus_file_id): thread.start() flash('Corpus file edited!') return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) + # If no form is submitted or valid, fill out fields with current values + edit_corpus_file_form.address.data = corpus_file.address + edit_corpus_file_form.author.data = corpus_file.author + edit_corpus_file_form.booktitle.data = corpus_file.booktitle + edit_corpus_file_form.chapter.data = corpus_file.chapter + edit_corpus_file_form.editor.data = corpus_file.editor + edit_corpus_file_form.institution.data = corpus_file.institution + edit_corpus_file_form.journal.data = corpus_file.journal + edit_corpus_file_form.pages.data = corpus_file.pages + edit_corpus_file_form.publisher.data = corpus_file.publisher + edit_corpus_file_form.publishing_year.data = corpus_file.publishing_year + edit_corpus_file_form.school.data = corpus_file.school + edit_corpus_file_form.title.data = corpus_file.title return render_template('corpora/edit_corpus_file.html.j2', + corpus_file=corpus_file, corpus=corpus, edit_corpus_file_form=edit_corpus_file_form, - corpus_file=corpus_file, title='Edit corpus file') + title='Edit corpus file') @corpora.route('//prepare') @@ -207,7 +222,7 @@ def prepare_corpus(corpus_id): corpus = Corpus.query.get_or_404(corpus_id) if not (corpus.creator == current_user or current_user.is_administrator()): abort(403) - if len(corpus.files.all()) > 0: + if corpus.files.all(): corpus.status = 'submitted' db.session.commit() flash('Corpus marked for preparation!') diff --git a/app/models.py b/app/models.py index d4044e57..4925be71 100644 --- a/app/models.py +++ b/app/models.py @@ -256,7 +256,6 @@ class JobInput(db.Model): def to_dict(self): return {'id': self.id, - 'dir': self.dir, 'filename': self.filename, 'job_id': self.job_id} @@ -280,7 +279,6 @@ class JobResult(db.Model): def to_dict(self): return {'id': self.id, - 'dir': self.dir, 'filename': self.filename, 'job_id': self.job_id} @@ -432,7 +430,6 @@ class CorpusFile(db.Model): 'author': self.author, 'booktitle': self.booktitle, 'chapter': self.chapter, - 'dir': self.dir, 'editor': self.editor, 'filename': self.filename, 'institution': self.institution, diff --git a/app/static/js/nopaque.js b/app/static/js/nopaque.js index e5f3ef65..cbefbc37 100644 --- a/app/static/js/nopaque.js +++ b/app/static/js/nopaque.js @@ -113,18 +113,11 @@ nopaque.Forms.init = function() { } form.addEventListener("submit", function(event) { event.preventDefault(); - var formData, progressModalTitleElement; + var formData; formData = new FormData(form); // Initialize progress modal if (progressModalElement) { - progressModalTitleElement = progressModalElement.querySelector(".title"); - for(let entry of formData.entries()) { - if (entry[0].endsWith("title")) { - progressModalTitleElement.innerText = entry[1]; - break; - } - } progressElement.style.width = "0%"; progressModal.open(); } diff --git a/app/templates/admin/edit_user.html.j2 b/app/templates/admin/edit_user.html.j2 index dd4e640b..42187b9e 100644 --- a/app/templates/admin/edit_user.html.j2 +++ b/app/templates/admin/edit_user.html.j2 @@ -12,44 +12,13 @@
{{ edit_user_form.hidden_tag() }} -
- account_circle - {{ edit_user_form.username() }} - {{ edit_user_form.username.label }} - {% for error in edit_user_form.username.errors %} - {{ error }} - {% endfor %} -
-
- mail - {{ edit_user_form.email() }} - {{ edit_user_form.email.label }} - {% for error in edit_user_form.email.errors %} - {{ error }} - {% endfor %} -
-
- swap_vert - {{ edit_user_form.role() }} - {{ edit_user_form.role.label }} - {% for error in edit_user_form.role.errors %} - {{ error }} - {% endfor %} -
-
- check - - {% for error in edit_user_form.confirmed.errors %} - {{ error }} - {% endfor %} -
+ {{ materialize.field(edit_user_form.username, data_length='64', material_icon='account_circle') }} + {{ materialize.field(edit_user_form.email, class_='validate', material_icon='email', type='email') }} + {{ materialize.field(edit_user_form.role, material_icon='swap_vert') }} + {{ materialize.field(edit_user_form.confirmed, material_icon='check') }}
- {{ macros.submit_button(edit_user_form.submit) }} + {{ materialize.field(edit_user_form.submit, material_icon='send') }}
diff --git a/app/templates/auth/login.html.j2 b/app/templates/auth/login.html.j2 index 6f57d209..4561a966 100644 --- a/app/templates/auth/login.html.j2 +++ b/app/templates/auth/login.html.j2 @@ -26,41 +26,21 @@
- {{ login_form.hidden_tag() }}
-
- person - {{ login_form.user(class='validate') }} - {{ login_form.user.label }} - {% for error in login_form.user.errors %} - {{ error }} - {% endfor %} -
-
- vpn_key - {{ login_form.password(class='validate') }} - {{ login_form.password.label }} - {% for error in login_form.password.errors %} - {{ error }} - {% endfor %} -
+ {{ login_form.hidden_tag() }} + {{ materialize.field(login_form.user, material_icon='person') }} + {{ materialize.field(login_form.password, material_icon='vpn_key') }}
-
- -
+ {{ materialize.field(login_form.remember_me) }}
- {{ macros.submit_button(login_form.submit) }} + {{ materialize.field(login_form.submit, material_icon='send') }}
diff --git a/app/templates/auth/register.html.j2 b/app/templates/auth/register.html.j2 index 8b38326a..42259f07 100644 --- a/app/templates/auth/register.html.j2 +++ b/app/templates/auth/register.html.j2 @@ -24,43 +24,15 @@
- {{ registration_form.hidden_tag() }}
-
- person - {{ registration_form.username(class='validate', data_length='64') }} - {{ registration_form.username.label }} - {% for error in registration_form.username.errors %} - {{ error }} - {% endfor %} -
-
- vpn_key - {{ registration_form.password(class='validate') }} - {{ registration_form.password.label }} - {% for error in registration_form.password.errors %} - {{ error }} - {% endfor %} -
-
- vpn_key - {{ registration_form.password_confirmation(class='validate') }} - {{ registration_form.password_confirmation.label }} - {% for error in registration_form.password_confirmation.errors %} - {{ error }} - {% endfor %} -
-
- email - {{ registration_form.email(class='validate', type='email') }} - {{ registration_form.email.label }} - {% for error in registration_form.email.errors %} - {{ error }} - {% endfor %} -
+ {{ registration_form.hidden_tag() }} + {{ materialize.field(registration_form.username, data_length='64', material_icon='person') }} + {{ materialize.field(registration_form.password, data_length='128', material_icon='vpn_key') }} + {{ materialize.field(registration_form.password_confirmation, data_length='128', material_icon='vpn_key') }} + {{ materialize.field(registration_form.email, class_='validate', material_icon='email', type='email') }}
- {{ macros.submit_button(registration_form.submit) }} + {{ materialize.field(registration_form.submit, material_icon='send') }}
diff --git a/app/templates/auth/reset_password.html.j2 b/app/templates/auth/reset_password.html.j2 index 5c6740cf..dcf2aa4b 100644 --- a/app/templates/auth/reset_password.html.j2 +++ b/app/templates/auth/reset_password.html.j2 @@ -9,25 +9,13 @@
- {{ reset_password_form.hidden_tag() }}
-
- {{ reset_password_form.password(class='validate') }} - {{ reset_password_form.password.label }} - {% for error in reset_password_form.password.errors %} - {{ error }} - {% endfor %} -
-
- {{ reset_password_form.password_confirmation(class='validate') }} - {{ reset_password_form.password_confirmation.label }} - {% for error in reset_password_form.password_confirmation.errors %} - {{ error }} - {% endfor %} -
+ {{ reset_password_form.hidden_tag() }} + {{ materialize.field(reset_password_form.password, data_length='128') }} + {{ materialize.field(reset_password_form.password_confirmation, data_length='128') }}
- {{ macros.submit_button(reset_password_form.submit) }} + {{ materialize.field(reset_password_form.submit, material_icon='send') }}
diff --git a/app/templates/auth/reset_password_request.html.j2 b/app/templates/auth/reset_password_request.html.j2 index 9ea8d8ea..e25481a1 100644 --- a/app/templates/auth/reset_password_request.html.j2 +++ b/app/templates/auth/reset_password_request.html.j2 @@ -8,18 +8,12 @@
- {{ reset_password_request_form.hidden_tag() }}
-
- {{ reset_password_request_form.email(class='validate', type='email') }} - {{ reset_password_request_form.email.label }} - {% for error in reset_password_request_form.email.errors %} - {{ error }} - {% endfor %} -
+ {{ reset_password_request_form.hidden_tag() }} + {{ materialize.field(reset_password_request_form.email, class_='validate', material_icon='email', type='email') }}
- {{ macros.submit_button(reset_password_request_form.submit) }} + {{ materialize.field(reset_password_request_form.submit, material_icon='send') }}
diff --git a/app/templates/corpora/add_corpus.html.j2 b/app/templates/corpora/add_corpus.html.j2 index 39e33abf..9fb6d841 100644 --- a/app/templates/corpora/add_corpus.html.j2 +++ b/app/templates/corpora/add_corpus.html.j2 @@ -9,33 +9,19 @@
- {{ add_corpus_form.hidden_tag() }}
+ {{ add_corpus_form.hidden_tag() }}
-
- title - {{ add_corpus_form.title(data_length='32') }} - {{ add_corpus_form.title.label }} - {% for error in add_corpus_form.title.errors %} - {{ error }} - {% endfor %} -
+ {{ materialize.field(add_corpus_form.title, data_length='32', material_icon='title') }}
-
- description - {{ add_corpus_form.description(data_length='255') }} - {{ add_corpus_form.description.label }} - {% for error in add_corpus_form.description.errors %} - {{ error }} - {% endfor %} -
+ {{ materialize.field(add_corpus_form.description, data_length='255', material_icon='description') }}
- {{ macros.submit_button(add_corpus_form.submit) }} + {{ materialize.field(add_corpus_form.submit, material_icon='send') }}
diff --git a/app/templates/corpora/add_corpus_file.html.j2 b/app/templates/corpora/add_corpus_file.html.j2 index fb7e3616..cee8457c 100644 --- a/app/templates/corpora/add_corpus_file.html.j2 +++ b/app/templates/corpora/add_corpus_file.html.j2 @@ -9,59 +9,27 @@
- {{ add_corpus_file_form.hidden_tag() }}
Required metadata + {{ add_corpus_file_form.hidden_tag() }}
-
- person - {{ add_corpus_file_form.author(data_length='255') }} - {{ add_corpus_file_form.author.label }} - {% for error in add_corpus_file_form.author.errors %} - {{ error }} - {% endfor %} -
+ {{ materialize.field(add_corpus_file_form.author, data_length='255', material_icon='person') }}
-
- title - {{ add_corpus_file_form.title(data_length='255') }} - {{ add_corpus_file_form.title.label }} - {% for error in add_corpus_file_form.title.errors %} - {{ error }} - {% endfor %} -
+ {{ materialize.field(add_corpus_file_form.title, data_length='255', material_icon='title') }}
-
- access_time - {{ add_corpus_file_form.publishing_year() }} - {{ add_corpus_file_form.publishing_year.label }} - {% for error in add_corpus_file_form.publishing_year.errors %} - {{ error }} - {% endfor %} -
+ {{ materialize.field(add_corpus_file_form.publishing_year, material_icon='access_time') }}
-
-
- {{ add_corpus_file_form.file.label.text }} - {{ add_corpus_file_form.file(accept='.vrt') }} -
-
- -
- {% for error in add_corpus_file_form.file.errors %} - {{ error }} - {% endfor %} -
+ {{ materialize.field(add_corpus_file_form.file, accept='.vrt') }}
- {{ macros.submit_button(add_corpus_file_form.submit) }} + {{ materialize.field(add_corpus_file_form.submit, material_icon='send') }}

@@ -69,8 +37,9 @@
  • addAdd additional metadata
    - {% for field in add_corpus_file_form if not (field.name.endswith(('author', 'csrf_token', 'file', 'publishing_year', 'submit', 'title'))) %} - {{ macros.render_field(field)}} + {% for field in add_corpus_file_form + if field.short_name not in ['author', 'csrf_token', 'file', 'publishing_year', 'submit', 'title'] %} + {{ materialize.field(field, data_length='255', material_icon=field.label.text[0:1]) }} {% endfor %}
  • @@ -95,7 +64,7 @@
    @@ -49,27 +49,13 @@
    -
    - format_list_numbered - {{ display_options_form.results_per_page() }} - {{ display_options_form.results_per_page.label }} -
    + {{ materialize.field(display_options_form.results_per_page, material_icon='format_list_numbered') }}
    -
    - short_text - {{ display_options_form.result_context() }} - {{ display_options_form.result_context.label }} -
    + {{ materialize.field(display_options_form.result_context, material_icon='short_text') }}
    -
    - -
    + {{ materialize.field(display_options_form.expert_mode) }}
    @@ -152,7 +138,6 @@