Skip to content

Commit

Permalink
chore: native SQL seeder queries
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutV committed Apr 10, 2024
1 parent 6e35e46 commit 6b4a12c
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 27 deletions.
65 changes: 41 additions & 24 deletions backend/api/management/commands/seed_db_new.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from random import choices, randint
from django.core.management.base import BaseCommand
from api.seeders.faker import faker
from authentication.models import User
from api.seeders.faker import faker
from api.seeders.seeder import seed_students, seed_assistants, seed_teachers, seed_courses, seed_projects, seed_groups, \
seed_submissions
from api.models.course import Course
from api.models.group import Group
from api.models.project import Project
from api.models.teacher import Teacher
from api.models.student import Student
from api.models.assistant import Assistant
from api.models.submission import Submission


def _seed_users(num: int = 500):
Expand Down Expand Up @@ -79,28 +82,42 @@ def _seed_groups(num: int = 3_000):
class Command(BaseCommand):
help = 'seed the db with data'

amount_of_users = 10_000
amount_of_courses = 1_000
amount_of_projects = 1_000
amount_of_groups = 5_000
amount_of_students = 50_000
amount_of_assistants = 5_000
amount_of_teachers = 1_500
amount_of_courses = 1_500
amount_of_projects = 3_000
amount_of_groups = 3_000
amount_of_submissions = 3_000

def handle(self, *args, **options):
# Users (Students, Teachers, Assistants)
self.stdout.write('Seeding users...')
_seed_users(self.amount_of_users)
self.stdout.write(self.style.SUCCESS('Seeded users'))

# Courses
self.stdout.write('Seeding courses...')
_seed_courses(self.amount_of_courses)
self.stdout.write(self.style.SUCCESS('Seeded courses'))

# Projects
self.stdout.write('Seeding projects...')
_seed_projects(self.amount_of_projects)
self.stdout.write(self.style.SUCCESS('Seeded projects'))

# Groups
self.stdout.write('Seeding groups...')
_seed_groups(self.amount_of_groups)
self.stdout.write(self.style.SUCCESS('Seeded groups'))
# Reset DB
User.objects.all().delete()
Student.objects.all().delete()
Assistant.objects.all().delete()
Teacher.objects.all().delete()
Course.objects.all().delete()
Project.objects.all().delete()
Submission.objects.all().delete()

# Seed students
fake = faker()
seed_students(fake, self.amount_of_students, 0)

# Seed assistants
seed_assistants(fake, self.amount_of_assistants, self.amount_of_students)

# Seed teachers
seed_teachers(fake, self.amount_of_teachers, self.amount_of_students + self.amount_of_assistants)

# Seed courses
seed_courses(faker(), self.amount_of_courses)

# Seed projects
seed_projects(faker(), self.amount_of_projects)

# Seed groups
seed_groups(faker(), self.amount_of_groups)

# Seed submissions
seed_submissions(faker(), self.amount_of_submissions)
4 changes: 2 additions & 2 deletions backend/api/seeders/providers/fake_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def fake_username(self, first_name: str, last_name: str) -> str:
rand = self.random_int(min=self.min_salt, max=self.max_salt)
return f"{first_name.lower()}{last_name.lower()}{rand}"[:12]

def fake_user(self, fake, id: int) -> User:
def fake_user(self, fake, id: int, staff_probability: float = 0.001) -> User:
"""Create a fake user"""
return User(
id=id,
Expand All @@ -25,7 +25,7 @@ def fake_user(self, fake, id: int) -> User:
email=fake.unique.email(),
create_time=timezone.now(),
last_enrolled=timezone.now().year,
is_staff=fake.boolean(chance_of_getting_true=0.001)
is_staff=fake.boolean(chance_of_getting_true=staff_probability)
)

def fake_course(self, fake, min_year=2022, max_year=2025) -> Course:
Expand Down
Loading

0 comments on commit 6b4a12c

Please sign in to comment.