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): class CreateCorpusForm(FlaskForm):
description = StringField('Description', description = StringField('Description',
validators=[DataRequired(), Length(1, 64)]) validators=[DataRequired(), Length(1, 64)])
files = MultipleFileField('Files', validators=[DataRequired()])
submit = SubmitField('Create corpus') submit = SubmitField('Create corpus')
title = StringField('Title', validators=[DataRequired(), Length(1, 32)]) 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): def validate_files(form, field):
for file in field.data: for file in field.data:
if not file.filename.lower().endswith('.vrt'): 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) render_template, url_for, send_from_directory)
from flask_login import current_user, login_required from flask_login import current_user, login_required
from . import main from . import main
from .forms import CreateCorpusForm, QueryForm from .forms import AddCorpusFileForm, CreateCorpusForm, QueryForm
from .. import db from .. import db
from ..models import Corpus, CorpusFile, Job, JobInput, JobResult from ..models import Corpus, CorpusFile, Job, JobInput, JobResult
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
@ -24,7 +24,12 @@ def corpus(corpus_id):
if not (corpus.creator == current_user if not (corpus.creator == current_user
or current_user.is_administrator()): or current_user.is_administrator()):
abort(403) 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', return render_template('main/corpora/corpus.html.j2',
add_corpus_file_form=add_corpus_file_form,
corpus=corpus, corpus=corpus,
title='Corpus') title='Corpus')

View File

@ -403,8 +403,11 @@ class CorpusFile(db.Model):
__tablename__ = 'corpus_files' __tablename__ = 'corpus_files'
# Primary key # Primary key
id = db.Column(db.Integer, primary_key=True) 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)) 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')) corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id'))

View File

@ -4,11 +4,6 @@
<div class="col s12 m4"> <div class="col s12 m4">
<h3 id="title">{{ corpus.title }}</h3> <h3 id="title">{{ corpus.title }}</h3>
<p id="description">{{ corpus.description }}</p> <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 --> <!-- Modal Strucutre -->
<div id="modal-confirm-delete" class="modal"> <div id="modal-confirm-delete" class="modal">
<div class="modal-content"> <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"> <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 <i class="material-icons left">help</i>Analyse
</a> </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 <i class="material-icons left">add</i>Add corpus file
</a> </a>
<a href="#modal-confirm-delete" class="waves-effect waves-light btn red modal-trigger right"> <a href="#modal-confirm-delete" class="waves-effect waves-light btn red modal-trigger right">
@ -61,33 +56,76 @@
<th>Filename</th> <th>Filename</th>
<th>Author</th> <th>Author</th>
<th>Title</th> <th>Title</th>
<th>Publishing year</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for file in corpus.files %}
<tr> <tr>
<td>Harry Potter.vrt</td> <td>{{ file.filename }}</td>
<td>Stephan Porada</td> <td>{{ file.author }}</td>
<td>Utopias</td> <td>{{ file.title }}</td>
<td>{{ file.publishing_year }}</td>
<td class="right-align"> <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"><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"><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 red"><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>
</td> </td>
</tr> </tr>
{% endfor %}
</tbody> </tbody>
</table> </table>
</div> </div>
</div> </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 %} {% endblock %}

View File

@ -112,18 +112,7 @@
</div> </div>
</div> </div>
<div class="col s12"> <div class="col s12">
<div class="file-field input-field"> <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 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>
</div> </div>
</div> </div>
<div class="modal-footer"> <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 ###