search multi user
This commit is contained in:
		
							
								
								
									
										90
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										90
									
								
								app.py
									
									
									
									
									
								
							@@ -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,51 +129,71 @@ 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
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Dodatkowe informacje o e-mailu
 | 
					    # Przygotowanie danych do wyświetlenia
 | 
				
			||||||
    temp_domains_cache = load_temp_domains()
 | 
					    headers = [
 | 
				
			||||||
    email_valid = not is_fake_email(user['mail']) and not is_temp_email(user['mail'], temp_domains_cache)
 | 
					        "UID", "Nazwa użytkownika", "E-mail", "Status konta",
 | 
				
			||||||
    bad_name = is_bad_name(user['name'])
 | 
					        "Data rejestracji", "Ostatnie logowanie", "Ostatnia aktywność",
 | 
				
			||||||
 | 
					        "Dni nieaktywności", "Punkty", "Liczba postów",
 | 
				
			||||||
 | 
					        "E-mail poprawny", "Tymczasowy e-mail", "Podejrzana nazwa"
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Oblicz dni nieaktywności
 | 
					    # Transpozycja danych - każdy użytkownik jako kolumna
 | 
				
			||||||
    now_ts = int(datetime.now().timestamp())
 | 
					    table_data = []
 | 
				
			||||||
    last_access = user['access'] or 0
 | 
					    for header in headers:
 | 
				
			||||||
    days_inactive = (now_ts - last_access) / 86400 if last_access else float('inf')
 | 
					        row = [header]
 | 
				
			||||||
 | 
					        for user in users:
 | 
				
			||||||
 | 
					            # Dodatkowe informacje o e-mailu
 | 
				
			||||||
 | 
					            temp_domains_cache = load_temp_domains()
 | 
				
			||||||
 | 
					            email_valid = not is_fake_email(user['mail']) and not is_temp_email(user['mail'], temp_domains_cache)
 | 
				
			||||||
 | 
					            bad_name = is_bad_name(user['name'])
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
    # Przygotuj dane do wyświetlenia
 | 
					            # Oblicz dni nieaktywności
 | 
				
			||||||
    user_data = {
 | 
					            now_ts = int(datetime.now().timestamp())
 | 
				
			||||||
        'UID': user['uid'],
 | 
					            last_access = user['access'] or 0
 | 
				
			||||||
        'Nazwa użytkownika': user['name'],
 | 
					            days_inactive = (now_ts - last_access) / 86400 if last_access else float('inf')
 | 
				
			||||||
        'E-mail': user['mail'],
 | 
					            
 | 
				
			||||||
        'Status konta': 'Aktywny' if user['status'] == 1 else 'Zablokowany',
 | 
					            # Mapowanie danych
 | 
				
			||||||
        'Data rejestracji': datetime.fromtimestamp(user['created']).strftime('%Y-%m-%d %H:%M:%S') if user.get('created') else 'Nieznana',
 | 
					            user_data = {
 | 
				
			||||||
        'Ostatnie logowanie': datetime.fromtimestamp(user['access']).strftime('%Y-%m-%d %H:%M:%S') if user.get('access') else 'Nigdy',
 | 
					                "UID": user['uid'],
 | 
				
			||||||
        'Ostatnia aktywność': datetime.fromtimestamp(user['login']).strftime('%Y-%m-%d %H:%M:%S') if user.get('login') else 'Nigdy',
 | 
					                "Nazwa użytkownika": user['name'],
 | 
				
			||||||
        'Dni nieaktywności': f"{days_inactive:.1f} dni",
 | 
					                "E-mail": user['mail'],
 | 
				
			||||||
        'Punkty': user.get('points', 0),
 | 
					                "Status konta": 'Aktywny' if user['status'] == 1 else 'Zablokowany',
 | 
				
			||||||
        'Liczba postów': user.get('post_count', 0),
 | 
					                "Data rejestracji": datetime.fromtimestamp(user['created']).strftime('%Y-%m-%d %H:%M:%S') if user.get('created') else 'Nieznana',
 | 
				
			||||||
        'E-mail poprawny': 'Tak' if email_valid else 'Nie',
 | 
					                "Ostatnie logowanie": datetime.fromtimestamp(user['access']).strftime('%Y-%m-%d %H:%M:%S') if user.get('access') else 'Nigdy',
 | 
				
			||||||
        'Tymczasowy e-mail': 'Tak' if is_temp_email(user['mail'], temp_domains_cache) else 'Nie',
 | 
					                "Ostatnia aktywność": datetime.fromtimestamp(user['login']).strftime('%Y-%m-%d %H:%M:%S') if user.get('login') else 'Nigdy',
 | 
				
			||||||
        'Podejrzana nazwa': 'Tak' if bad_name else 'Nie'
 | 
					                "Dni nieaktywności": f"{days_inactive:.1f} dni",
 | 
				
			||||||
    }
 | 
					                "Punkty": user.get('points', 0),
 | 
				
			||||||
 | 
					                "Liczba postów": user.get('post_count', 0),
 | 
				
			||||||
 | 
					                "E-mail poprawny": 'Tak' if email_valid 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'
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            row.append(user_data.get(header, ''))
 | 
				
			||||||
 | 
					        table_data.append(row)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Wyświetl wyniki w tabeli
 | 
					    # Wyświetl wyniki w tabeli
 | 
				
			||||||
    print("\n🔍 Szczegóły użytkownika:")
 | 
					    print("\n🔍 Porównanie użytkowników:")
 | 
				
			||||||
    print(tabulate([(k, v) for k, v in user_data.items()], tablefmt="fancy_grid"))
 | 
					    print(tabulate(table_data, headers=[""] + [user['name'] for user in users], tablefmt="fancy_grid"))
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    return user
 | 
					    return users
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def is_fake_email(email):
 | 
					def is_fake_email(email):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user