"""Generates fake user accounts.""" from io import BytesIO from time import sleep import requests from PIL import Image from faker import Faker from lumi2.usermodel import User import lumi2.ldap as ldap def get_random_avatar() -> Image.Image: """Returns a PIL JPEG Image of an AI-generated cat.""" url = "https://thiscatdoesnotexist.com/" response = requests.get(url) if response.status_code != 200: raise RuntimeError( f"Request to '{url}' failed with code {response.status_code}." ) return Image.open(BytesIO(response.content)) def generate_random_user() -> User: """Generates a randomized user object and returns it.""" faker = Faker() return User( faker.user_name(), User.generate_password_hash(faker.password()), faker.email(), faker.first_name(), faker.last_name(), picture=get_random_avatar(), ) def populate_users(num_of_users: int = 10) -> None: """Populates the DIT with the specified number of randomly generated users.""" conn = ldap.get_connection() for i in range(num_of_users): print(f"Creating a random user... ({i+1}/{num_of_users})") user = generate_random_user() ldap.create_user(conn, user) user._generate_static_images() # Delay to give 'thispersondoesnotexist.com' time to generate a new image if i < num_of_users - 1: sleep(1) conn.unbind()