|
| 1 | +# Generate users.csv with 100 users |
| 2 | +# Format: |
| 3 | +# Name,Email,Password,Role |
| 4 | +import csv |
| 5 | +from random import randint |
| 6 | +import os |
| 7 | + |
| 8 | + |
| 9 | +def generate_user_password(): |
| 10 | + # A through Z, 0 through 9 excluding I, 1 and 0, O |
| 11 | + allowed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 12 | + allowed_chars = allowed_chars.replace("I", "").replace("O", "").replace("0", "").replace("1", "") |
| 13 | + |
| 14 | + # Generate a random 6-character password |
| 15 | + password = ''.join(allowed_chars[randint(0, len(allowed_chars) - 1)] for _ in range(6)) |
| 16 | + return password |
| 17 | + |
| 18 | +def main(): |
| 19 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 20 | + |
| 21 | + file_path = os.path.join(script_dir, "users.csv") |
| 22 | + file_path_2 = os.path.join(script_dir, "users_export.csv") |
| 23 | + |
| 24 | + with open(file_path, mode="w", newline="") as file: |
| 25 | + writer = csv.writer(file) |
| 26 | + writer.writerow(["Name", "Email", "Password", "Role"]) # Header |
| 27 | + users = [] |
| 28 | + for _ in range(100): |
| 29 | + while True: |
| 30 | + name = "ars" + str(randint(1000, 9999)) |
| 31 | + if name not in users: |
| 32 | + break |
| 33 | + users.append(name) |
| 34 | + email = name.lower() + "@linz.coderdojo.net" |
| 35 | + password = generate_user_password() |
| 36 | + role = "user" |
| 37 | + writer.writerow([name, email, password, role]) |
| 38 | + |
| 39 | + with open(file_path_2, mode="w", newline="") as file: |
| 40 | + writer = csv.writer(file) |
| 41 | + writer.writerow(["Email", "Password"]) |
| 42 | + with open(file_path, mode="r", newline="") as file: |
| 43 | + reader = csv.reader(file) |
| 44 | + next(reader) # Skip header |
| 45 | + for row in reader: |
| 46 | + # Get name and password |
| 47 | + email = row[1] |
| 48 | + password = row[2] |
| 49 | + writer.writerow([email, password]) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments