Simplify db lookup for queries by id

This commit is contained in:
Patrick Jentsch 2019-11-15 13:09:12 +01:00
parent 7457a4be94
commit 5fd9711da9
5 changed files with 14 additions and 8 deletions

View File

@ -3,7 +3,7 @@ from app.models import Corpus, CorpusFile
def delete_corpus_(app, corpus_id): def delete_corpus_(app, corpus_id):
with app.app_context(): with app.app_context():
corpus = Corpus.query.filter_by(id=corpus_id).first() corpus = Corpus.query.get(corpus_id)
if corpus is None: if corpus is None:
raise Exception('Corpus {} not found!'.format(corpus_id)) raise Exception('Corpus {} not found!'.format(corpus_id))
corpus.delete() corpus.delete()
@ -11,7 +11,7 @@ def delete_corpus_(app, corpus_id):
def delete_corpus_file_(app, corpus_file_id): def delete_corpus_file_(app, corpus_file_id):
with app.app_context(): with app.app_context():
corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first() corpus_file = CorpusFile.query.get(corpus_file_id)
if corpus_file is None: if corpus_file is None:
raise Exception('Corpus file {} not found!'.format(corpus_file_id)) raise Exception('Corpus file {} not found!'.format(corpus_file_id))
corpus_file.delete() corpus_file.delete()
@ -19,7 +19,7 @@ def delete_corpus_file_(app, corpus_file_id):
def edit_corpus_file_(app, corpus_file_id): def edit_corpus_file_(app, corpus_file_id):
with app.app_context(): with app.app_context():
corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first() corpus_file = CorpusFile.query.get(corpus_file_id)
if corpus_file is None: if corpus_file is None:
raise Exception('Corpus file {} not found!'.format(corpus_file_id)) raise Exception('Corpus file {} not found!'.format(corpus_file_id))
corpus_file.insert_metadata() corpus_file.insert_metadata()

View File

@ -22,7 +22,7 @@ analysis_clients = {}
@socketio.on('init_corpus_analysis') @socketio.on('init_corpus_analysis')
@login_required @login_required
def init_corpus_analysis(corpus_id): def init_corpus_analysis(corpus_id):
corpus = Corpus.query.filter_by(id=corpus_id).first() corpus = Corpus.query.get(corpus_id)
if corpus is None: if corpus is None:
socketio.emit('init_corpus_analysis', '[ERROR 404]: Not Found', socketio.emit('init_corpus_analysis', '[ERROR 404]: Not Found',
room=request.sid) room=request.sid)
@ -85,7 +85,10 @@ def recv_query(message):
def observe_corpus_analysis_connection(app, corpus_id, session_id): def observe_corpus_analysis_connection(app, corpus_id, session_id):
with app.app_context(): with app.app_context():
corpus = Corpus.query.filter_by(id=corpus_id).first() corpus = Corpus.query.get(corpus_id)
if corpus is None:
''' TODO: Handle this '''
return
while corpus.status != 'analysing': while corpus.status != 'analysing':
db.session.refresh(corpus) db.session.refresh(corpus)
socketio.sleep(3) socketio.sleep(3)

View File

@ -72,7 +72,10 @@ def user_ressource_subscription_handler(app, user_id, session_id,
else 'update-jobs'} else 'update-jobs'}
with app.app_context(): with app.app_context():
# Gather current values from database. # Gather current values from database.
user = User.query.filter_by(id=user_id).first() user = User.query.get(user_id)
if user is None:
''' TODO: Handle this '''
return
corpora = {corpus.id: corpus.to_dict() for corpus in user.corpora} corpora = {corpus.id: corpus.to_dict() for corpus in user.corpora}
jobs = {job.id: job.to_dict() for job in user.jobs} jobs = {job.id: job.to_dict() for job in user.jobs}
# Send initial values to the user. # Send initial values to the user.

View File

@ -3,7 +3,7 @@ from app.models import Job
def delete_job_(app, job_id): def delete_job_(app, job_id):
with app.app_context(): with app.app_context():
job = Job.query.filter_by(id=job_id).first() job = Job.query.get(job_id)
if job is None: if job is None:
raise Exception('Job {} not found!'.format(job_id)) raise Exception('Job {} not found!'.format(job_id))
job.delete() job.delete()

View File

@ -3,7 +3,7 @@ from app.models import User
def delete_user_(app, user_id): def delete_user_(app, user_id):
with app.app_context(): with app.app_context():
user = User.query.filter_by(id=user_id).first() user = User.query.get(user_id)
if user is None: if user is None:
raise Exception('User {} not found!'.format(user_id)) raise Exception('User {} not found!'.format(user_id))
user.delete() user.delete()