diff --git a/app.py b/app.py index 8e67339..9fe2732 100644 --- a/app.py +++ b/app.py @@ -160,34 +160,29 @@ class UserDynamicVariables(db.Model): user = db.relationship('User', backref='dynamic_variables') - # Funkcje pomocnicze - def get_user_dynamic_variables(user_id): user_variables = UserDynamicVariables.query.filter_by(user_id=user_id).all() return {var.variable_name: var.variable_value for var in user_variables} -def ensure_local_defaults(content, user_id): - default_entries = LocalDefaultEntry.query.filter_by(user_id=user_id).all() - required_lines = [] +def format_host(host): + resolved_name = None + # Priorytet dla Linux Daemon + if host.use_daemon and host.type == 'linux' and host.daemon_url: + resolved_name = host.resolved_daemon or host.hostname - for entry in default_entries: - # Sprawdzenie, czy wpis zawiera zarówno IP, jak i hostname - if entry.ip_address and entry.hostname: - required_lines.append(f"{entry.ip_address} {entry.hostname}".strip()) + # Dla standardowych hostów używamy resolved_hostname + if not resolved_name: + resolved_name = host.resolved_hostname or host.hostname - # Usuń puste linie i unikaj duplikatów - lines = [l.rstrip() for l in content.splitlines() if l.strip()] - lines = [line for line in lines if not any(line.startswith(e.split()[0]) for e in required_lines)] - - # Dodaj wymagane wpisy na górę listy - lines = required_lines + lines - - # Połącz w finalny format - final_content = "\n".join(lines) + "\n" - - return final_content + # Jeśli resolved_name nadal jest IP, spróbuj rozwiązać przez DNS + if resolved_name == host.raw_ip: + try: + resolved_name = socket.gethostbyaddr(host.raw_ip)[0] + except (socket.herror, socket.gaierror): + pass # Jeśli nie można rozwiązać, pozostaw IP + return f"{resolved_name} ({host.raw_ip})" def get_user_dynamic_variables(user_id): user_variables = UserDynamicVariables.query.filter_by(user_id=user_id).all()