-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_db.py
58 lines (50 loc) · 1.7 KB
/
populate_db.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
import os
import django
from faker import Faker
import random
from tasks.models import Employee, Project, Tasks, TaskDetail
# Set up Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'task_management.settings')
django.setup()
# Function to populate the database
def populate_db():
# Initialize Faker
fake = Faker()
# Create Projects
projects = [Project.objects.create(
name=fake.bs().capitalize(),
description=fake.paragraph(),
start_date=fake.date_this_year()
) for _ in range(5)]
print(f"Created {len(projects)} projects.")
# Create Employees
employees = [Employee.objects.create(
name=fake.name(),
email=fake.email()
) for _ in range(10)]
print(f"Created {len(employees)} employees.")
# Create Tasks
tasks = []
for _ in range(20):
task = Tasks.objects.create(
project=random.choice(projects),
title=fake.sentence(),
description=fake.paragraph(),
due_date=fake.date_this_year(),
status=random.choice(['PENDING', 'IN_PROGRESS', 'COMPLETED']),
is_completed=random.choice([True, False])
)
task.assigned_to.set(random.sample(employees, random.randint(1, 3)))
tasks.append(task)
print(f"Created {len(tasks)} tasks.")
# Create Task Details
for task in tasks:
TaskDetail.objects.create(
task=task,
assigned_to=", ".join(
[emp.name for emp in task.assigned_to.all()]),
priority=random.choice(['H', 'M', 'L']),
notes=fake.paragraph()
)
print("Populated TaskDetails for all tasks.")
print("Database populated successfully!")