2022-11-29 13:22:22 +00:00
|
|
|
"""Generates fake user accounts."""
|
|
|
|
|
|
|
|
from io import BytesIO
|
2022-11-29 23:20:52 +00:00
|
|
|
from time import sleep
|
2022-11-29 13:22:22 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from PIL import Image
|
|
|
|
from faker import Faker
|
|
|
|
|
|
|
|
from lumi2.usermodel import User
|
2022-11-29 23:20:52 +00:00
|
|
|
import lumi2.ldap as ldap
|
2022-11-29 13:22:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_random_avatar() -> Image.Image:
|
2022-12-03 20:39:32 +00:00
|
|
|
"""Returns a PIL JPEG Image of an AI-generated cat."""
|
2022-11-29 13:22:22 +00:00
|
|
|
|
2022-12-03 20:39:32 +00:00
|
|
|
url = "https://thiscatdoesnotexist.com/"
|
2022-11-29 13:22:22 +00:00
|
|
|
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(),
|
|
|
|
)
|
2022-11-29 23:20:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|