Change corpus file model to save metadata.

This commit is contained in:
Patrick Jentsch 2019-10-28 15:46:25 +01:00
parent 8a4a7577a0
commit f4c7af02ad
6 changed files with 111 additions and 37 deletions

View File

@ -7,10 +7,17 @@ from wtforms.validators import DataRequired, Length, NumberRange
class CreateCorpusForm(FlaskForm):
description = StringField('Description',
validators=[DataRequired(), Length(1, 64)])
files = MultipleFileField('Files', validators=[DataRequired()])
submit = SubmitField('Create corpus')
title = StringField('Title', validators=[DataRequired(), Length(1, 32)])
class AddCorpusFileForm(FlaskForm):
author = StringField('Author', validators=[DataRequired(), Length(1, 64)])
files = MultipleFileField('Files', validators=[DataRequired()])
publishing_year = IntegerField('Publishing year', validators=[DataRequired()])
submit = SubmitField()
title = StringField('Title', validators=[DataRequired(), Length(1, 64)])
def validate_files(form, field):
for file in field.data:
if not file.filename.lower().endswith('.vrt'):

View File

@ -3,7 +3,7 @@ from flask import (abort, current_app, flash, redirect, request,
render_template, url_for, send_from_directory)
from flask_login import current_user, login_required
from . import main
from .forms import CreateCorpusForm, QueryForm
from .forms import AddCorpusFileForm, CreateCorpusForm, QueryForm
from .. import db
from ..models import Corpus, CorpusFile, Job, JobInput, JobResult
from werkzeug.utils import secure_filename
@ -24,7 +24,12 @@ def corpus(corpus_id):
if not (corpus.creator == current_user
or current_user.is_administrator()):
abort(403)
add_corpus_file_form = AddCorpusFileForm()
if add_corpus_file_form.validate_on_submit():
flash('Corpus file added!')
return redirect(url_for('main.corpus', corpus_id=corpus_id))
return render_template('main/corpora/corpus.html.j2',
add_corpus_file_form=add_corpus_file_form,
corpus=corpus,
title='Corpus')

View File

@ -403,8 +403,11 @@ class CorpusFile(db.Model):
__tablename__ = 'corpus_files'
# Primary key
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(255))
author = db.Column(db.String(64))
dir = db.Column(db.String(255))
filename = db.Column(db.String(255))
publishing_year = db.Column(db.Integer)
title = db.Column(db.String(64))
corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id'))

View File

@ -4,11 +4,6 @@
<div class="col s12 m4">
<h3 id="title">{{ corpus.title }}</h3>
<p id="description">{{ corpus.description }}</p>
<h2>Actions:</h2>
<!-- Confirm deletion of job with modal dialogue
Modal Trigger-->
<a href="#modal-confirm-delete" class="waves-effect waves-light btn red modal-trigger"><i class="material-icons left">delete</i>Delete Corpus</a>
<a href="{{ url_for('main.corpus_analysis', corpus_id=corpus.id) }}" class="waves-effect waves-light btn "><i class="material-icons left">help</i>Analyse</a>
<!-- Modal Strucutre -->
<div id="modal-confirm-delete" class="modal">
<div class="modal-content">
@ -39,7 +34,7 @@
<a href="{{ url_for('main.corpus_analysis', corpus_id=corpus.id) }}" class="waves-effect waves-light btn">
<i class="material-icons left">help</i>Analyse
</a>
<a href="" class="waves-effect waves-light btn">
<a data-target="add-corpus-file-modal" class="waves-effect waves-light btn modal-trigger">
<i class="material-icons left">add</i>Add corpus file
</a>
<a href="#modal-confirm-delete" class="waves-effect waves-light btn red modal-trigger right">
@ -61,33 +56,76 @@
<th>Filename</th>
<th>Author</th>
<th>Title</th>
<th>Publishing year</th>
<th></th>
</tr>
</thead>
<tbody>
{% for file in corpus.files %}
<tr>
<td>Harry Potter.vrt</td>
<td>Stephan Porada</td>
<td>Utopias</td>
<td>{{ file.filename }}</td>
<td>{{ file.author }}</td>
<td>{{ file.title }}</td>
<td>{{ file.publishing_year }}</td>
<td class="right-align">
<a class="waves-effect waves-light btn-small" download="" href="/jobs/1/download?ressource_id=1&amp;ressource_type=input"><i class="material-icons">edit</i></a>
<a class="waves-effect waves-light btn-small" download="" href="/jobs/1/download?ressource_id=1&amp;ressource_type=input"><i class="material-icons">file_download</i></a>
<a class="waves-effect waves-light btn-small red" download="" href="/jobs/1/download?ressource_id=1&amp;ressource_type=input"><i class="material-icons">delete</i></a>
</td>
</tr>
<tr>
<td>Harry Potter.vrt</td>
<td>Stephan Porada</td>
<td>Utopias</td>
<td class="right-align">
<a class="waves-effect waves-light btn-small" download="" href="/jobs/1/download?ressource_id=1&amp;ressource_type=input"><i class="material-icons">edit</i></a>
<a class="waves-effect waves-light btn-small" download="" href="/jobs/1/download?ressource_id=1&amp;ressource_type=input"><i class="material-icons">file_download</i></a>
<a class="waves-effect waves-light btn-small red" download="" href="/jobs/1/download?ressource_id=1&amp;ressource_type=input"><i class="material-icons">delete</i></a>
<a class="waves-effect waves-light btn-small"><i class="material-icons">edit</i></a>
<a class="waves-effect waves-light btn-small"><i class="material-icons">file_download</i></a>
<a class="waves-effect waves-light btn-small red"><i class="material-icons">delete</i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div id="add-corpus-file-modal" class="modal">
<div class="modal-content">
<h4>Add corpus file</h4>
<form method="POST" enctype="multipart/form-data">
{{ add_corpus_file_form.hidden_tag() }}
<div class="row">
<div class="col s12 m8">
<div class="input-field">
<i class="material-icons prefix">person</i>
{{ add_corpus_file_form.author(data_length='64') }}
{{ add_corpus_file_form.author.label }}
{% for error in add_corpus_file_form.author.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
</div>
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">title</i>
{{ add_corpus_file_form.title(data_length='64') }}
{{ add_corpus_file_form.title.label }}
{% for error in add_corpus_file_form.title.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
</div>
<div class="col s12">
<div class="file-field input-field">
<div class="btn">
<span>{{ add_corpus_file_form.files.label.text }}</span>
{{ add_corpus_file_form.files(accept='.vrt') }}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
{% for error in add_corpus_file_form.files.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
</div>
</div>
<div class="modal-footer">
{{ add_corpus_file_form.submit(class='btn') }}
</div>
</form>
</div>
</div>
{% endblock %}

View File

@ -112,18 +112,7 @@
</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>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,</p>
</div>
</div>
<div class="modal-footer">

View File

@ -0,0 +1,32 @@
"""empty message
Revision ID: abf60427ff84
Revises: da9fd175af8c
Create Date: 2019-10-28 14:43:39.691313
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'abf60427ff84'
down_revision = 'da9fd175af8c'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('corpus_files', sa.Column('author', sa.String(length=64), nullable=True))
op.add_column('corpus_files', sa.Column('publishing_year', sa.Integer(), nullable=True))
op.add_column('corpus_files', sa.Column('title', sa.String(length=64), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('corpus_files', 'title')
op.drop_column('corpus_files', 'publishing_year')
op.drop_column('corpus_files', 'author')
# ### end Alembic commands ###