wip
This commit is contained in:
parent
254593d260
commit
0a99abb52d
4 changed files with 279 additions and 148 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue