38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
def validate_app_data(name, server_id, existing_app_id=None):
|
|
"""
|
|
Validate application data, checking for required fields and uniqueness
|
|
|
|
Args:
|
|
name: The application name
|
|
server_id: The server ID
|
|
existing_app_id: The ID of the application being edited (if any)
|
|
|
|
Returns:
|
|
tuple: (valid, error_message)
|
|
"""
|
|
# Check for required fields
|
|
if not name or not name.strip():
|
|
return False, "Application name is required"
|
|
|
|
if not server_id:
|
|
return False, "Server is required"
|
|
|
|
# Check name length
|
|
if len(name) < 2:
|
|
return False, "Application name must be at least 2 characters"
|
|
|
|
# Check for uniqueness of name on the server
|
|
from app.models import App
|
|
|
|
query = App.query.filter(App.name == name, App.server_id == server_id)
|
|
|
|
# If editing an existing app, exclude it from the uniqueness check
|
|
if existing_app_id:
|
|
query = query.filter(App.id != existing_app_id)
|
|
|
|
existing_app = query.first()
|
|
|
|
if existing_app:
|
|
return False, f"Application '{name}' already exists on this server"
|
|
|
|
return True, None
|