From e1a8fbc531a6ab0c871776af6d3c88bdcdc5ef92 Mon Sep 17 00:00:00 2001 From: Stephan Porada Date: Thu, 9 Jan 2020 16:04:52 +0100 Subject: [PATCH] Add options to add more detailed metadate to corpus file --- app/corpora/forms.py | 10 ++---- app/corpora/views.py | 17 ++++++--- app/templates/_formhelpers.html.j2 | 27 ++++++++++++++ app/templates/corpora/add_corpus_file.html.j2 | 36 +++++++++++++------ .../corpora/edit_corpus_file.html.j2 | 25 +++++++++++++ nopaque.py | 8 +++-- 6 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 app/templates/_formhelpers.html.j2 diff --git a/app/corpora/forms.py b/app/corpora/forms.py index 9d97c496..e508fe30 100644 --- a/app/corpora/forms.py +++ b/app/corpora/forms.py @@ -13,7 +13,7 @@ class AddCorpusFileForm(FlaskForm): file = FileField('File', validators=[DataRequired()]) institution = StringField('institution', validators=[Length(0, 255)]) journal = StringField('Journal', validators=[Length(0, 255)]) - pages = StringField('pages', validators=[Length(0, 255)]) + pages = StringField('Pages', validators=[Length(0, 255)]) publisher = StringField('Publisher', validators=[Length(0, 255)]) publishing_year = IntegerField('Publishing year', validators=[DataRequired()]) school = StringField('School', validators=[Length(0, 255)]) @@ -33,12 +33,8 @@ class AddCorpusForm(FlaskForm): title = StringField('Title', validators=[DataRequired(), Length(1, 32)]) -class EditCorpusFileForm(FlaskForm): - author = StringField('Author', validators=[DataRequired(), Length(1, 64)]) - publishing_year = IntegerField('Publishing year', - validators=[DataRequired()]) - submit = SubmitField() - title = StringField('Title', validators=[DataRequired(), Length(1, 64)]) +class EditCorpusFileForm(AddCorpusFileForm): + pass class QueryForm(FlaskForm): diff --git a/app/corpora/views.py b/app/corpora/views.py index f2d6f83a..a6670d62 100644 --- a/app/corpora/views.py +++ b/app/corpora/views.py @@ -1,4 +1,4 @@ -from app import db +from app import db, logger from app.models import Corpus, CorpusFile from flask import (abort, current_app, flash, redirect, request, render_template, url_for, send_from_directory) @@ -97,10 +97,16 @@ def add_corpus_file(corpus_id): dir = os.path.join(str(corpus.user_id), 'corpora', str(corpus.id)) file.save(os.path.join(current_app.config['NOPAQUE_STORAGE'], dir, filename)) - corpus_file = CorpusFile(author=add_corpus_file_form.author.data, - corpus=corpus, dir=dir, filename=filename, - publishing_year=add_corpus_file_form.publishing_year.data, - title=add_corpus_file_form.title.data) + 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) db.session.add(corpus_file) db.session.commit() thread = Thread(target=edit_corpus_file_, @@ -160,6 +166,7 @@ def edit_corpus_file(corpus_id, corpus_file_id): corpus_file.author = edit_corpus_file_form.author.data corpus_file.publishing_year = edit_corpus_file_form.publishing_year.data corpus_file.title = edit_corpus_file_form.title.data + corpus_file.pages = edit_corpus_file_form.pages.data db.session.commit() thread = Thread(target=edit_corpus_file_, args=(current_app._get_current_object(), diff --git a/app/templates/_formhelpers.html.j2 b/app/templates/_formhelpers.html.j2 new file mode 100644 index 00000000..37d4f381 --- /dev/null +++ b/app/templates/_formhelpers.html.j2 @@ -0,0 +1,27 @@ +{% macro render_field(field) %} +
+ {{ field.name[0:1]|upper }} + {{ field.label }} + {{ field(**kwargs)|safe }} +
+ {% if field.errors %} + {% for error in field.errors %} + {{ error }} + {% endfor %} + {% endif %} +{% endmacro %} + +{% macro render_field_with_value(field, corpus_file) %} + {% set field_name = field.name %} + {{ field_name }} +
+ {{ field.name[0:1]|upper }} + {{ field.label }} + {{ field(value=corpus_file[field.name])| safe }} +
+ {% if field.errors %} + {% for error in field.errors %} + {{ error }} + {% endfor %} + {% endif %} +{% endmacro %} diff --git a/app/templates/corpora/add_corpus_file.html.j2 b/app/templates/corpora/add_corpus_file.html.j2 index abd0e691..6c6c11a4 100644 --- a/app/templates/corpora/add_corpus_file.html.j2 +++ b/app/templates/corpora/add_corpus_file.html.j2 @@ -16,7 +16,7 @@
person - {{ add_corpus_file_form.author(data_length='64') }} + {{ add_corpus_file_form.author(data_length='255') }} {{ add_corpus_file_form.author.label }} {% for error in add_corpus_file_form.author.errors %} {{ error }} @@ -26,7 +26,7 @@
title - {{ add_corpus_file_form.title(data_length='64') }} + {{ add_corpus_file_form.title(data_length='255') }} {{ add_corpus_file_form.title.label }} {% for error in add_corpus_file_form.title.errors %} {{ error }} @@ -71,14 +71,30 @@
-
- person - {{ add_corpus_file_form.author(data_length='64') }} - {{ add_corpus_file_form.author.label }} - {% for error in add_corpus_file_form.author.errors %} - {{ error }} - {% endfor %} -
+ {% from "_formhelpers.html.j2" import render_field %} + {% 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") %} + {{ render_field(field)}} + {% endfor %} +
+
+
+
+ + +
+
    +
  • +
    addAdd metadata with BibTex
    +
    + +
    +
    +
    diff --git a/app/templates/corpora/edit_corpus_file.html.j2 b/app/templates/corpora/edit_corpus_file.html.j2 index c23ec548..b098ab54 100644 --- a/app/templates/corpora/edit_corpus_file.html.j2 +++ b/app/templates/corpora/edit_corpus_file.html.j2 @@ -47,6 +47,31 @@
    +
    +
      +
    • +
      editEdit additional metadata
      +
      + +
      +
      + {% from "_formhelpers.html.j2" import render_field_with_value %} + {% for field in edit_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") %} +
      + {{ render_field_with_value(field, corpus_file)}} +
      + {% endfor %} +
      +
      +
      +
      +
    • +
diff --git a/nopaque.py b/nopaque.py index 63393b27..f521ca90 100644 --- a/nopaque.py +++ b/nopaque.py @@ -1,7 +1,7 @@ import eventlet eventlet.monkey_patch() from app import create_app, db, socketio -from app.models import Corpus, Job, Role, User +from app.models import * from flask_migrate import Migrate import os @@ -12,7 +12,11 @@ migrate = Migrate(app, db) @app.shell_context_processor def make_shell_context(): - return {'db': db, 'Corpus': Corpus, 'Job': Job, 'User': User} + return {'db': db, + 'Corpus': Corpus, + 'Job': Job, + 'User': User, + 'CorpusFile': CorpusFile} @app.cli.command()