mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-24 10:34:17 +00:00
Add error handling
This commit is contained in:
parent
74d9147735
commit
755f5f3137
@ -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,32 +18,38 @@ 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)
|
||||
db.session.add(corpus)
|
||||
db.session.commit()
|
||||
dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
|
||||
str(corpus.user_id), 'corpora', str(corpus.id))
|
||||
try:
|
||||
os.makedirs(dir)
|
||||
except OSError:
|
||||
flash('OSError!')
|
||||
db.session.remove(corpus)
|
||||
corpus = Corpus(creator=current_user,
|
||||
description=add_corpus_form.description.data,
|
||||
title=add_corpus_form.title.data)
|
||||
db.session.add(corpus)
|
||||
db.session.commit()
|
||||
flash('Corpus created!')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus.id))
|
||||
dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
|
||||
str(corpus.user_id), 'corpora', str(corpus.id))
|
||||
try:
|
||||
os.makedirs(dir)
|
||||
except OSError:
|
||||
flash('OSError!')
|
||||
db.session.remove(corpus)
|
||||
db.session.commit()
|
||||
flash('Corpus created!')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus.id))
|
||||
|
||||
|
||||
@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,27 +103,28 @@ 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)
|
||||
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 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))
|
||||
author = add_corpus_file_form.author.data
|
||||
publishing_year = add_corpus_file_form.publishing_year.data
|
||||
title = add_corpus_file_form.title.data
|
||||
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!')
|
||||
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:
|
||||
if filename == corpus_file.filename:
|
||||
flash('File already registered to this corpus.')
|
||||
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))
|
||||
author = add_corpus_file_form.author.data
|
||||
publishing_year = add_corpus_file_form.publishing_year.data
|
||||
title = add_corpus_file_form.title.data
|
||||
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!')
|
||||
else:
|
||||
session['add_corpus_file_form_data'] = request.form
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user