in project/paperbench/paperbench/gui/app.py [0:0]
def move_node():
"""Moves a node up or down relative to its siblings."""
with lock:
data = sanitize_request_data(request.json)
node_id = data.get("node_id")
direction = data.get("direction") # "up" or "down"
try:
with open(app.config["PATH_TO_PAPER"] / app.config["RUBRIC_FILE_NAME"], "r") as f:
rubric = json.load(f)
root = app.config["NODE_TYPE"].from_dict(rubric)
try:
parent = root.get_parent(node_id)
except ValueError as e:
return jsonify({"status": "error", "message": "Cannot move the root node."})
current_idx = next(i for i, task in enumerate(parent.sub_tasks) if task.id == node_id)
if direction == "up" and current_idx > 0:
new_idx = current_idx - 1
elif direction == "down" and current_idx < len(parent.sub_tasks) - 1:
new_idx = current_idx + 1
else:
return jsonify(
{"status": "error", "message": "Cannot move node further in that direction."}
)
# Swap nodes
new_sub_tasks = list(parent.sub_tasks)
new_sub_tasks[current_idx], new_sub_tasks[new_idx] = (
new_sub_tasks[new_idx],
new_sub_tasks[current_idx],
)
new_parent = parent.set_sub_tasks(new_sub_tasks)
new_root = root.replace(parent.id, new_parent)
with open(app.config["PATH_TO_PAPER"] / app.config["RUBRIC_FILE_NAME"], "w") as f:
json.dump(new_root.to_dict(), f, indent=4)
return jsonify({"status": "success"})
except Exception as e:
return jsonify({"status": "error", "message": f"Error moving node {node_id}: {str(e)}"})