nopaque/app/contributions/spacy_nlp_pipeline_models/json_routes.py

53 lines
1.9 KiB
Python
Raw Normal View History

2023-03-14 10:13:35 +00:00
from flask import abort, current_app, request
2023-04-11 09:46:33 +00:00
from flask_login import current_user
2023-03-14 10:13:35 +00:00
from threading import Thread
from app import db
from app.decorators import content_negotiation, permission_required
from app.models import SpaCyNLPPipelineModel
2024-04-10 11:34:48 +00:00
from . import bp
2023-03-14 10:13:35 +00:00
2023-03-14 10:58:06 +00:00
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['DELETE'])
2023-03-14 10:13:35 +00:00
@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():
snpm = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
snpm.delete()
db.session.commit()
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (snpm.user == current_user or current_user.is_administrator):
2023-03-14 10:13:35 +00:00
abort(403)
thread = Thread(
target=_delete_spacy_model,
args=(current_app._get_current_object(), snpm.id)
)
thread.start()
2023-04-11 11:30:38 +00:00
response_data = {
2023-03-14 10:13:35 +00:00
'message': \
f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
}
2023-04-11 11:30:38 +00:00
return response_data, 202
2023-03-14 10:13:35 +00:00
2023-03-14 10:58:06 +00:00
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>/is_public', methods=['PUT'])
2023-03-14 10:13:35 +00:00
@permission_required('CONTRIBUTE')
@content_negotiation(consumes='application/json', produces='application/json')
def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id):
is_public = request.json
if not isinstance(is_public, bool):
abort(400)
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (snpm.user == current_user or current_user.is_administrator):
2023-03-14 10:13:35 +00:00
abort(403)
snpm.is_public = is_public
db.session.commit()
response_data = {
'message': (
f'SpaCy NLP Pipeline Model "{snpm.title}"'
f' is now {"public" if is_public else "private"}'
)
}
return response_data, 200