wip
This commit is contained in:
parent
0a31714a93
commit
66fe31eabe
3 changed files with 419 additions and 206 deletions
|
@ -19,6 +19,7 @@ import markdown
|
|||
from datetime import datetime
|
||||
from flask import flash
|
||||
from app.utils.app_utils import is_port_in_use, validate_port_data
|
||||
from difflib import SequenceMatcher
|
||||
|
||||
bp = Blueprint("api", __name__, url_prefix="/api")
|
||||
csrf = CSRFProtect()
|
||||
|
@ -424,7 +425,7 @@ def get_free_port(server_id):
|
|||
@bp.route("/validate/app-name", methods=["GET"])
|
||||
@login_required
|
||||
def validate_app_name():
|
||||
"""API endpoint to validate application name in real-time"""
|
||||
"""API endpoint to validate application name in real-time with fuzzy matching"""
|
||||
name = request.args.get("name", "").strip()
|
||||
server_id = request.args.get("server_id")
|
||||
app_id = request.args.get("app_id")
|
||||
|
@ -448,18 +449,41 @@ def validate_app_name():
|
|||
"edit_url": url_for('dashboard.app_edit', app_id=existing_app.id)
|
||||
})
|
||||
|
||||
# Check for similar names
|
||||
similar_apps = App.query.filter(
|
||||
App.name.ilike(f"%{name}%"),
|
||||
App.server_id == server_id
|
||||
).limit(3).all()
|
||||
# Get all apps on this server for similarity check
|
||||
server_apps = App.query.filter(App.server_id == server_id).all()
|
||||
|
||||
if similar_apps and (not app_id or not any(str(app.id) == app_id for app in similar_apps)):
|
||||
similar_names = [{"name": app.name, "id": app.id} for app in similar_apps]
|
||||
# Filter out the current app if we're editing
|
||||
if app_id:
|
||||
server_apps = [app for app in server_apps if str(app.id) != app_id]
|
||||
|
||||
# Find similar apps using fuzzy matching
|
||||
similar_apps = []
|
||||
|
||||
for app in server_apps:
|
||||
# Calculate similarity ratio using SequenceMatcher
|
||||
similarity = SequenceMatcher(None, name.lower(), app.name.lower()).ratio()
|
||||
|
||||
# Consider similar if:
|
||||
# 1. One is a substring of the other OR
|
||||
# 2. They have high similarity ratio (over 0.7)
|
||||
is_substring = name.lower() in app.name.lower() or app.name.lower() in name.lower()
|
||||
is_similar = similarity > 0.7
|
||||
|
||||
if (is_substring or is_similar) and len(name) >= 3:
|
||||
similar_apps.append({
|
||||
"name": app.name,
|
||||
"id": app.id,
|
||||
"similarity": similarity
|
||||
})
|
||||
|
||||
# Sort by similarity (highest first) and limit to top 3
|
||||
similar_apps = sorted(similar_apps, key=lambda x: x["similarity"], reverse=True)[:3]
|
||||
|
||||
if similar_apps:
|
||||
return jsonify({
|
||||
"valid": True,
|
||||
"warning": "Similar application names found",
|
||||
"similar_apps": similar_names
|
||||
"similar_apps": similar_apps
|
||||
})
|
||||
|
||||
return jsonify({"valid": True})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue