Add corpus file upload

This commit is contained in:
Patrick Jentsch 2019-10-29 13:47:25 +01:00
parent f4c7af02ad
commit 4930709c9d
3 changed files with 55 additions and 22 deletions

View File

@ -1,7 +1,21 @@
from flask_wtf import FlaskForm
from wtforms import (MultipleFileField, StringField, SubmitField,
ValidationError, IntegerField)
from wtforms.validators import DataRequired, Length, NumberRange
from wtforms import (FileField, IntegerField, StringField, SubmitField,
ValidationError)
from wtforms.validators import DataRequired, Length
class AddCorpusFileForm(FlaskForm):
author = StringField('Author', validators=[DataRequired(), Length(1, 64)])
file = FileField('File', validators=[DataRequired()])
publishing_year = IntegerField('Publishing year',
validators=[DataRequired()])
submit = SubmitField()
title = StringField('Title', validators=[DataRequired(), Length(1, 64)])
def validate_file(form, field):
if not field.data.filename.lower().endswith('.vrt'):
raise ValidationError('File does not have an approved extension: '
'.vrt')
class CreateCorpusForm(FlaskForm):
@ -11,20 +25,6 @@ class CreateCorpusForm(FlaskForm):
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'):
raise ValidationError('File does not have an approved '
'extension: .vrt')
class QueryForm(FlaskForm):
query = StringField('CQP Query', validators=[DataRequired()])
# hits_per_page = IntegerField('Hits per page',

View File

@ -17,7 +17,7 @@ def index():
return render_template('main/index.html.j2', title='Opaque')
@main.route('/corpora/<int:corpus_id>')
@main.route('/corpora/<int:corpus_id>', methods=['GET', 'POST'])
@login_required
def corpus(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id)
@ -26,6 +26,29 @@ def corpus(corpus_id):
abort(403)
add_corpus_file_form = AddCorpusFileForm()
if add_corpus_file_form.validate_on_submit():
filename = secure_filename(add_corpus_file_form.file.data.filename)
for corpus_file in corpus.files:
if filename == corpus_file.filename:
flash('File already registered to this corpus.')
return redirect(url_for('main.corpus', corpus_id=corpus_id))
# Gather information to create new corpus file database entry
author = add_corpus_file_form.author.data
dir = os.path.join(str(corpus.user_id), 'jobs', str(corpus.id))
file = add_corpus_file_form.file.data
publishing_year = add_corpus_file_form.publishing_year.data
title = add_corpus_file_form.title.data
# Save the file
file_dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
dir)
file.save(os.path.join(file_dir, filename))
corpus_file = CorpusFile(author=author,
corpus=corpus,
dir=dir,
filename=filename,
publishing_year=publishing_year,
title=title)
db.session.add(corpus_file)
db.session.commit()
flash('Corpus file added!')
return redirect(url_for('main.corpus', corpus_id=corpus_id))
return render_template('main/corpora/corpus.html.j2',

View File

@ -87,7 +87,7 @@
<form method="POST" enctype="multipart/form-data">
{{ add_corpus_file_form.hidden_tag() }}
<div class="row">
<div class="col s12 m8">
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">person</i>
{{ add_corpus_file_form.author(data_length='64') }}
@ -107,16 +107,26 @@
{% endfor %}
</div>
</div>
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">access_time</i>
{{ add_corpus_file_form.publishing_year() }}
{{ add_corpus_file_form.publishing_year.label }}
{% for error in add_corpus_file_form.publishing_year.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') }}
<span>{{ add_corpus_file_form.file.label.text }}</span>
{{ add_corpus_file_form.file(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 %}
{% for error in add_corpus_file_form.file.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>