now the ports cannot be assigned twice, also simmilar application names work

This commit is contained in:
pika 2025-04-03 11:58:17 +02:00
parent e5300424ef
commit e9d1f985ae
7 changed files with 57 additions and 106 deletions

View file

@ -18,7 +18,7 @@ from flask_wtf import CSRFProtect
import markdown
from datetime import datetime
from flask import flash
from app.utils.app_utils import is_port_in_use
from app.utils.app_utils import is_port_in_use, validate_port_data
bp = Blueprint("api", __name__, url_prefix="/api")
csrf = CSRFProtect()
@ -287,89 +287,40 @@ 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'
# Get port details from the form
port_number = request.form.get("port_number")
protocol = request.form.get("protocol", "TCP")
description = request.form.get("description", "")
# Validate the port
valid, clean_port, error = validate_port_data ( # validate_port_data(
port_number,
protocol,
description,
app.server_id,
app_id
)
if not valid:
flash(error, "danger")
return redirect(url_for("dashboard.app_view", app_id=app_id))
# Create the new port
try:
port_number = request.form.get("port_number")
protocol = request.form.get("protocol", "TCP")
description = request.form.get("description", "")
# Validate port data with server-side conflict check
valid, clean_port, error = validate_port_data(
port_number, protocol, description, app.server_id, app_id
)
if not valid:
flash(error, "danger")
# If port is in use by another app, provide a direct link
if "already in use by application" in error:
app_name = error.split("'")[1] # Extract app name from error message
conflict_app = App.query.filter_by(name=app_name, server_id=app.server_id).first()
if conflict_app:
edit_url = url_for('dashboard.app_edit', app_id=conflict_app.id)
edit_link = f'<a href="{edit_url}">Edit {app_name}</a>'
flash(f"Would you like to edit the conflicting application? {edit_link}", "info")
return (
redirect(url_for("dashboard.app_view", app_id=app_id))
if not is_ajax
else jsonify({"success": False, "error": error})
), 400
# Check if port already exists for this app
existing_port = Port.query.filter_by(
app_id=app_id, port_number=clean_port, protocol=protocol
).first()
if existing_port:
error_msg = f"Port {clean_port}/{protocol} already exists for this application"
flash(error_msg, "warning")
return (
redirect(url_for("dashboard.app_view", app_id=app_id))
if not is_ajax
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 is_ajax and request.content_type != 'application/json':
return redirect(url_for("dashboard.app_view", app_id=app_id))
# 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
}
})
flash(f"Port {clean_port}/{protocol} added successfully", "success")
except Exception as e:
db.session.rollback()
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 is_ajax
else jsonify({"success": False, "error": error_msg})
), 500
flash(f"Error adding port: {str(e)}", "danger")
return redirect(url_for("dashboard.app_view", app_id=app_id))
@bp.route("/app/<int:app_id>/ports", methods=["GET"])