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']) @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(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():
@ -118,18 +121,21 @@ def delete_spacy_model(spacy_nlp_pipeline_model_id):
spacy_nlp_pipeline_model.delete() spacy_nlp_pipeline_model.delete()
db.session.commit() db.session.commit()
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id) snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (spacy_nlp_pipeline_model.user == current_user or current_user.is_administrator()): if not (snpm.user == current_user or current_user.is_administrator()):
abort(403) abort(403)
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(), spacy_nlp_pipeline_model_id)
) )
thread.start() 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']) @bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>/is_public', methods=['PUT'])
@login_required @login_required
@permission_required('CONTRIBUTE') @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): 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):
response = jsonify('The request body must be a boolean') abort(400)
response.status_code = 400 snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
abort(response) if not (snpm.user == current_user or current_user.is_administrator()):
spacy_nlp_pipeline_model = \ abort(403)
SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id) snpm.is_public = is_public
spacy_nlp_pipeline_model.is_public = is_public
db.session.commit() db.session.commit()
response = jsonify( 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"}' f' is now {"public" if is_public else "private"}'
) )
response.status_code = 200 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']) @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_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():
@ -233,18 +241,21 @@ def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
tesseract_ocr_pipeline_model.delete() tesseract_ocr_pipeline_model.delete()
db.session.commit() db.session.commit()
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id) topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
if not (tesseract_ocr_pipeline_model.user == current_user or current_user.is_administrator()): if not (topm.user == current_user or current_user.is_administrator()):
abort(403) abort(403)
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(), tesseract_ocr_pipeline_model_id)
) )
thread.start() 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']) @bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>/is_public', methods=['PUT'])
@login_required @login_required
@permission_required('CONTRIBUTE') @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): 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):
response = jsonify('The request body must be a boolean') abort(400)
response.status_code = 400 topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
abort(response) if not (topm.user == current_user or current_user.is_administrator()):
tesseract_ocr_pipeline_model = \ abort(403)
TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id) topm.is_public = is_public
tesseract_ocr_pipeline_model.is_public = is_public
db.session.commit() db.session.commit()
response = jsonify( 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"}' f' is now {"public" if is_public else "private"}'
) )
response.status_code = 200 response.status_code = 200