wip
This commit is contained in:
parent
d79359cd65
commit
30e9c9328e
18 changed files with 320 additions and 141 deletions
|
@ -1,4 +1,4 @@
|
|||
from flask import Blueprint, jsonify, request, abort, current_app, render_template
|
||||
from flask import Blueprint, jsonify, request, abort, current_app, render_template, redirect, url_for
|
||||
from flask_login import login_required
|
||||
from app.core.models import Subnet, Server, App, Port
|
||||
from app.core.extensions import db
|
||||
|
@ -197,15 +197,6 @@ def status():
|
|||
return jsonify({"status": "OK"})
|
||||
|
||||
|
||||
@bp.route("/markdown-preview", methods=["POST"])
|
||||
@csrf.exempt # Remove this line in production! Temporary fix for demo purposes
|
||||
def markdown_preview():
|
||||
data = request.json
|
||||
md_content = data.get("markdown", "")
|
||||
html = markdown.markdown(md_content)
|
||||
return jsonify({"html": html})
|
||||
|
||||
|
||||
@bp.route("/ports/suggest", methods=["GET"])
|
||||
def suggest_ports():
|
||||
app_type = request.args.get("type", "").lower()
|
||||
|
@ -296,15 +287,15 @@ def add_app_port(app_id):
|
|||
valid, clean_port, error = validate_port_data(port_number, protocol, description)
|
||||
|
||||
if not valid:
|
||||
return jsonify({"success": False, "error": error}), 400
|
||||
flash(error, "danger")
|
||||
return redirect(url_for("dashboard.app_view", app_id=app_id)) if not request.is_xhr else jsonify({"success": False, "error": error}), 400
|
||||
|
||||
# Check if port already exists
|
||||
existing_port = Port.query.filter_by(app_id=app_id, port_number=clean_port).first()
|
||||
if existing_port:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": f"Port {clean_port} already exists for this application"
|
||||
}), 400
|
||||
error_msg = f"Port {clean_port} already exists for this application"
|
||||
flash(error_msg, "warning")
|
||||
return redirect(url_for("dashboard.app_view", app_id=app_id)) if not request.is_xhr else jsonify({"success": False, "error": error_msg}), 400
|
||||
|
||||
# Create new port
|
||||
new_port = Port(
|
||||
|
@ -316,10 +307,17 @@ def add_app_port(app_id):
|
|||
db.session.add(new_port)
|
||||
db.session.commit()
|
||||
|
||||
flash(f"Port {clean_port}/{protocol} added successfully", "success")
|
||||
success_msg = f"Port {clean_port}/{protocol} added successfully"
|
||||
flash(success_msg, "success")
|
||||
|
||||
# If it's a regular form submission (not AJAX), redirect
|
||||
if not request.is_xhr and not request.is_json:
|
||||
return redirect(url_for("dashboard.app_view", app_id=app_id))
|
||||
|
||||
# Otherwise return JSON for API/AJAX calls
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": f"Port {clean_port}/{protocol} added successfully",
|
||||
"message": success_msg,
|
||||
"port": {
|
||||
"id": new_port.id,
|
||||
"number": new_port.port_number,
|
||||
|
@ -330,7 +328,8 @@ def add_app_port(app_id):
|
|||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 500
|
||||
flash(f"Error: {str(e)}", "danger")
|
||||
return redirect(url_for("dashboard.app_view", app_id=app_id)) if not request.is_xhr else jsonify({"success": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@bp.route("/app/<int:app_id>/ports", methods=["GET"])
|
||||
|
@ -357,25 +356,26 @@ def get_app_ports(app_id):
|
|||
return jsonify(result)
|
||||
|
||||
|
||||
@bp.route("/port/<int:port_id>/delete", methods=["POST"])
|
||||
@bp.route("/app/<int:app_id>/port/<int:port_id>/delete", methods=["POST"])
|
||||
@login_required
|
||||
def delete_port(port_id):
|
||||
"""Delete a port"""
|
||||
# Add CSRF validation
|
||||
if request.is_json: # For AJAX requests
|
||||
csrf_token = request.json.get("csrf_token")
|
||||
if not csrf_token or not csrf.validate_csrf(csrf_token):
|
||||
return jsonify({"success": False, "error": "CSRF validation failed"}), 403
|
||||
|
||||
def delete_app_port(app_id, port_id):
|
||||
"""Delete a port from an application"""
|
||||
app = App.query.get_or_404(app_id)
|
||||
port = Port.query.get_or_404(port_id)
|
||||
|
||||
|
||||
if port.app_id != app.id:
|
||||
flash("Port does not belong to this application", "danger")
|
||||
return redirect(url_for("dashboard.app_view", app_id=app_id))
|
||||
|
||||
try:
|
||||
db.session.delete(port)
|
||||
db.session.commit()
|
||||
return jsonify({"success": True, "message": f"Port {port.number} deleted"})
|
||||
flash(f"Port {port.port_number}/{port.protocol} deleted successfully", "success")
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"success": False, "error": str(e)}), 500
|
||||
flash(f"Error deleting port: {str(e)}", "danger")
|
||||
|
||||
return redirect(url_for("dashboard.app_view", app_id=app_id))
|
||||
|
||||
|
||||
@bp.route("/subnets/<int:subnet_id>/servers", methods=["GET"])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue