Add error handling

This commit is contained in:
Patrick Jentsch 2019-10-31 11:34:45 +01:00
parent 74d9147735
commit 755f5f3137

View File

@ -1,12 +1,13 @@
from flask import (abort, current_app, flash, redirect, request,
render_template, url_for, send_from_directory)
render_template, session, url_for, send_from_directory)
from flask_login import current_user, login_required
from werkzeug import MultiDict
from werkzeug.utils import secure_filename
from . import corpora
from .forms import (AddCorpusFileForm, AddCorpusForm, QueryDownloadForm,
QueryForm)
from .. import db
from ..models import Corpus, CorpusFile
from werkzeug.utils import secure_filename
import os
import threading
import logging
@ -17,7 +18,6 @@ import logging
def add_corpus():
add_corpus_form = AddCorpusForm()
if not add_corpus_form.validate_on_submit():
abort(400)
corpus = Corpus(creator=current_user,
description=add_corpus_form.description.data,
title=add_corpus_form.title.data)
@ -38,11 +38,18 @@ def add_corpus():
@corpora.route('/<int:corpus_id>')
@login_required
def corpus(corpus_id):
add_corpus_file_form_data = session.pop('add_corpus_file_form_data',
default=None)
add_corpus_file_form = AddCorpusFileForm(
MultiDict(add_corpus_file_form_data)
)
if add_corpus_file_form_data is not None:
add_corpus_file_form.validate()
corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.creator == current_user or current_user.is_administrator()):
abort(403)
return render_template('corpora/corpus.html.j2',
add_corpus_file_form=AddCorpusFileForm(),
add_corpus_file_form=add_corpus_file_form,
corpus=corpus, title='Corpus')
@ -96,8 +103,7 @@ def add_corpus_file(corpus_id):
if not (corpus.creator == current_user or current_user.is_administrator()):
abort(403)
add_corpus_file_form = AddCorpusFileForm()
if not add_corpus_file_form.validate_on_submit():
abort(400)
if add_corpus_file_form.validate_on_submit():
file = add_corpus_file_form.file.data
filename = secure_filename(file.filename)
for corpus_file in corpus.files:
@ -106,8 +112,8 @@ def add_corpus_file(corpus_id):
return redirect(url_for('corpora.corpus', 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['OPAQUE_STORAGE_DIRECTORY'], dir,
filename))
file.save(os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
dir, filename))
author = add_corpus_file_form.author.data
publishing_year = add_corpus_file_form.publishing_year.data
title = add_corpus_file_form.title.data
@ -117,6 +123,8 @@ def add_corpus_file(corpus_id):
db.session.add(corpus_file)
db.session.commit()
flash('Corpus file added!')
else:
session['add_corpus_file_form_data'] = request.form
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))