Add feature to restore script content to a specific version on history page
This commit is contained in:
@@ -393,3 +393,47 @@ def editor(function_id):
|
||||
)
|
||||
|
||||
return render_template("dashboard/http_functions/editor.html", **editor_data)
|
||||
|
||||
|
||||
@http.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
|
||||
http_function = db.execute(
|
||||
"SELECT id FROM http_functions WHERE id = %s AND user_id = %s",
|
||||
[function_id, user_id],
|
||||
one=True
|
||||
)
|
||||
if not http_function:
|
||||
return jsonify({"status": "error", "message": "Function not found"}), 404
|
||||
|
||||
# Fetch the content of the selected version
|
||||
version_data = db.execute(
|
||||
"SELECT script_content FROM http_functions_versions WHERE http_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 http_functions SET script_content = %s WHERE id = %s",
|
||||
[version_data["script_content"], 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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user