now validating the input for code refinement

This commit is contained in:
Karma Riuk
2025-05-14 16:16:05 +02:00
parent 32949633cc
commit a11be64a6d

View File

@ -25,6 +25,25 @@ def validate_json_format_for_comment_gen(data: str) -> dict[str, str]:
raise InvalidJsonFormatError() raise InvalidJsonFormatError()
def validate_json_format_for_code_refinement(data: str) -> dict[str, dict[str, str]]:
try:
obj = json.loads(data)
if not isinstance(obj, dict):
raise InvalidJsonFormatError("Submitted json doesn't contain an object")
for _, submission in obj.items():
if not all(isinstance(content, str) for content in submission.values()):
raise InvalidJsonFormatError(
"Submitted json object must be str -> {str -> str}. Namely id -> {filename -> content of file}"
)
return obj
except InvalidJsonFormatError as e:
raise e
except Exception:
raise InvalidJsonFormatError()
@router.route('/submit/comments', methods=['POST']) @router.route('/submit/comments', methods=['POST'])
def submit_comments(): def submit_comments():
file = request.files.get('file') file = request.files.get('file')
@ -51,5 +70,18 @@ def submit_comments():
@router.route('/submit/refinement', methods=['POST']) @router.route('/submit/refinement', methods=['POST'])
def submit_refinement(): def submit_refinement():
file = request.files.get('file') file = request.files.get('file')
# similar to above if file is None or file.filename is None or file.filename.split('.')[-1] not in ALLOWED_EXT:
return jsonify({'error': 'Only JSON files are allowed'}), 400
data = file.read().decode()
try:
validated = validate_json_format_for_code_refinement(data)
except InvalidJsonFormatError as e:
return jsonify({'error': 'Invalid JSON format', 'message': str(e)}), 400
socketio = current_app.extensions['socketio']
sid = request.headers.get('X-Socket-Id')
if sid:
socketio.emit('successful-upload', room=sid)
socketio.emit('started-processing', room=sid)
return jsonify({'message': 'Answer submitted successfully'}) return jsonify({'message': 'Answer submitted successfully'})