mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-25 19:04:18 +00:00
Better exception handling in json-routes
This commit is contained in:
parent
09fdad2162
commit
0e7e5933cc
@ -117,21 +117,37 @@ def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
|
|||||||
def delete_spacy_model(spacy_nlp_pipeline_model_id):
|
def delete_spacy_model(spacy_nlp_pipeline_model_id):
|
||||||
def _delete_spacy_model(app, spacy_nlp_pipeline_model_id):
|
def _delete_spacy_model(app, spacy_nlp_pipeline_model_id):
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
|
snpm = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
|
||||||
spacy_nlp_pipeline_model.delete()
|
snpm.delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
snpm = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
|
||||||
|
if snpm is None:
|
||||||
|
resonse_data = {
|
||||||
|
'message': f'"{snpm.title}" not found',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 404
|
||||||
|
return response
|
||||||
if not (snpm.user == current_user or current_user.is_administrator()):
|
if not (snpm.user == current_user or current_user.is_administrator()):
|
||||||
abort(403)
|
resonse_data = {
|
||||||
|
'message': f'You are not allowed to delete "{snpm.title}"',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 403
|
||||||
|
return response
|
||||||
thread = Thread(
|
thread = Thread(
|
||||||
target=_delete_spacy_model,
|
target=_delete_spacy_model,
|
||||||
args=(current_app._get_current_object(), spacy_nlp_pipeline_model_id)
|
args=(current_app._get_current_object(), snpm.id)
|
||||||
)
|
)
|
||||||
thread.start()
|
thread.start()
|
||||||
response = jsonify(
|
resonse_data = {
|
||||||
f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
|
'message': \
|
||||||
)
|
f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
response.status_code = 202
|
response.status_code = 202
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -143,16 +159,39 @@ def delete_spacy_model(spacy_nlp_pipeline_model_id):
|
|||||||
def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id):
|
def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id):
|
||||||
is_public = request.json
|
is_public = request.json
|
||||||
if not isinstance(is_public, bool):
|
if not isinstance(is_public, bool):
|
||||||
abort(400)
|
resonse_data = {
|
||||||
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
'message': 'Request body must be a boolean',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 400
|
||||||
|
return response
|
||||||
|
snpm = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
|
||||||
|
if snpm is None:
|
||||||
|
resonse_data = {
|
||||||
|
'message': f'"{snpm.title}" not found',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 404
|
||||||
|
return response
|
||||||
if not (snpm.user == current_user or current_user.is_administrator()):
|
if not (snpm.user == current_user or current_user.is_administrator()):
|
||||||
abort(403)
|
resonse_data = {
|
||||||
|
'message': f'You are not allowed to delete "{snpm.title}"',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 403
|
||||||
|
return response
|
||||||
snpm.is_public = is_public
|
snpm.is_public = is_public
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
response = jsonify(
|
response_data = {
|
||||||
f'SpaCy NLP Pipeline Model "{snpm.title}"'
|
'message': (
|
||||||
f' is now {"public" if is_public else "private"}'
|
f'SpaCy NLP Pipeline Model "{snpm.title}"'
|
||||||
)
|
f' is now {"public" if is_public else "private"}'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
response = jsonify(response_data)
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
return response
|
return response
|
||||||
#endregion json-routes
|
#endregion json-routes
|
||||||
@ -237,22 +276,38 @@ def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id):
|
|||||||
def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
|
def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
|
||||||
def _delete_tesseract_ocr_pipeline_model(app, tesseract_ocr_pipeline_model_id):
|
def _delete_tesseract_ocr_pipeline_model(app, tesseract_ocr_pipeline_model_id):
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
|
topm = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
|
||||||
tesseract_ocr_pipeline_model.delete()
|
topm.delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
|
topm = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
|
||||||
|
if topm is None:
|
||||||
|
resonse_data = {
|
||||||
|
'message': f'"{topm.title}" not found',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 404
|
||||||
|
return response
|
||||||
if not (topm.user == current_user or current_user.is_administrator()):
|
if not (topm.user == current_user or current_user.is_administrator()):
|
||||||
abort(403)
|
resonse_data = {
|
||||||
|
'message': f'You are not allowed to delete "{topm.title}"',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 403
|
||||||
|
return response
|
||||||
thread = Thread(
|
thread = Thread(
|
||||||
target=_delete_tesseract_ocr_pipeline_model,
|
target=_delete_tesseract_ocr_pipeline_model,
|
||||||
args=(current_app._get_current_object(), tesseract_ocr_pipeline_model_id)
|
args=(current_app._get_current_object(), topm.id)
|
||||||
)
|
)
|
||||||
thread.start()
|
thread.start()
|
||||||
response = jsonify(
|
resonse_data = {
|
||||||
f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
|
'message': \
|
||||||
)
|
f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
|
||||||
response.status_code = 200
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 202
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
@ -263,16 +318,39 @@ def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
|
|||||||
def update_tesseract_ocr_pipeline_model_is_public(tesseract_ocr_pipeline_model_id):
|
def update_tesseract_ocr_pipeline_model_is_public(tesseract_ocr_pipeline_model_id):
|
||||||
is_public = request.json
|
is_public = request.json
|
||||||
if not isinstance(is_public, bool):
|
if not isinstance(is_public, bool):
|
||||||
abort(400)
|
resonse_data = {
|
||||||
topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
|
'message': 'Request body must be a boolean',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 400
|
||||||
|
return response
|
||||||
|
topm = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
|
||||||
|
if topm is None:
|
||||||
|
resonse_data = {
|
||||||
|
'message': f'"{topm.title}" not found',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 404
|
||||||
|
return response
|
||||||
if not (topm.user == current_user or current_user.is_administrator()):
|
if not (topm.user == current_user or current_user.is_administrator()):
|
||||||
abort(403)
|
resonse_data = {
|
||||||
|
'message': f'You are not allowed to delete "{topm.title}"',
|
||||||
|
'category': 'error'
|
||||||
|
}
|
||||||
|
response = jsonify(resonse_data)
|
||||||
|
response.status_code = 403
|
||||||
|
return response
|
||||||
topm.is_public = is_public
|
topm.is_public = is_public
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
response = jsonify(
|
response_data = {
|
||||||
f'Tesseract OCR Pipeline Model "{topm.title}"'
|
'message': (
|
||||||
f' is now {"public" if is_public else "private"}'
|
f'Tesseract OCR Pipeline Model "{topm.title}"'
|
||||||
)
|
f' is now {"public" if is_public else "private"}'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
response = jsonify(response_data)
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
return response
|
return response
|
||||||
#endregion json-routes
|
#endregion json-routes
|
||||||
|
Loading…
Reference in New Issue
Block a user