search multi user

This commit is contained in:
Mateusz Gruszczyński
2025-05-26 19:58:33 +02:00
parent 43b4007030
commit b3e944db20

72
app.py
View File

@ -108,8 +108,10 @@ def get_users(db_config):
connection.close() connection.close()
return users return users
def search_user(db_config, username): def search_user(db_config, usernames):
"""Wyszukuje użytkownika po nazwie i wyświetla jego statystyki.""" """Wyszukuje wielu użytkowników po nazwach i wyświetla ich statystyki w tabeli."""
usernames_list = [name.strip() for name in usernames.split(',')]
connection = mysql.connector.connect(**db_config) connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True) cursor = connection.cursor(dictionary=True)
@ -127,19 +129,36 @@ def search_user(db_config, username):
FROM users u FROM users u
LEFT JOIN node n ON u.uid = n.uid LEFT JOIN node n ON u.uid = n.uid
LEFT JOIN userpoints p ON u.uid = p.uid LEFT JOIN userpoints p ON u.uid = p.uid
WHERE u.name = %s WHERE u.name IN (%s)
GROUP BY u.uid GROUP BY u.uid
""" """
cursor.execute(query, (username,)) # Przygotowanie parametrów dla IN w zapytaniu SQL
user = cursor.fetchone() params = tuple(usernames_list)
query = query % (','.join(['%s'] * len(usernames_list)))
cursor.execute(query, params)
users = cursor.fetchall()
cursor.close() cursor.close()
connection.close() connection.close()
if not user: if not users:
print(f"Użytkownik '{username}' nie został znaleziony.") print(f"Żaden z użytkowników ({usernames}) nie został znaleziony.")
return None return None
# Przygotowanie danych do wyświetlenia
headers = [
"UID", "Nazwa użytkownika", "E-mail", "Status konta",
"Data rejestracji", "Ostatnie logowanie", "Ostatnia aktywność",
"Dni nieaktywności", "Punkty", "Liczba postów",
"E-mail poprawny", "Tymczasowy e-mail", "Podejrzana nazwa"
]
# Transpozycja danych - każdy użytkownik jako kolumna
table_data = []
for header in headers:
row = [header]
for user in users:
# Dodatkowe informacje o e-mailu # Dodatkowe informacje o e-mailu
temp_domains_cache = load_temp_domains() temp_domains_cache = load_temp_domains()
email_valid = not is_fake_email(user['mail']) and not is_temp_email(user['mail'], temp_domains_cache) email_valid = not is_fake_email(user['mail']) and not is_temp_email(user['mail'], temp_domains_cache)
@ -150,28 +169,31 @@ def search_user(db_config, username):
last_access = user['access'] or 0 last_access = user['access'] or 0
days_inactive = (now_ts - last_access) / 86400 if last_access else float('inf') days_inactive = (now_ts - last_access) / 86400 if last_access else float('inf')
# Przygotuj dane do wyświetlenia # Mapowanie danych
user_data = { user_data = {
'UID': user['uid'], "UID": user['uid'],
'Nazwa użytkownika': user['name'], "Nazwa użytkownika": user['name'],
'E-mail': user['mail'], "E-mail": user['mail'],
'Status konta': 'Aktywny' if user['status'] == 1 else 'Zablokowany', "Status konta": 'Aktywny' if user['status'] == 1 else 'Zablokowany',
'Data rejestracji': datetime.fromtimestamp(user['created']).strftime('%Y-%m-%d %H:%M:%S') if user.get('created') else 'Nieznana', "Data rejestracji": datetime.fromtimestamp(user['created']).strftime('%Y-%m-%d %H:%M:%S') if user.get('created') else 'Nieznana',
'Ostatnie logowanie': datetime.fromtimestamp(user['access']).strftime('%Y-%m-%d %H:%M:%S') if user.get('access') else 'Nigdy', "Ostatnie logowanie": datetime.fromtimestamp(user['access']).strftime('%Y-%m-%d %H:%M:%S') if user.get('access') else 'Nigdy',
'Ostatnia aktywność': datetime.fromtimestamp(user['login']).strftime('%Y-%m-%d %H:%M:%S') if user.get('login') else 'Nigdy', "Ostatnia aktywność": datetime.fromtimestamp(user['login']).strftime('%Y-%m-%d %H:%M:%S') if user.get('login') else 'Nigdy',
'Dni nieaktywności': f"{days_inactive:.1f} dni", "Dni nieaktywności": f"{days_inactive:.1f} dni",
'Punkty': user.get('points', 0), "Punkty": user.get('points', 0),
'Liczba postów': user.get('post_count', 0), "Liczba postów": user.get('post_count', 0),
'E-mail poprawny': 'Tak' if email_valid else 'Nie', "E-mail poprawny": 'Tak' if email_valid else 'Nie',
'Tymczasowy e-mail': 'Tak' if is_temp_email(user['mail'], temp_domains_cache) else 'Nie', "Tymczasowy e-mail": 'Tak' if is_temp_email(user['mail'], temp_domains_cache) else 'Nie',
'Podejrzana nazwa': 'Tak' if bad_name else 'Nie' "Podejrzana nazwa": 'Tak' if bad_name else 'Nie'
} }
# Wyświetl wyniki w tabeli row.append(user_data.get(header, ''))
print("\n🔍 Szczegóły użytkownika:") table_data.append(row)
print(tabulate([(k, v) for k, v in user_data.items()], tablefmt="fancy_grid"))
return user # Wyświetl wyniki w tabeli
print("\n🔍 Porównanie użytkowników:")
print(tabulate(table_data, headers=[""] + [user['name'] for user in users], tablefmt="fancy_grid"))
return users
def is_fake_email(email): def is_fake_email(email):
try: try: