Add feature to restore script content to a specific version on history page

This commit is contained in:
Peter Stockings
2025-11-25 15:43:12 +11:00
parent 17518c3fcc
commit c0970835ab
5 changed files with 131 additions and 6 deletions

View File

@@ -465,3 +465,47 @@ def history(function_id):
return render_block(environment, 'dashboard/timer_functions/history.html', 'page', **args)
return render_template('dashboard/timer_functions/history.html', **args)
@timer.route('/restore/<int:function_id>', methods=['POST'])
@login_required
def restore(function_id):
try:
user_id = current_user.id
version_number = request.json.get('version_number')
if not version_number:
return jsonify({"status": "error", "message": "Version number is required"}), 400
# Verify ownership and existence of the function
timer_function = db.execute(
"SELECT id FROM timer_functions WHERE id = %s AND user_id = %s",
[function_id, user_id],
one=True
)
if not timer_function:
return jsonify({"status": "error", "message": "Timer function not found"}), 404
# Fetch the content of the selected version
version_data = db.execute(
"SELECT script FROM timer_function_versions WHERE timer_function_id = %s AND version_number = %s",
[function_id, version_number],
one=True
)
if not version_data:
return jsonify({"status": "error", "message": "Version not found"}), 404
# Update the function with the old script content
# This will trigger the database function to create a new version entry
db.execute(
"UPDATE timer_functions SET code = %s WHERE id = %s",
[version_data["script"], function_id],
commit=True
)
return jsonify({"status": "success", "message": f"Restored to version {version_number}"})
except Exception as e:
print(e)
return jsonify({"status": "error", "message": str(e)}), 500