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 @@