From 0a99abb52d038be455c7b8306f9c6d6c1e4dfa75 Mon Sep 17 00:00:00 2001 From: pika Date: Mon, 31 Mar 2025 01:03:02 +0200 Subject: [PATCH] wip --- .gitignore | 84 +++++++ app/routes/api.py | 56 ++--- app/templates/dashboard/app_view.html | 13 +- app/templates/dashboard/server_view.html | 274 +++++++++++++---------- 4 files changed, 279 insertions(+), 148 deletions(-) diff --git a/.gitignore b/.gitignore index 121a57c..1518c81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,86 @@ tests/__pycache__/conftest.cpython-313-pytest-8.3.5.pyc scripts/__pycache__/check_routes.cpython-313-pytest-8.3.5.pyc + +# Python bytecode +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +dist/ +build/ +*.egg-info/ + +# Virtual environments +venv/ +env/ +ENV/ +.env/ +.venv/ + +# Flask stuff +instance/ +.webassets-cache +flask_session/ + +# SQLite database files +*.sqlite +*.sqlite3 +*.db + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Jupyter Notebook +.ipynb_checkpoints + +# VS Code +.vscode/ +*.code-workspace + +# PyCharm +.idea/ +*.iml +*.iws +*.ipr + +# Environment variables +.env +.env.local +.env.development +.env.test +.env.production + +# Logs +logs/ +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# System files +.DS_Store +Thumbs.db +ehthumbs.db +Desktop.ini + +# User-specific files +*.swp +*.swo +*~ + +# Local development settings +local_settings.py + +# Media and static files (if collected locally) +/media/ +/static/collected/ diff --git a/app/routes/api.py b/app/routes/api.py index bd0adca..7a7dce6 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -286,25 +286,28 @@ 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) - + + # Check if request is AJAX (XMLHttpRequest) + is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.accept_mimetypes.best == 'application/json' + 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 ) - + if not valid: flash(error, "danger") return ( redirect(url_for("dashboard.app_view", app_id=app_id)) - if not request.is_xhr + if not is_ajax 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 @@ -314,10 +317,10 @@ def add_app_port(app_id): flash(error_msg, "warning") return ( redirect(url_for("dashboard.app_view", app_id=app_id)) - if not request.is_xhr + if not is_ajax else jsonify({"success": False, "error": error_msg}) ), 400 - + # Create new port new_port = Port( app_id=app_id, @@ -327,35 +330,34 @@ def add_app_port(app_id): ) 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: + if not is_ajax and request.content_type != 'application/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, - }, + + # Otherwise return JSON response + return jsonify({ + "success": True, + "message": success_msg, + "port": { + "id": new_port.id, + "port_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") + error_msg = f"Error adding port: {str(e)}" + flash(error_msg, "danger") return ( redirect(url_for("dashboard.app_view", app_id=app_id)) - if not request.is_xhr - else jsonify({"success": False, "error": str(e)}) + if not is_ajax + else jsonify({"success": False, "error": error_msg}) ), 500 diff --git a/app/templates/dashboard/app_view.html b/app/templates/dashboard/app_view.html index ff31c16..064e303 100644 --- a/app/templates/dashboard/app_view.html +++ b/app/templates/dashboard/app_view.html @@ -71,14 +71,14 @@ PORT PROTOCOL DESCRIPTION - + {% for port in app.ports %} {{ port.port_number }} - {{ port.protocol }} + {{ port.protocol }} {{ port.description }} @@ -355,7 +353,8 @@ const appId = {{ app.id }}; let portToDelete = null; - // Function to handle port delete confirmation + // IMPORTANT: Define confirmDeletePort outside the DOMContentLoaded event + // so it's available in the global scope function confirmDeletePort(portId) { portToDelete = portId; const modal = new bootstrap.Modal(document.getElementById('deletePortModal')); diff --git a/app/templates/dashboard/server_view.html b/app/templates/dashboard/server_view.html index 03cc608..6c6576d 100644 --- a/app/templates/dashboard/server_view.html +++ b/app/templates/dashboard/server_view.html @@ -11,14 +11,15 @@

{{ server.hostname }}

+
{{ server.ip_address }}
-
+ @@ -26,7 +27,7 @@
-
+
@@ -34,11 +35,11 @@
- -
+ +
-
-
- Free - Used -
+
+ + Free + + + Used + +
-
- -
+
+

Used Ports

+
+ {% set used_ports = [] %} + {% for app in server.apps %} + {% for port in app.ports %} + {% set _ = used_ports.append({'port': port.port_number, 'protocol': port.protocol, 'app': app.name}) %} + {% endfor %} + {% endfor %} -
-

Used Ports

-
- -
+ {% if used_ports %} + {% for port_info in used_ports %} + + {{ port_info.port }}/{{ port_info.protocol }} + + {% endfor %} + {% else %} + No ports in use + {% endif %}
-
+

Documentation

-
+
{% if server.documentation %} - {{ server.documentation|markdown|safe }} - {% else %} -
-
- -
-

No documentation available

-

- Add documentation to this server to keep track of important information. -

- +
+ {{ server.documentation|markdown|safe }}
+ {% else %} +
No documentation added yet.
{% endif %}
-
+

Applications

-
- - Add Application +
+ + Add Application - + {% endif %}
{% if server.apps %} -
+
{% for app in server.apps %}

-

-
+
-
- - View - - - Edit - - +
+

{{ app.name }}

+
+ + View + + + Edit + + +
+ {% if app.documentation %} -
- {{ app.documentation|markdown|safe }} +
+
Documentation
+
+ {{ (app.documentation | truncate(200, true, "..."))|markdown|safe }} + {% if app.documentation | length > 200 %} + Read more... + {% endif %} +
- {% else %} -
No documentation available
{% endif %}
@@ -201,8 +213,7 @@ - +