From f264b27ae5d224cfdfabfb1e8f1c427ea75e289d Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 15 Apr 2020 14:57:17 +0200 Subject: [PATCH] Change validation process (currently broken) --- app/corpora/forms.py | 11 ++++++++++- app/corpora/views.py | 13 ++++++------- app/static/js/nopaque.js | 1 + 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/corpora/forms.py b/app/corpora/forms.py index 82745e76..fab0e4d2 100644 --- a/app/corpora/forms.py +++ b/app/corpora/forms.py @@ -1,4 +1,5 @@ from flask_wtf import FlaskForm +from werkzeug.utils import secure_filename from wtforms import (BooleanField, FileField, StringField, SubmitField, ValidationError, IntegerField, SelectField) from wtforms.validators import DataRequired, Length @@ -21,10 +22,18 @@ class AddCorpusFileForm(FlaskForm): submit = SubmitField() title = StringField('Title', validators=[DataRequired(), Length(1, 255)]) - def validate_file(form, field): + def __init__(self, corpus, *args, **kwargs): + super(AddCorpusFileForm, self).__init__(*args, **kwargs) + self.corpus = corpus + + def validate_file(self, field): if not field.data.filename.lower().endswith('.vrt'): raise ValidationError('File does not have an approved extension: ' '.vrt') + filename = secure_filename(field.data.filename) + for corpus_file in self.corpus.files: + if filename == corpus_file.filename: + raise ValidationError('File already registered to this corpus.') class EditCorpusFileForm(FlaskForm): diff --git a/app/corpora/views.py b/app/corpora/views.py index dc4d4ade..a0654ff6 100644 --- a/app/corpora/views.py +++ b/app/corpora/views.py @@ -1,3 +1,4 @@ +from app import logger from flask import (abort, current_app, flash, make_response, redirect, request, render_template, url_for, send_from_directory) from flask_login import current_user, login_required @@ -92,15 +93,13 @@ def add_corpus_file(corpus_id): corpus = Corpus.query.get_or_404(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(): + add_corpus_file_form = AddCorpusFileForm(corpus, prefix='add-job-form') + if add_corpus_file_form.is_submitted(): + if not add_corpus_file_form.validate(): + logger.warning(add_corpus_file_form.errors) + return make_response(add_corpus_file_form.errors, 400) file = add_corpus_file_form.file.data filename = secure_filename(file.filename) - for corpus_file in corpus.files: - if filename == corpus_file.filename: - flash('File already registered to this corpus.') - return make_response({'redirect_url': url_for('corpora.corpus', corpus_id=corpus.id)}, 500) - # return redirect(url_for('corpora.add_corpus_file', corpus_id=corpus_id)) # Save the file dir = os.path.join(str(corpus.user_id), 'corpora', str(corpus.id)) file.save(os.path.join(current_app.config['NOPAQUE_STORAGE'], diff --git a/app/static/js/nopaque.js b/app/static/js/nopaque.js index ee2f0469..d39d80ff 100644 --- a/app/static/js/nopaque.js +++ b/app/static/js/nopaque.js @@ -133,6 +133,7 @@ nopaque.Forms.init = function() { window.location.href = JSON.parse(this.responseText).redirect_url; } if (request.status === 400) { + console.log(request); for (let [field, errors] of Object.entries(JSON.parse(this.responseText))) { fieldElement = form.querySelector(`input[name="add-job-form-${field}"]`).closest(".input-field"); for (let error of errors) {