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

@ -38,15 +38,6 @@ def validate_app_data(name, server_id, existing_app_id=None):
def is_port_in_use(port_number, protocol, server_id, exclude_app_id=None):
"""
Check if a port+protocol combination is already in use by any application on the server
Args:
port_number: The port number to check
protocol: The protocol (TCP, UDP, etc.)
server_id: The server ID
exclude_app_id: Optional app ID to exclude from the check (for edit operations)
Returns:
Tuple (bool, app_name): Is port in use and by which app
"""
from sqlalchemy import and_
@ -57,9 +48,9 @@ def is_port_in_use(port_number, protocol, server_id, exclude_app_id=None):
App.server_id == server_id
)
# Exclude the current app if editing
if exclude_app_id:
query = query.filter(App.id != exclude_app_id)
# # Exclude the current app if editing
# if exclude_app_id:
# query = query.filter(App.id != exclude_app_id)
result = query.first()
@ -70,10 +61,19 @@ def is_port_in_use(port_number, protocol, server_id, exclude_app_id=None):
def validate_port_data(port_number, protocol, description, server_id=None, app_id=None):
"""
Validate port data
Returns tuple (valid, clean_port, error_message)
Validate port data for an application
Args:
port_number: The port number to validate
protocol: The protocol (TCP/UDP)
description: Port description
server_id: ID of the server
app_id: ID of the application (for excluding the current app when checking conflicts)
Returns:
Tuple of (valid, clean_port, error_message)
"""
# Existing validation
# Check if port number is provided
if not port_number:
return False, None, "Port number is required"
@ -85,11 +85,10 @@ def validate_port_data(port_number, protocol, description, server_id=None, app_i
if clean_port < 1 or clean_port > 65535:
return False, None, "Port number must be between 1 and 65535"
# Add server-side check for conflicts if server_id is provided
if server_id:
in_use, app_name = is_port_in_use(clean_port, protocol, server_id, app_id)
if in_use:
return False, clean_port, f"Port {clean_port}/{protocol} is already in use by application '{app_name}'"
# Always check for port conflicts
in_use, app_name = is_port_in_use(clean_port, protocol, server_id, app_id)
if in_use:
return False, clean_port, f"Port {clean_port}/{protocol} is already in use by application '{app_name}'"
return True, clean_port, None