diff --git a/app/main/forms.py b/app/main/forms.py index 20826e48..43b63275 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -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', diff --git a/app/main/views.py b/app/main/views.py index bff10de1..b5b71985 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -17,7 +17,7 @@ def index(): return render_template('main/index.html.j2', title='Opaque') -@main.route('/corpora/') +@main.route('/corpora/', 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', diff --git a/app/templates/main/corpora/corpus.html.j2 b/app/templates/main/corpora/corpus.html.j2 index 5896af60..6a55167d 100644 --- a/app/templates/main/corpora/corpus.html.j2 +++ b/app/templates/main/corpora/corpus.html.j2 @@ -87,7 +87,7 @@
{{ add_corpus_file_form.hidden_tag() }}
-
+
person {{ add_corpus_file_form.author(data_length='64') }} @@ -107,16 +107,26 @@ {% endfor %}
+
+
+ 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 %} +
+
- {{ add_corpus_file_form.files.label.text }} - {{ add_corpus_file_form.files(accept='.vrt') }} + {{ add_corpus_file_form.file.label.text }} + {{ add_corpus_file_form.file(accept='.vrt') }}
- {% for error in add_corpus_file_form.files.errors %} + {% for error in add_corpus_file_form.file.errors %} {{ error }} {% endfor %}