mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 04:12:45 +00:00 
			
		
		
		
	Better exception handling in json-routes
This commit is contained in:
		@@ -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(app, spacy_nlp_pipeline_model_id):
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
 | 
			
		||||
            spacy_nlp_pipeline_model.delete()
 | 
			
		||||
            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)
 | 
			
		||||
    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()):
 | 
			
		||||
        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(
 | 
			
		||||
        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()
 | 
			
		||||
    response = jsonify(
 | 
			
		||||
        f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
 | 
			
		||||
    )
 | 
			
		||||
    resonse_data = {
 | 
			
		||||
        'message': \
 | 
			
		||||
            f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
 | 
			
		||||
    }
 | 
			
		||||
    response = jsonify(resonse_data)
 | 
			
		||||
    response.status_code = 202
 | 
			
		||||
    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):
 | 
			
		||||
    is_public = request.json
 | 
			
		||||
    if not isinstance(is_public, bool):
 | 
			
		||||
        abort(400)
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
 | 
			
		||||
        resonse_data = {
 | 
			
		||||
            '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()):
 | 
			
		||||
        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
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    response = jsonify(
 | 
			
		||||
        f'SpaCy NLP Pipeline Model "{snpm.title}"'
 | 
			
		||||
        f' is now {"public" if is_public else "private"}'
 | 
			
		||||
    )
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': (
 | 
			
		||||
            f'SpaCy NLP Pipeline Model "{snpm.title}"'
 | 
			
		||||
            f' is now {"public" if is_public else "private"}'
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
    response = jsonify(response_data)
 | 
			
		||||
    response.status_code = 200
 | 
			
		||||
    return response
 | 
			
		||||
#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_ocr_pipeline_model(app, tesseract_ocr_pipeline_model_id):
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
            tesseract_ocr_pipeline_model.delete()
 | 
			
		||||
            topm = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
            topm.delete()
 | 
			
		||||
            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()):
 | 
			
		||||
        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(
 | 
			
		||||
        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()
 | 
			
		||||
    response = jsonify(
 | 
			
		||||
        f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
 | 
			
		||||
    )
 | 
			
		||||
    response.status_code = 200
 | 
			
		||||
    resonse_data = {
 | 
			
		||||
        'message': \
 | 
			
		||||
            f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
 | 
			
		||||
    }
 | 
			
		||||
    response = jsonify(resonse_data)
 | 
			
		||||
    response.status_code = 202
 | 
			
		||||
    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):
 | 
			
		||||
    is_public = request.json
 | 
			
		||||
    if not isinstance(is_public, bool):
 | 
			
		||||
        abort(400)
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
        resonse_data = {
 | 
			
		||||
            '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()):
 | 
			
		||||
        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
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    response = jsonify(
 | 
			
		||||
        f'Tesseract OCR Pipeline Model "{topm.title}"'
 | 
			
		||||
        f' is now {"public" if is_public else "private"}'
 | 
			
		||||
    )
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': (
 | 
			
		||||
            f'Tesseract OCR Pipeline Model "{topm.title}"'
 | 
			
		||||
            f' is now {"public" if is_public else "private"}'
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
    response = jsonify(response_data)
 | 
			
		||||
    response.status_code = 200
 | 
			
		||||
    return response
 | 
			
		||||
#endregion json-routes
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user