Add community section where public functions can be viewed
This commit is contained in:
50
routes/community.py
Normal file
50
routes/community.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify
|
||||
from flask_login import login_required, current_user
|
||||
from extensions import db, environment, htmx
|
||||
from jinja2_fragments import render_block
|
||||
import json
|
||||
|
||||
community = Blueprint('community', __name__)
|
||||
|
||||
@community.route("/", methods=["GET"])
|
||||
@login_required
|
||||
def index():
|
||||
search_query = request.args.get("q", "")
|
||||
public_functions = db.get_public_http_functions(search_query)
|
||||
|
||||
if htmx:
|
||||
return render_block(
|
||||
environment,
|
||||
"community/index.html",
|
||||
"page",
|
||||
public_functions=public_functions,
|
||||
search_query=search_query
|
||||
)
|
||||
return render_template("community/index.html", public_functions=public_functions, search_query=search_query)
|
||||
|
||||
@community.route("/<int:function_id>", methods=["GET"])
|
||||
@login_required
|
||||
def view(function_id):
|
||||
function = db.get_public_http_function_by_id(function_id)
|
||||
if not function:
|
||||
flash("Function not found or not public", "error")
|
||||
return redirect(url_for("community.index"))
|
||||
|
||||
# Format environment info for display
|
||||
if function.get('environment_info'):
|
||||
function['environment_info'] = json.dumps(function['environment_info'], indent=2)
|
||||
|
||||
if htmx:
|
||||
return render_block(environment, "community/view.html", "page", function=function)
|
||||
return render_template("community/view.html", function=function)
|
||||
|
||||
@community.route("/fork/<int:function_id>", methods=["POST"])
|
||||
@login_required
|
||||
def fork(function_id):
|
||||
try:
|
||||
user_id = current_user.id
|
||||
new_name = db.fork_http_function(user_id, function_id)
|
||||
flash(f"Function forked as '{new_name}'", "success")
|
||||
return jsonify({"status": "success", "redirect": url_for("http.overview")})
|
||||
except Exception as e:
|
||||
return jsonify({"status": "error", "message": str(e)}), 400
|
||||
@@ -166,6 +166,7 @@ def new():
|
||||
"is_public": False,
|
||||
"log_request": True,
|
||||
"log_response": False,
|
||||
"description": "",
|
||||
}
|
||||
if htmx:
|
||||
return render_block(
|
||||
@@ -181,6 +182,7 @@ def new():
|
||||
log_request = request.json.get("log_request")
|
||||
log_response = request.json.get("log_response")
|
||||
runtime = request.json.get("runtime", "node")
|
||||
description = request.json.get("description", "")
|
||||
|
||||
db.create_new_http_function(
|
||||
user_id,
|
||||
@@ -192,6 +194,7 @@ def new():
|
||||
log_request,
|
||||
log_response,
|
||||
runtime,
|
||||
description
|
||||
)
|
||||
|
||||
return (
|
||||
@@ -218,6 +221,7 @@ def edit(function_id):
|
||||
log_request = request.json.get("log_request")
|
||||
log_response = request.json.get("log_response")
|
||||
runtime = request.json.get("runtime", "node")
|
||||
description = request.json.get("description", "")
|
||||
|
||||
updated_version = db.edit_http_function(
|
||||
user_id,
|
||||
@@ -230,6 +234,7 @@ def edit(function_id):
|
||||
log_request,
|
||||
log_response,
|
||||
runtime,
|
||||
description
|
||||
)
|
||||
|
||||
return {"status": "success", "message": f"{name} updated"}
|
||||
@@ -374,6 +379,7 @@ def editor(function_id):
|
||||
"log_response": http_function["log_response"],
|
||||
"version_number": http_function["version_number"],
|
||||
"runtime": http_function.get("runtime", "node"),
|
||||
"description": http_function.get("description", ""),
|
||||
"user_id": user_id,
|
||||
"function_id": function_id,
|
||||
# Add new URLs for navigation
|
||||
|
||||
Reference in New Issue
Block a user