Files
function/routes/community.py
2025-11-21 10:30:14 +11:00

51 lines
1.9 KiB
Python

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