nowy mail i funkcja
This commit is contained in:
		
							
								
								
									
										70
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								app.py
									
									
									
									
									
								
							@@ -165,7 +165,7 @@ def get_smtp_config():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    return config
 | 
					    return config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def send_email_batch(users, smtp_config, mails_per_pack=100, time_per_pack=60, dry_run=False, inactive_range=None):
 | 
					def send_email_batch(users, smtp_config, mails_per_pack=100, time_per_pack=60, dry_run=False):
 | 
				
			||||||
    import os
 | 
					    import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    template_path = "mail_template.html"
 | 
					    template_path = "mail_template.html"
 | 
				
			||||||
@@ -180,18 +180,6 @@ def send_email_batch(users, smtp_config, mails_per_pack=100, time_per_pack=60, d
 | 
				
			|||||||
        print(f"❌ Błąd podczas odczytu szablonu HTML: {e}")
 | 
					        print(f"❌ Błąd podczas odczytu szablonu HTML: {e}")
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if inactive_range:
 | 
					 | 
				
			||||||
        min_days, max_days = inactive_range
 | 
					 | 
				
			||||||
        now_ts = int(datetime.now().timestamp())
 | 
					 | 
				
			||||||
        filtered_users = []
 | 
					 | 
				
			||||||
        for user in users:
 | 
					 | 
				
			||||||
            last_access = user['access'] or 0
 | 
					 | 
				
			||||||
            days_inactive = (now_ts - last_access) / 86400
 | 
					 | 
				
			||||||
            if min_days <= days_inactive <= max_days:
 | 
					 | 
				
			||||||
                filtered_users.append(user)
 | 
					 | 
				
			||||||
        users = filtered_users
 | 
					 | 
				
			||||||
        print(f"📨 Wysyłka maili tylko dla użytkowników nieaktywnych od {min_days} do {max_days} dni ({len(users)} osób)")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        smtp = smtplib.SMTP(smtp_config["host"], int(smtp_config["port"]))
 | 
					        smtp = smtplib.SMTP(smtp_config["host"], int(smtp_config["port"]))
 | 
				
			||||||
        smtp.ehlo()
 | 
					        smtp.ehlo()
 | 
				
			||||||
@@ -340,7 +328,7 @@ def main():
 | 
				
			|||||||
        type=str,
 | 
					        type=str,
 | 
				
			||||||
        help="Zakres dni nieaktywności w formacie min-max, np. 360-1825 (tylko dla wysyłki maili)"
 | 
					        help="Zakres dni nieaktywności w formacie min-max, np. 360-1825 (tylko dla wysyłki maili)"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    args = parser.parse_args()
 | 
					    args = parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    inactive_range = None
 | 
					    inactive_range = None
 | 
				
			||||||
@@ -349,16 +337,15 @@ def main():
 | 
				
			|||||||
            min_days, max_days = map(int, args.inactive_since.split('-'))
 | 
					            min_days, max_days = map(int, args.inactive_since.split('-'))
 | 
				
			||||||
            inactive_range = (min_days, max_days)
 | 
					            inactive_range = (min_days, max_days)
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            print(f"Błąd parsowania zakresu dni w --inactive-since: {e}")
 | 
					            print(f"Błąd parsowania zakresu dni: {e}")
 | 
				
			||||||
            exit(1)
 | 
					            exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if args.send_test:
 | 
					    if args.send_test:
 | 
				
			||||||
        test_user = {
 | 
					        test_user = {
 | 
				
			||||||
            'name': 'Testowy Użytkownik',
 | 
					            'name': 'Testowy Użytkownik',
 | 
				
			||||||
            'mail': args.send_test,
 | 
					            'mail': args.send_test,
 | 
				
			||||||
            'created': int(datetime.now().timestamp()) - (86400 * 365 * 2)  # 2 lata temu
 | 
					            'created': int(datetime.now().timestamp()) - (86400 * 365 * 2)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					 | 
				
			||||||
        print(f"📬 Wysyłka testowego maila na: {test_user['mail']}")
 | 
					        print(f"📬 Wysyłka testowego maila na: {test_user['mail']}")
 | 
				
			||||||
        send_email_batch([test_user], smtp_config, mails_per_pack=1, time_per_pack=0, dry_run=False)
 | 
					        send_email_batch([test_user], smtp_config, mails_per_pack=1, time_per_pack=0, dry_run=False)
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
@@ -397,12 +384,19 @@ def main():
 | 
				
			|||||||
        if (user.get('post_count') or 0) > 0:
 | 
					        if (user.get('post_count') or 0) > 0:
 | 
				
			||||||
            continue
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        last_access = user['access'] or 0
 | 
				
			||||||
 | 
					        days_inactive = (now_ts - last_access) / 86400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Nowa logika oznaczania nieaktywnych
 | 
				
			||||||
 | 
					        is_inactive_by_days = (args.days_inactive is not None) and (days_inactive > args.days_inactive)
 | 
				
			||||||
 | 
					        is_inactive_by_range = inactive_range and (inactive_range[0] <= days_inactive <= inactive_range[1])
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        user['inactive'] = is_inactive_by_days or is_inactive_by_range
 | 
				
			||||||
        user['temp_email'] = is_temp_email(user['mail'])
 | 
					        user['temp_email'] = is_temp_email(user['mail'])
 | 
				
			||||||
        user['email_valid'] = not is_fake_email(user['mail']) and not user['temp_email']
 | 
					        user['email_valid'] = not is_fake_email(user['mail']) and not user['temp_email']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if args.only_invalid_emails:
 | 
					        if args.only_invalid_emails:
 | 
				
			||||||
            if not user['email_valid']:
 | 
					            if not user['email_valid']:
 | 
				
			||||||
                user['inactive'] = True
 | 
					 | 
				
			||||||
                final_candidates.append(user)
 | 
					                final_candidates.append(user)
 | 
				
			||||||
            continue
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -414,11 +408,6 @@ def main():
 | 
				
			|||||||
                skipped_veterans += 1
 | 
					                skipped_veterans += 1
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        last_access = user['access'] or 0
 | 
					 | 
				
			||||||
        user['inactive'] = (args.days_inactive is not None) and ((now_ts - last_access) > args.days_inactive * 86400)
 | 
					 | 
				
			||||||
        user['temp_email'] = is_temp_email(user['mail'])
 | 
					 | 
				
			||||||
        user['email_valid'] = not is_fake_email(user['mail']) and not user['temp_email']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if user['inactive']:
 | 
					        if user['inactive']:
 | 
				
			||||||
            inactive_count += 1
 | 
					            inactive_count += 1
 | 
				
			||||||
        if not user['email_valid']:
 | 
					        if not user['email_valid']:
 | 
				
			||||||
@@ -428,7 +417,7 @@ def main():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        if args.validate or user['inactive'] or not user['email_valid']:
 | 
					        if args.validate or user['inactive'] or not user['email_valid']:
 | 
				
			||||||
            final_candidates.append(user)
 | 
					            final_candidates.append(user)
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
    final_candidates = [u for u in final_candidates if (u.get('points') or 0) == 0]
 | 
					    final_candidates = [u for u in final_candidates if (u.get('points') or 0) == 0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if args.report_domains:
 | 
					    if args.report_domains:
 | 
				
			||||||
@@ -453,42 +442,37 @@ def main():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if args.send_mails:
 | 
					    if args.send_mails:
 | 
				
			||||||
        send_email_batch(
 | 
					        send_email_batch(
 | 
				
			||||||
            final_candidates, 
 | 
					            [u for u in final_candidates if u['inactive']],  # Tylko nieaktywni
 | 
				
			||||||
            smtp_config, 
 | 
					            smtp_config, 
 | 
				
			||||||
            args.mails_per_pack, 
 | 
					            args.mails_per_pack, 
 | 
				
			||||||
            args.time_per_pack,
 | 
					            args.time_per_pack
 | 
				
			||||||
            inactive_range=inactive_range
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print("\n📋 Parametry filtrowania:")
 | 
					    print("\n📋 Parametry filtrowania:")
 | 
				
			||||||
    if args.days_inactive:
 | 
					    if args.days_inactive:
 | 
				
			||||||
        print(f"- Nieaktywni: brak logowania przez ≥ {args.days_inactive} dni (~{days_to_years(args.days_inactive)} lat)")
 | 
					        print(f"- Nieaktywni: > {args.days_inactive} dni (~{days_to_years(args.days_inactive)} lat)")
 | 
				
			||||||
    print(f"- Weterani: konta zarejestrowane w roku ≤ {args.veteran_year}")
 | 
					    if inactive_range:
 | 
				
			||||||
    print(f"- Pominięci weterani: logowanie w ciągu ostatnich ≤ {args.recent_login_days} dni (~{days_to_years(args.recent_login_days)} lat)")
 | 
					        print(f"- Zakres nieaktywności: {inactive_range[0]}-{inactive_range[1]} dni (~{days_to_years(inactive_range[0])}-{days_to_years(inactive_range[1])} lat)")
 | 
				
			||||||
    if args.inactive_since:
 | 
					    print(f"- Weterani: konta przed {args.veteran_year}")
 | 
				
			||||||
        min_days, max_days = map(int, args.inactive_since.split('-'))
 | 
					    print(f"- Pominięci weterani: logowanie w ostatnich {args.recent_login_days} dniach")
 | 
				
			||||||
        print(f"- Wysyłka maili tylko dla nieaktywnych od {min_days} do {max_days} dni (~{days_to_years(min_days)}-{days_to_years(max_days)} lat)")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print("\n📊 Podsumowanie:")
 | 
					    print("\n📊 Podsumowanie:")
 | 
				
			||||||
    print(f"- Całkowita liczba użytkowników w bazie: {len(users)}")
 | 
					    print(f"- Całkowita liczba użytkowników: {len(users)}")
 | 
				
			||||||
    print(f"- Pominięci z punktami: {skipped_with_points}")
 | 
					    print(f"- Pominięci z punktami: {skipped_with_points}")
 | 
				
			||||||
    print(f"- Nieaktywni (> {args.days_inactive} dni): {inactive_count}")
 | 
					    print(f"- Nieaktywni: {inactive_count}")
 | 
				
			||||||
    print(f"- Z niepoprawnym e-mailem (MX lub tymczasowy): {invalid_email_count}")
 | 
					    print(f"- Z niepoprawnym e-mailem: {invalid_email_count}")
 | 
				
			||||||
    print(f"- Z tymczasowym e-mailem: {temp_email_count}")
 | 
					    print(f"- Z tymczasowym e-mailem: {temp_email_count}")
 | 
				
			||||||
    print(f"- Kandydaci do usunięcia: {len(final_candidates)}")
 | 
					    print(f"- Kandydaci do usunięcia: {len(final_candidates)}")
 | 
				
			||||||
    print(f"- Pominięci jako aktywni weterani: {skipped_veterans}")
 | 
					    print(f"- Pominięci weterani: {skipped_veterans}")
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if args.only_invalid_emails:
 | 
					 | 
				
			||||||
        print("- Tryb: tylko użytkownicy z nieprawidłowymi adresami e-mail")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if args.delete:
 | 
					    if args.delete:
 | 
				
			||||||
        confirm_delete()
 | 
					        confirm_delete()
 | 
				
			||||||
        if not args.drupal_path:
 | 
					        if not args.drupal_path:
 | 
				
			||||||
            print("❌ Brak parametru --drupal-path. Nie można usunąć użytkowników bez ścieżki do Drupala.")
 | 
					            print("❌ Brak ścieżki do Drupala!")
 | 
				
			||||||
            sys.exit(1)
 | 
					            sys.exit(1)
 | 
				
			||||||
        for u in tqdm(final_candidates, desc="Usuwanie użytkowników"):
 | 
					        for u in tqdm(final_candidates, desc="Usuwanie"):
 | 
				
			||||||
            delete_user_via_php(u['uid'], args.drupal_path)
 | 
					            delete_user_via_php(u['uid'], args.drupal_path)
 | 
				
			||||||
        print(f"✅ Usunięto {len(final_candidates)} użytkowników przez delete_user.php")
 | 
					        print(f"✅ Usunięto {len(final_candidates)} użytkowników")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    main()
 | 
					    main()
 | 
				
			||||||
		Reference in New Issue
	
	Block a user