Standardize code for reference

This commit is contained in:
Patrick Jentsch 2023-03-07 16:34:49 +01:00
parent 9272150212
commit 09fdad2162

View File

@ -110,7 +110,10 @@ def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
)
#region json-routes
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['DELETE'])
@login_required
@content_negotiation(produces='application/json')
def delete_spacy_model(spacy_nlp_pipeline_model_id):
def _delete_spacy_model(app, spacy_nlp_pipeline_model_id):
with app.app_context():
@ -118,18 +121,21 @@ def delete_spacy_model(spacy_nlp_pipeline_model_id):
spacy_nlp_pipeline_model.delete()
db.session.commit()
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (spacy_nlp_pipeline_model.user == current_user or current_user.is_administrator()):
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (snpm.user == current_user or current_user.is_administrator()):
abort(403)
thread = Thread(
target=_delete_spacy_model,
args=(current_app._get_current_object(), spacy_nlp_pipeline_model_id)
)
thread.start()
return {}, 202
response = jsonify(
f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
)
response.status_code = 202
return response
#region json-routes
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>/is_public', methods=['PUT'])
@login_required
@permission_required('CONTRIBUTE')
@ -137,15 +143,14 @@ def delete_spacy_model(spacy_nlp_pipeline_model_id):
def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id):
is_public = request.json
if not isinstance(is_public, bool):
response = jsonify('The request body must be a boolean')
response.status_code = 400
abort(response)
spacy_nlp_pipeline_model = \
SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
spacy_nlp_pipeline_model.is_public = is_public
abort(400)
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (snpm.user == current_user or current_user.is_administrator()):
abort(403)
snpm.is_public = is_public
db.session.commit()
response = jsonify(
f'SpaCy NLP Pipeline Model "{spacy_nlp_pipeline_model.title}"'
f'SpaCy NLP Pipeline Model "{snpm.title}"'
f' is now {"public" if is_public else "private"}'
)
response.status_code = 200
@ -225,7 +230,10 @@ def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id):
)
#region json-routes
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['DELETE'])
@login_required
@content_negotiation(produces='application/json')
def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
def _delete_tesseract_ocr_pipeline_model(app, tesseract_ocr_pipeline_model_id):
with app.app_context():
@ -233,18 +241,21 @@ def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
tesseract_ocr_pipeline_model.delete()
db.session.commit()
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
if not (tesseract_ocr_pipeline_model.user == current_user or current_user.is_administrator()):
topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
if not (topm.user == current_user or current_user.is_administrator()):
abort(403)
thread = Thread(
target=_delete_tesseract_ocr_pipeline_model,
args=(current_app._get_current_object(), tesseract_ocr_pipeline_model_id)
)
thread.start()
return {}, 202
response = jsonify(
f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
)
response.status_code = 200
return response
#region json-routes
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>/is_public', methods=['PUT'])
@login_required
@permission_required('CONTRIBUTE')
@ -252,15 +263,14 @@ def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
def update_tesseract_ocr_pipeline_model_is_public(tesseract_ocr_pipeline_model_id):
is_public = request.json
if not isinstance(is_public, bool):
response = jsonify('The request body must be a boolean')
response.status_code = 400
abort(response)
tesseract_ocr_pipeline_model = \
TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
tesseract_ocr_pipeline_model.is_public = is_public
abort(400)
topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
if not (topm.user == current_user or current_user.is_administrator()):
abort(403)
topm.is_public = is_public
db.session.commit()
response = jsonify(
f'Tesseract OCR Pipeline Model "{tesseract_ocr_pipeline_model.title}"'
f'Tesseract OCR Pipeline Model "{topm.title}"'
f' is now {"public" if is_public else "private"}'
)
response.status_code = 200