diff --git a/__pycache__/config.cpython-313.pyc b/__pycache__/config.cpython-313.pyc deleted file mode 100644 index 30bb4be..0000000 Binary files a/__pycache__/config.cpython-313.pyc and /dev/null differ diff --git a/app/__pycache__/__init__.cpython-313.pyc b/app/__pycache__/__init__.cpython-313.pyc deleted file mode 100644 index 38132e0..0000000 Binary files a/app/__pycache__/__init__.cpython-313.pyc and /dev/null differ diff --git a/app/core/__pycache__/auth.cpython-313.pyc b/app/core/__pycache__/auth.cpython-313.pyc deleted file mode 100644 index baa0e7c..0000000 Binary files a/app/core/__pycache__/auth.cpython-313.pyc and /dev/null differ diff --git a/app/core/__pycache__/extensions.cpython-313.pyc b/app/core/__pycache__/extensions.cpython-313.pyc deleted file mode 100644 index f73c28f..0000000 Binary files a/app/core/__pycache__/extensions.cpython-313.pyc and /dev/null differ diff --git a/app/core/__pycache__/models.cpython-313.pyc b/app/core/__pycache__/models.cpython-313.pyc deleted file mode 100644 index 905c29a..0000000 Binary files a/app/core/__pycache__/models.cpython-313.pyc and /dev/null differ diff --git a/app/core/__pycache__/template_filters.cpython-313.pyc b/app/core/__pycache__/template_filters.cpython-313.pyc deleted file mode 100644 index af6067a..0000000 Binary files a/app/core/__pycache__/template_filters.cpython-313.pyc and /dev/null differ diff --git a/app/routes/__pycache__/api.cpython-313.pyc b/app/routes/__pycache__/api.cpython-313.pyc deleted file mode 100644 index e859e0b..0000000 Binary files a/app/routes/__pycache__/api.cpython-313.pyc and /dev/null differ diff --git a/app/routes/__pycache__/auth.cpython-313.pyc b/app/routes/__pycache__/auth.cpython-313.pyc deleted file mode 100644 index e8382ee..0000000 Binary files a/app/routes/__pycache__/auth.cpython-313.pyc and /dev/null differ diff --git a/app/routes/__pycache__/dashboard.cpython-313.pyc b/app/routes/__pycache__/dashboard.cpython-313.pyc deleted file mode 100644 index 2b89819..0000000 Binary files a/app/routes/__pycache__/dashboard.cpython-313.pyc and /dev/null differ diff --git a/app/routes/__pycache__/importexport.cpython-313.pyc b/app/routes/__pycache__/importexport.cpython-313.pyc deleted file mode 100644 index 3cfde01..0000000 Binary files a/app/routes/__pycache__/importexport.cpython-313.pyc and /dev/null differ diff --git a/app/routes/__pycache__/ipam.cpython-313.pyc b/app/routes/__pycache__/ipam.cpython-313.pyc deleted file mode 100644 index 7c694f5..0000000 Binary files a/app/routes/__pycache__/ipam.cpython-313.pyc and /dev/null differ diff --git a/app/routes/__pycache__/static.cpython-313.pyc b/app/routes/__pycache__/static.cpython-313.pyc deleted file mode 100644 index c135bf5..0000000 Binary files a/app/routes/__pycache__/static.cpython-313.pyc and /dev/null differ diff --git a/app/routes/api.py b/app/routes/api.py index 8cd2acb..bd0adca 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -1,4 +1,13 @@ -from flask import Blueprint, jsonify, request, abort, current_app, render_template, redirect, url_for +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 @@ -277,59 +286,77 @@ def suggest_port(server_id): def add_app_port(app_id): """Add a port to an application""" app = App.query.get_or_404(app_id) - + try: port_number = request.form.get("port_number") protocol = request.form.get("protocol", "TCP") description = request.form.get("description", "") - + # Validate port data - valid, clean_port, error = validate_port_data(port_number, protocol, description) - + valid, clean_port, error = validate_port_data( + port_number, protocol, description + ) + if not valid: 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 - + 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() + existing_port = Port.query.filter_by( + app_id=app_id, port_number=clean_port + ).first() if existing_port: 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 - + 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( app_id=app_id, port_number=clean_port, protocol=protocol, - description=description + description=description, ) db.session.add(new_port) db.session.commit() - + 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": success_msg, - "port": { - "id": new_port.id, - "number": new_port.port_number, - "protocol": new_port.protocol, - "description": new_port.description + return jsonify( + { + "success": True, + "message": success_msg, + "port": { + "id": new_port.id, + "number": new_port.port_number, + "protocol": new_port.protocol, + "description": new_port.description, + }, } - }) - + ) + except Exception as e: db.session.rollback() 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 + 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//ports", methods=["GET"]) @@ -362,19 +389,21 @@ 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() - flash(f"Port {port.port_number}/{port.protocol} deleted successfully", "success") + flash( + f"Port {port.port_number}/{port.protocol} deleted successfully", "success" + ) except Exception as e: db.session.rollback() flash(f"Error deleting port: {str(e)}", "danger") - + return redirect(url_for("dashboard.app_view", app_id=app_id)) diff --git a/app/routes/dashboard.py b/app/routes/dashboard.py index 1800518..f1defda 100644 --- a/app/routes/dashboard.py +++ b/app/routes/dashboard.py @@ -218,52 +218,65 @@ def server_delete(server_id): @bp.route("/app/new", methods=["GET", "POST"]) +@bp.route("/app/new/", methods=["GET", "POST"]) @login_required -def app_new(): - """Create a new application with comprehensive error handling""" - # Get all servers for dropdown +def app_new(server_id=None): + """Create a new application""" servers = Server.query.all() - if not servers: - flash("You need to create a server before adding applications", "warning") - return redirect(url_for("dashboard.server_new")) - if request.method == "POST": - # Get form data - name = request.form.get("name", "").strip() + # Handle form submission + name = request.form.get("name") server_id = request.form.get("server_id") documentation = request.form.get("documentation", "") - # Process port data from form - port_data = [] - port_numbers = request.form.getlist("port_numbers[]") - protocols = request.form.getlist("protocols[]") - descriptions = request.form.getlist("port_descriptions[]") + if not name or not server_id: + flash("Name and server are required", "danger") + return render_template( + "dashboard/app_form.html", + title="New Application", + edit_mode=False, + servers=servers, + selected_server_id=server_id, + ) - for i in range(len(port_numbers)): - if port_numbers[i] and port_numbers[i].strip(): - protocol = protocols[i] if i < len(protocols) else "TCP" - description = descriptions[i] if i < len(descriptions) else "" - port_data.append((port_numbers[i], protocol, description)) + # Create the app + app = App(name=name, server_id=server_id, documentation=documentation) - # Save application - from app.utils.app_utils import save_app + try: + db.session.add(app) + db.session.commit() - success, app, error = save_app(name, server_id, documentation, port_data) + # Process port numbers if any + port_numbers = request.form.getlist("port_numbers[]") + protocols = request.form.getlist("protocols[]") + descriptions = request.form.getlist("port_descriptions[]") - if success: - flash("Application created successfully", "success") + for i, port_number in enumerate(port_numbers): + if port_number and port_number.isdigit(): + port = Port( + app_id=app.id, + port_number=int(port_number), + protocol=protocols[i] if i < len(protocols) else "TCP", + description=descriptions[i] if i < len(descriptions) else "", + ) + db.session.add(port) + + db.session.commit() + flash(f"Application '{name}' created successfully", "success") return redirect(url_for("dashboard.app_view", app_id=app.id)) - else: - flash(error, "danger") - # For GET requests or failed POSTs + except Exception as e: + db.session.rollback() + flash(f"Error creating application: {str(e)}", "danger") + + # GET request - render the form return render_template( "dashboard/app_form.html", - title="Create New Application", + title="New Application", edit_mode=False, - dashboard_link=url_for("dashboard.dashboard_home"), servers=servers, + selected_server_id=server_id, # This will pre-select the server ) diff --git a/app/scripts/__pycache__/ip_scanner.cpython-313.pyc b/app/scripts/__pycache__/ip_scanner.cpython-313.pyc deleted file mode 100644 index 202d100..0000000 Binary files a/app/scripts/__pycache__/ip_scanner.cpython-313.pyc and /dev/null differ diff --git a/app/templates/dashboard/app_form.html b/app/templates/dashboard/app_form.html index bf1992b..7d136eb 100644 --- a/app/templates/dashboard/app_form.html +++ b/app/templates/dashboard/app_form.html @@ -58,7 +58,8 @@