diff --git a/app/main/forms.py b/app/main/forms.py index a3ff4a63..e26c34da 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, +from wtforms import (FileField, StringField, SubmitField, ValidationError, IntegerField, SelectField) -from wtforms.validators import DataRequired, Length, NumberRange +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(), (Length(1, 1024))]) hits_per_page = SelectField('Hits per page', diff --git a/app/main/views.py b/app/main/views.py index e2202423..1806fe28 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), 'corpora', 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..df302548 100644 --- a/app/templates/main/corpora/corpus.html.j2 +++ b/app/templates/main/corpora/corpus.html.j2 @@ -69,7 +69,7 @@ {{ file.publishing_year }} edit - file_download + file_download delete @@ -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 %}
diff --git a/app/utils.py b/app/utils.py index e68d0682..2abecee2 100644 --- a/app/utils.py +++ b/app/utils.py @@ -63,5 +63,7 @@ def background_delete_corpus(app, corpus_id): logger.warning('Called by delete_thread.') logger.warning('Corpus id is: {}.'.format(corpus_id)) corpus = Corpus.query.filter_by(id=corpus_id).first() - logger.warning('Corpus object is: {}'.format(corpus)) + for corpus_file in corpus.files: + db.session.delete(corpus_file) + logger.warning('Corpus object is: {}'.format(corpus)) corpus.delete_corpus()