Merge branch 'development' of gitlab.ub.uni-bielefeld.de:sfb1288inf/opaque into development

This commit is contained in:
Stephan Porada 2020-04-15 15:11:31 +02:00
commit 4992ac73a8
3 changed files with 17 additions and 8 deletions

View File

@ -1,4 +1,5 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from werkzeug.utils import secure_filename
from wtforms import (BooleanField, FileField, StringField, SubmitField, from wtforms import (BooleanField, FileField, StringField, SubmitField,
ValidationError, IntegerField, SelectField) ValidationError, IntegerField, SelectField)
from wtforms.validators import DataRequired, Length, NumberRange from wtforms.validators import DataRequired, Length, NumberRange
@ -21,10 +22,18 @@ class AddCorpusFileForm(FlaskForm):
submit = SubmitField() submit = SubmitField()
title = StringField('Title', validators=[DataRequired(), Length(1, 255)]) 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'): if not field.data.filename.lower().endswith('.vrt'):
raise ValidationError('File does not have an approved extension: ' raise ValidationError('File does not have an approved extension: '
'.vrt') '.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): class EditCorpusFileForm(FlaskForm):

View File

@ -1,3 +1,4 @@
from app import logger
from flask import (abort, current_app, flash, make_response, redirect, request, from flask import (abort, current_app, flash, make_response, 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
@ -92,15 +93,13 @@ def add_corpus_file(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id) corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.creator == current_user or current_user.is_administrator()): if not (corpus.creator == current_user or current_user.is_administrator()):
abort(403) abort(403)
add_corpus_file_form = AddCorpusFileForm() add_corpus_file_form = AddCorpusFileForm(corpus, prefix='add-job-form')
if add_corpus_file_form.validate_on_submit(): 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 file = add_corpus_file_form.file.data
filename = secure_filename(file.filename) 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 # Save the file
dir = os.path.join(str(corpus.user_id), 'corpora', str(corpus.id)) dir = os.path.join(str(corpus.user_id), 'corpora', str(corpus.id))
file.save(os.path.join(current_app.config['NOPAQUE_STORAGE'], file.save(os.path.join(current_app.config['NOPAQUE_STORAGE'],

View File

@ -133,6 +133,7 @@ nopaque.Forms.init = function() {
window.location.href = JSON.parse(this.responseText).redirect_url; window.location.href = JSON.parse(this.responseText).redirect_url;
} }
if (request.status === 400) { if (request.status === 400) {
console.log(request);
for (let [field, errors] of Object.entries(JSON.parse(this.responseText))) { for (let [field, errors] of Object.entries(JSON.parse(this.responseText))) {
fieldElement = form.querySelector(`input[name="add-job-form-${field}"]`).closest(".input-field"); fieldElement = form.querySelector(`input[name="add-job-form-${field}"]`).closest(".input-field");
for (let error of errors) { for (let error of errors) {