-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed_db_employee.py
64 lines (50 loc) · 1.7 KB
/
seed_db_employee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import django
from django_seed import Seed
from django_seed.seeder import Seeder
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE", "EmployeeManagementDjangoProject.settings"
)
django.setup()
from staff_management.models import Employee, Position
def seed_users(amount: int) -> None:
seeder = Seed.seeder()
amount -= Employee.objects.count()
seeder.add_entity(
Employee,
amount,
{
"email": lambda x: unique_email(seeder),
"full_name": lambda
x: f"{seeder.faker.first_name()} {seeder.faker.last_name()}",
"hired": lambda x: seeder.faker.date_this_decade(
before_today=True, after_today=False
),
"username": lambda x: unique_username(seeder),
"first_name": "",
"last_name": "",
"is_superuser": 0,
"is_staff": 0,
"is_active": 0,
"supervisor_id": "",
},
)
inserted_pks: dict = seeder.execute()
for pk in inserted_pks[Employee]:
user = Employee.objects.get(pk=pk)
user.set_password(f"seed_password{pk}")
user.save()
print(f"{amount} users has been created.")
def unique_email(seeder: Seeder) -> str:
email = seeder.faker.email()
while Employee.objects.filter(email=email).exists():
email = seeder.faker.email()
return email
def unique_username(seeder: Seeder) -> str:
username = seeder.faker.user_name()
while Employee.objects.filter(username=username).exists():
username = seeder.faker.user_name()
return username
if __name__ == "__main__":
seed_users(
50_005) # set the end goal amount even if the script was run before