Refactor HTTP function editor with Mithril components and new API endpoints

- Add new Mithril components for editor (editor.js), response view (responseView.js), and alerts (alert.js)
- Create new API endpoints for creating, updating, and deleting HTTP functions
- Update templates to use new Mithril editor component
- Improve header navigation with more consistent styling and active state indicators
- Remove old edit form route and template
- Add new dedicated editor route and template

cursor.ai
This commit is contained in:
Peter Stockings
2025-02-14 00:40:45 +11:00
parent abf3ca15d2
commit 5ec8bba9e8
15 changed files with 1230 additions and 131 deletions

141
app.py
View File

@@ -151,24 +151,6 @@ def create_http_function():
print(e)
return { "status": "error", "message": str(e) }
@ app.route("/dashboard/http_functions/<int:function_id>/edit_form", methods=["GET"])
@login_required
def get_http_function_edit_form(function_id):
user_id = current_user.id
http_function = db.get_http_function_by_id(user_id, function_id)
if not http_function:
return jsonify({'error': 'Function not found'}), 404
name = http_function['name']
script = http_function['script_content']
environment_info = json.dumps(http_function['environment_info'], indent=2)
is_public = http_function['is_public']
log_request = http_function['log_request']
log_response = http_function['log_response']
version_number = http_function['version_number']
if htmx:
return render_block(app.jinja_env, 'dashboard/http_functions/edit.html', 'page', user_id=user_id, function_id=function_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number)
return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, function_id=function_id, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number)
@ app.route("/dashboard/http_functions/<int:function_id>/edit", methods=["POST"])
@login_required
@@ -437,6 +419,129 @@ def logout():
logout_user()
return redirect(url_for('home'))
@app.route("/http_function_editor/<int:function_id>", methods=["GET"])
@login_required
def http_function_editor(function_id):
user_id = current_user.id
http_function = db.get_http_function_by_id(user_id, function_id)
if not http_function:
return jsonify({'error': 'Function not found'}), 404
# Create a view model with all necessary data for the editor
editor_data = {
'id': http_function['id'],
'name': http_function['name'],
'script_content': http_function['script_content'],
'environment_info': json.dumps(http_function['environment_info'], indent=2),
'is_public': http_function['is_public'],
'log_request': http_function['log_request'],
'log_response': http_function['log_response'],
'version_number': http_function['version_number'],
'user_id': user_id,
'function_id': function_id,
# Add new URLs for navigation
'cancel_url': url_for('dashboard_http_functions'),
'edit_url': url_for('http_function_editor', function_id=function_id),
}
if htmx:
return render_block(app.jinja_env, "dashboard/http_functions/editor.html", "page", **editor_data)
return render_template("dashboard/http_functions/editor.html", **editor_data)
@app.route("/api/http_functions", methods=["POST"])
@login_required
def api_create_http_function():
try:
user_id = current_user.id
data = request.get_json()
name = data.get('name')
script_content = data.get('script_content')
environment_info = data.get('environment_info')
is_public = data.get('is_public')
log_request = data.get('log_request')
log_response = data.get('log_response')
# Check if function with same name already exists for this user
existing_function = db.get_http_function(user_id, name)
if existing_function:
return jsonify({
"status": "error",
"message": f"A function with the name '{name}' already exists"
}), 400
http_function = db.create_new_http_function(
user_id,
name,
script_content,
environment_info,
is_public,
log_request,
log_response
)
return jsonify({
"status": "success",
"message": f'{name} created',
"function": http_function
})
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 400
@app.route("/api/http_functions/<int:function_id>", methods=["POST"])
@login_required
def api_update_http_function(function_id):
try:
user_id = current_user.id
data = request.get_json()
name = data.get('name')
script_content = data.get('script_content')
environment_info = data.get('environment_info')
is_public = data.get('is_public')
log_request = data.get('log_request')
log_response = data.get('log_response')
updated_function = db.edit_http_function(
user_id,
function_id,
name,
script_content,
environment_info,
is_public,
log_request,
log_response
)
return jsonify({
"status": "success",
"message": f'{name} updated',
"function": updated_function
})
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 400
@app.route("/api/http_functions/<int:function_id>", methods=["DELETE"])
@login_required
def api_delete_http_function(function_id):
try:
user_id = current_user.id
db.delete_http_function(user_id, function_id)
return jsonify({
"status": "success",
"message": "Function deleted successfully"
})
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 400
@login_manager.user_loader
def load_user(user_id):