This commit is contained in:
pika 2025-04-03 13:51:52 +02:00
parent 78ce15e82d
commit 0a31714a93
10 changed files with 159 additions and 149 deletions

View file

@ -298,7 +298,6 @@ def app_view(app_id):
@login_required
def app_edit(app_id):
"""Edit an existing application with comprehensive error handling"""
# Get the application and all servers
app = App.query.get_or_404(app_id)
servers = Server.query.all()
@ -323,87 +322,57 @@ def app_edit(app_id):
# Check for port conflicts proactively
conflicts = []
seen_ports = set() # To track ports already seen in this submission
for i, (port_number, protocol, _) in enumerate(port_data):
try:
clean_port = int(port_number)
# Check if this port has already been seen in this submission
port_key = f"{clean_port}/{protocol}"
if port_key in seen_ports:
conflicts.append((clean_port, protocol, "Duplicate port in submission"))
continue
seen_ports.add(port_key)
# Check if the port is in use by another application
in_use, conflicting_app_name = is_port_in_use(
clean_port, protocol, server_id, exclude_app_id=app_id
)
if in_use:
conflicts.append((clean_port, protocol, conflicting_app_name))
conflicts.append((clean_port, protocol, f"Port {clean_port}/{protocol} is already in use by application '{conflicting_app_name}'"))
except (ValueError, TypeError):
continue
if conflicts:
# Find the IDs of conflicting apps for linking
conflict_msgs = []
for port, protocol, conflict_app_name in conflicts:
conflict_app = App.query.filter_by(name=conflict_app_name, server_id=server_id).first()
if conflict_app:
edit_url = url_for('dashboard.app_edit', app_id=conflict_app.id)
conflict_msgs.append(
f'Port {port}/{protocol} is in use by <a href="{edit_url}">{conflict_app_name}</a>'
)
else:
conflict_msgs.append(f'Port {port}/{protocol} is in use by {conflict_app_name}')
for msg in conflict_msgs:
flash(msg, "danger")
return render_template(
"dashboard/app_form.html",
title=f"Edit {app.name}",
edit_mode=True,
servers=servers,
app=app
)
for conflict in conflicts:
flash(f"Conflict: {conflict[0]}/{conflict[1]} - {conflict[2]}", "danger")
return render_template("dashboard/app_edit.html", app=app, servers=servers)
# Replace local validation with shared function
valid, error = validate_app_data(name, server_id, existing_app_id=app_id)
# Update application details
app.name = name
app.server_id = server_id
app.documentation = documentation
app.url = url
if valid:
# Update application with URL
app.name = name
app.server_id = server_id
app.documentation = documentation
app.url = url
# Only delete existing ports if new port data is provided
if port_data:
# Remove existing ports and add new ones
Port.query.filter_by(app_id=app_id).delete()
for port_number, protocol, description in port_data:
new_port = Port(
app_id=app_id,
port_number=int(port_number),
protocol=protocol,
description=description
)
db.session.add(new_port)
# Update application
from app.utils.app_utils import save_app
db.session.commit()
flash("Application updated successfully", "success")
return redirect(url_for("dashboard.app_view", app_id=app_id))
success, updated_app, error = save_app(
name, server_id, documentation, port_data, app_id, url
)
if success:
flash("Application updated successfully", "success")
return redirect(url_for("dashboard.app_view", app_id=app_id))
else:
flash(error, "danger")
# Extract app name from error and provide link if it's a conflict
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=server_id).first()
if conflict_app:
edit_url = url_for('dashboard.app_edit', app_id=conflict_app.id)
flash(
f'Would you like to edit the conflicting application? '
f'<a href="{edit_url}">Edit {app_name}</a>',
"info"
)
else:
flash(error, "danger")
# GET request - display the form
return render_template(
"dashboard/app_form.html",
title=f"Edit {app.name}",
edit_mode=True,
servers=servers,
app=app
)
return render_template("dashboard/app_edit.html", app=app, servers=servers)
@bp.route("/app/<int:app_id>/delete", methods=["POST"])

View file

@ -4,34 +4,34 @@ import os
bp = Blueprint("static_assets", __name__)
@bp.route("/static/libs/tabler-icons/tabler-icons.min.css")
def tabler_icons():
"""Serve tabler-icons CSS from node_modules or download if missing"""
icons_path = os.path.join(current_app.static_folder, "libs", "tabler-icons")
# Create directory if it doesn't exist
if not os.path.exists(icons_path):
os.makedirs(icons_path)
css_file = os.path.join(icons_path, "tabler-icons.min.css")
# If file doesn't exist, download from CDN
if not os.path.exists(css_file):
import requests
try:
cdn_url = "https://cdn.jsdelivr.net/npm/@tabler/icons@latest/iconfont/tabler-icons.min.css"
response = requests.get(cdn_url)
if response.status_code == 200:
with open(css_file, "wb") as f:
f.write(response.content)
print(f"Downloaded tabler-icons.min.css from CDN")
else:
print(f"Failed to download tabler-icons CSS: {response.status_code}")
except Exception as e:
print(f"Error downloading tabler-icons CSS: {e}")
return send_from_directory(icons_path, "tabler-icons.min.css")
# @bp.route("/static/libs/tabler-icons/tabler-icons.min.css")
# def tabler_icons():
# """Serve tabler-icons CSS from node_modules or download if missing"""
# icons_path = os.path.join(current_app.static_folder, "libs", "tabler-icons")
#
# # Create directory if it doesn't exist
# if not os.path.exists(icons_path):
# os.makedirs(icons_path)
#
# css_file = os.path.join(icons_path, "tabler-icons.min.css")
#
# # If file doesn't exist, download from CDN
# if not os.path.exists(css_file):
# import requests
#
# try:
# cdn_url = "https://cdn.jsdelivr.net/npm/@tabler/core@1.1.1/dist/css/tabler.min.css"
# response = requests.get(cdn_url)
# if response.status_code == 200:
# with open(css_file, "wb") as f:
# f.write(response.content)
# print(f"Downloaded tabler-icons.min.css from CDN")
# else:
# print(f"Failed to download tabler-icons CSS: {response.status_code}")
# except Exception as e:
# print(f"Error downloading tabler-icons CSS: {e}")
#
# return send_from_directory(icons_path, "tabler-icons.min.css")
@bp.route("/static/css/tabler.min.css")
@ -50,7 +50,7 @@ def tabler_css():
import requests
try:
cdn_url = "https://cdn.jsdelivr.net/npm/@tabler/core@latest/dist/css/tabler.min.css"
cdn_url = "https://cdn.jsdelivr.net/npm/@tabler/core@1.1.1/dist/css/tabler.min.css"
response = requests.get(cdn_url)
if response.status_code == 200:
with open(css_file, "wb") as f:
@ -82,7 +82,7 @@ def favicon():
try:
# Using a simple placeholder favicon
cdn_url = "https://www.google.com/favicon.ico"
cdn_url = "https://www.svgrepo.com/show/529863/server-minimalistic.svg"
response = requests.get(cdn_url)
if response.status_code == 200:
with open(favicon_file, "wb") as f: