diff --git a/backend/api/management/commands/seedDb.py b/backend/api/management/commands/seedDb.py index b52ae71d..ca35547c 100644 --- a/backend/api/management/commands/seedDb.py +++ b/backend/api/management/commands/seedDb.py @@ -14,60 +14,60 @@ from api.models.project import Project from api.models.submission import Submission from api.models.checks import FileExtension, StructureCheck - fake = Faker() # Faker.seed(4321) # set to make data same each time faculty_provider = DynamicProvider( - provider_name="faculty_provider", - elements=Faculty.objects.all(), - ) + provider_name="faculty_provider", + elements=Faculty.objects.all(), +) student_provider = DynamicProvider( - provider_name="student_provider", - elements=Student.objects.all(), - ) + provider_name="student_provider", + elements=Student.objects.all(), +) assistant_provider = DynamicProvider( - provider_name="assistant_provider", - elements=Assistant.objects.all(), - ) + provider_name="assistant_provider", + elements=Assistant.objects.all(), +) teacher_provider = DynamicProvider( - provider_name="teacher_provider", - elements=Teacher.objects.all(), - ) + provider_name="teacher_provider", + elements=Teacher.objects.all(), +) course_provider = DynamicProvider( - provider_name="course_provider", - elements=Course.objects.all(), - ) + provider_name="course_provider", + elements=Course.objects.all(), +) project_provider = DynamicProvider( - provider_name="project_provider", - elements=Project.objects.all(), - ) + provider_name="project_provider", + elements=Project.objects.all(), +) group_provider = DynamicProvider( - provider_name="group_provider", - elements=Group.objects.all(), - ) + provider_name="group_provider", + elements=Group.objects.all(), +) Submission_provider = DynamicProvider( - provider_name="Submission_provider", - elements=Submission.objects.all(), - ) + provider_name="Submission_provider", + elements=Submission.objects.all(), +) fileExtension_provider = DynamicProvider( - provider_name="fileExtension_provider", - elements=FileExtension.objects.all(), - ) + provider_name="fileExtension_provider", + elements=FileExtension.objects.all(), +) structureCheck_provider = DynamicProvider( - provider_name="structureCheck_provider", - elements=StructureCheck.objects.all(), - ) + provider_name="structureCheck_provider", + elements=StructureCheck.objects.all(), +) + # create new provider class class Providers(BaseProvider): @@ -88,17 +88,19 @@ def provide_teacher(self, errHandler, min_faculty=1, max_faculty=2, staf_prob=0. try: first = fake.first_name() last = fake.last_name() - id = fake.unique.random_int(min=self.min_id, max = self.max_id) - faculty = [fake.faculty_provider().id for _ in range(0,fake.random_int(min=min_faculty, max = max_faculty))] # generate 1 or 2 facultys - username = f"{first}_{last}_{fake.random_int(min=self.min_salt, max = self.max_salt)}" + id = fake.unique.random_int(min=self.min_id, max=self.max_id) + faculty = [ + fake.faculty_provider().id for _ in range(0, fake.random_int(min=min_faculty, max=max_faculty)) + ] # generate 1 or 2 facultys + username = f"{first}_{last}_{fake.random_int(min=self.min_salt, max=self.max_salt)}" teacher = Teacher.objects.create( id=id, first_name=first, last_name=last, username=username, - email=username+"@example.com", + email=username + "@example.com", create_time=timezone.now(), - last_enrolled= timezone.now().year, + last_enrolled=timezone.now().year, is_staff=fake.boolean(chance_of_getting_true=staf_prob) ) @@ -106,14 +108,11 @@ def provide_teacher(self, errHandler, min_faculty=1, max_faculty=2, staf_prob=0. for fac in faculty: teacher.faculties.add(fac) - # if courses is not None: - # for cours in courses: - # teacher.courses.add(cours) - return teacher - except: + except Exception: tries += 1 - errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique teacher.")) + errHandler.stdout.write( + errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique teacher.")) def provide_assistant(self, errHandler, min_faculty=1, max_faculty=3, staf_prob=0.01): """ @@ -124,17 +123,19 @@ def provide_assistant(self, errHandler, min_faculty=1, max_faculty=3, staf_prob= try: first = fake.first_name() last = fake.last_name() - id = fake.unique.random_int(min=self.min_id, max = self.max_id) - faculty = [fake.faculty_provider().id for _ in range(0,fake.random_int(min=min_faculty, max = max_faculty))] # generate 1 or 2 or 3 facultys - username = f"{first}_{last}_{fake.random_int(min=self.min_salt, max = self.max_salt)}" + id = fake.unique.random_int(min=self.min_id, max=self.max_id) + faculty = [ + fake.faculty_provider().id for _ in range(0, fake.random_int(min=min_faculty, max=max_faculty)) + ] # generate 1 or 2 or 3 facultys + username = f"{first}_{last}_{fake.random_int(min=self.min_salt, max=self.max_salt)}" assistant = Assistant.objects.create( id=id, first_name=first, last_name=last, username=username, - email=username+"@example.com", + email=username + "@example.com", create_time=timezone.now(), - last_enrolled= timezone.now().year, + last_enrolled=timezone.now().year, is_staff=fake.boolean(chance_of_getting_true=staf_prob) ) @@ -142,14 +143,11 @@ def provide_assistant(self, errHandler, min_faculty=1, max_faculty=3, staf_prob= for fac in faculty: assistant.faculties.add(fac) - # if courses is not None: - # for cours in courses: - # assistant.courses.add(cours) - return assistant - except: + except Exception: tries += 1 - errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique assistant.")) + errHandler.stdout.write( + errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique assistant.")) def provide_student(self, errHandler, min_faculty=1, max_faculty=3, staf_prob=0.01): """ @@ -160,17 +158,19 @@ def provide_student(self, errHandler, min_faculty=1, max_faculty=3, staf_prob=0. try: first = fake.first_name() last = fake.last_name() - id = fake.unique.random_int(min=self.min_id, max = self.max_id) - faculty = [fake.faculty_provider().id for _ in range(0,fake.random_int(min=min_faculty, max = max_faculty))] # generate 1 or 2 or 3 facultys - username = f"{first}_{last}_{fake.random_int(min=self.min_salt, max = self.max_salt)}" + id = fake.unique.random_int(min=self.min_id, max=self.max_id) + faculty = [ + fake.faculty_provider().id for _ in range(0, fake.random_int(min=min_faculty, max=max_faculty)) + ] # generate 1 or 2 or 3 facultys + username = f"{first}_{last}_{fake.random_int(min=self.min_salt, max=self.max_salt)}" student = Student.objects.create( id=id, first_name=first, last_name=last, username=username, - email=username+"@example.com", + email=username + "@example.com", create_time=timezone.now(), - last_enrolled= timezone.now().year, + last_enrolled=timezone.now().year, student_id=id, is_staff=fake.boolean(chance_of_getting_true=staf_prob) ) @@ -179,14 +179,11 @@ def provide_student(self, errHandler, min_faculty=1, max_faculty=3, staf_prob=0. for fac in faculty: student.faculties.add(fac) - # if courses is not None: - # for cours in courses: - # student.courses.add(cours) - return student - except: + except Exception: tries += 1 - errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique student.")) + errHandler.stdout.write( + errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique student.")) def provide_course( self, @@ -198,40 +195,39 @@ def provide_course( min_teachers=1, max_teachers=5, min_assistants=0, - max_assistants=5 - ): + max_assistants=5): """ Create a Course with the given arguments. """ tries = 0 while tries < self.MAX_TRIES: try: - parent_course = None # TODO make this sometimes a course + parent_course = None # TODO make this sometimes a course course_name = fake.catch_phrase() course: Course = Course.objects.create( name=course_name, - academic_startyear=timezone.now().year - fake.random_int(min=min_year_passed, max = max_year_passed) , + academic_startyear=timezone.now().year - fake.random_int(min=min_year_passed, max=max_year_passed), faculty=fake.faculty_provider(), description=fake.paragraph(), parent_course=parent_course ) # add students - student_count = fake.random_int(min=min_students, max = max_students) + student_count = fake.random_int(min=min_students, max=max_students) while course.students.count() < student_count: student = fake.student_provider() if student not in course.students.all(): course.students.add(student) # add teachers - teacher_count = fake.random_int(min=min_teachers, max = max_teachers) + teacher_count = fake.random_int(min=min_teachers, max=max_teachers) while course.teachers.count() < teacher_count: teacher = fake.teacher_provider() if teacher not in course.teachers.all(): course.teachers.add(teacher) # add assistants - assistant_count = fake.random_int(min=min_assistants, max = max_assistants) + assistant_count = fake.random_int(min=min_assistants, max=max_assistants) while course.assistants.count() < assistant_count: assistant = fake.assistant_provider() if assistant not in course.assistants.all(): @@ -239,32 +235,32 @@ def provide_course( # print(course_name) return course - except: + except Exception: tries += 1 errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique Course.")) def provide_project( self, errHandler, - min_start_date_dev = -100, - max_start_date_dev = 100, - min_deadline_dev = 1, - max_deadline_dev = 100, - visible_prob = 80, - archived_prob = 10, - score_visible_prob = 30, - locked_groups_prob = 30, - min_max_score = 1, - max_max_score = 100, - min_group_size = 1, - max_group_size = 15 - ): + min_start_date_dev=-100, + max_start_date_dev=100, + min_deadline_dev=1, + max_deadline_dev=100, + visible_prob=80, + archived_prob=10, + score_visible_prob=30, + locked_groups_prob=30, + min_max_score=1, + max_max_score=100, + min_group_size=1, + max_group_size=15): """Create a Project with the given arguments.""" tries = 0 while tries < self.MAX_TRIES: try: - start_date = timezone.now() + timezone.timedelta(days=fake.random_int(min=min_start_date_dev, max = max_start_date_dev)) - deadline = start_date + timezone.timedelta(days=fake.random_int(min=min_deadline_dev, max = max_deadline_dev)) + start_date = timezone.now() + timezone.timedelta( + days=fake.random_int(min=min_start_date_dev, max=max_start_date_dev)) + deadline = start_date + timezone.timedelta(days=fake.random_int(min=min_deadline_dev, max=max_deadline_dev)) course = fake.course_provider() return Project.objects.create( name=fake.catch_phrase(), @@ -276,12 +272,13 @@ def provide_project( deadline=deadline, course=course, start_date=start_date, - max_score=fake.random_int(min=min_max_score, max = max_max_score), - group_size=fake.random_int(min=min_group_size, max = max_group_size) + max_score=fake.random_int(min=min_max_score, max=max_max_score), + group_size=fake.random_int(min=min_group_size, max=max_group_size) ) - except: + except Exception: tries += 1 - errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique project.")) + errHandler.stdout.write( + errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique project.")) def provide_group(self, errHandler, min_score=0): """Create a Group with the given arguments.""" @@ -289,14 +286,13 @@ def provide_group(self, errHandler, min_score=0): while tries < self.MAX_TRIES: try: project: Project = fake.project_provider() - group:Group = Group.objects.create( + group: Group = Group.objects.create( project=project, - score=fake.random_int(min=min_score, max = project.max_score) + score=fake.random_int(min=min_score, max=project.max_score) ) max_group_size = group.project.group_size - students = group.project.course.students.all() groups = group.project.groups.all() joined_students = [] @@ -312,12 +308,12 @@ def provide_group(self, errHandler, min_score=0): group.students.extend(students_not_in_group) else: for _ in range(0, max_group_size): - random_student = students_not_in_group[fake.random_int(min=0,max=len(students_not_in_group))] + random_student = students_not_in_group[fake.random_int(min=0, max=len(students_not_in_group))] group.students.add(random_student) students_not_in_group.remove(random_student) return group - except: + except Exception: tries += 1 errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique group.")) @@ -326,7 +322,7 @@ def provide_submission(self, errHandler, struct_check_passed_prob=70): tries = 0 while tries < self.MAX_TRIES: try: - group:Group = fake.group_provider() + group: Group = fake.group_provider() # Generate a random timestamp between start and end timestamps random_timestamp = random.uniform(group.project.start_date.timestamp(), group.project.deadline.timestamp()) @@ -339,15 +335,16 @@ def provide_submission(self, errHandler, struct_check_passed_prob=70): ).aggregate(Max('submission_number'))['submission_number__max'] or 0 # print(fake.zip(uncompressed_size=10, num_files=5, min_file_size=1)) - return Submission.objects.create( # TODO add fake files + return Submission.objects.create( # TODO add fake files group=group, submission_time=random_datetime, structure_checks_passed=fake.boolean(chance_of_getting_true=struct_check_passed_prob), submission_number=max_submission_number + 1 ) - except: + except Exception: tries += 1 - errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique submission.")) + errHandler.stdout.write( + errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique submission.")) def provide_fileExtension(self, errHandler): """ @@ -358,11 +355,12 @@ def provide_fileExtension(self, errHandler): try: ext = fake.file_extension() return FileExtension.objects.create(extension=ext) - except: + except Exception: tries += 1 - errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique file extension.")) + errHandler.stdout.write( + errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique file extension.")) - def provide_structure_check(self, errHandler, min_extensions=1, max_extensions = 5, min_path_depth=1, max_path_depth=10): + def provide_structure_check(self, errHandler, min_extensions=1, max_extensions=5, min_path_depth=1, max_path_depth=10): """ Create a StructureCheck with the given arguments. """ @@ -387,17 +385,17 @@ def provide_structure_check(self, errHandler, min_extensions=1, max_extensions = extension = fake.fileExtension_provider() if extension not in blocked_extensions and extension not in obligated_extensions: blocked_extensions.append(extension) - # print([x.extension for x in obligated_extensions]) - # print([x.extension for x in blocked_extensions]) for ext in obligated_extensions: check.obligated_extensions.add(ext) for ext in blocked_extensions: check.blocked_extensions.add(ext) return check - except: + except Exception: tries += 1 - errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique structure check.")) + errHandler.stdout.write( + errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique structure check.")) + def update_providers(): faculty_provider.elements = Faculty.objects.all() @@ -412,7 +410,7 @@ def update_providers(): structureCheck_provider.elements = StructureCheck.objects.all() - # then add new provider to faker instance +# add new providers to faker instance fake.add_provider(Providers) fake.add_provider(faculty_provider) fake.add_provider(student_provider) @@ -425,75 +423,45 @@ def update_providers(): fake.add_provider(fileExtension_provider) fake.add_provider(structureCheck_provider) + class Command(BaseCommand): help = 'seed the db with data' - def handle(self, *args, **options): - - #TODO maybey take as option - amountOfStudents = 10_000 - amountOfAssistants = 1_000 - amountOfTeachers = 1_000 - amountOfCourses = 1_000 - amountOfProjects = 5_000 - amountOfGroups = 20_000 - amountOfSubmissions = 50_000 - amountOfFileExtensions = 20 - amountOfStructureChecks = 10_000 - - # amountOfStudents = 0 - # amountOfAssistants = 0 - # amountOfTeachers = 0 - # amountOfCourses = 0 - # amountOfProjects = 0 - # amountOfGroups = 1 - # amountOfSubmissions = 0 - # amountOfFileExtensions = 0 - # amountOfStructureChecks = 0 - - for _ in range(0,amountOfStudents): - fake.provide_student(self) - - if amountOfStudents: update_providers() - - for _ in range(0,amountOfAssistants): - fake.provide_assistant(self) - - if amountOfAssistants: update_providers() + def seed_data(self, amount, provider_function): + for _ in range(amount): + provider_function(self) + update_providers() - for _ in range(0,amountOfTeachers): - fake.provide_teacher(self) - - if amountOfTeachers: update_providers() - - for _ in range(0,amountOfCourses): - fake.provide_course(self) - - if amountOfCourses: update_providers() - - for _ in range(0,amountOfProjects): - fake.provide_project(self) - - if amountOfProjects: update_providers() - - for _ in range(0,amountOfGroups): - fake.provide_group(self) - - if amountOfGroups: update_providers() - - for _ in range(0,amountOfSubmissions): - fake.provide_submission(self) - - if amountOfSubmissions: update_providers() - - for _ in range(0,amountOfFileExtensions): - fake.provide_fileExtension(self) - - if amountOfFileExtensions: update_providers() - - for _ in range(0,amountOfStructureChecks): - fake.provide_structure_check(self) - - if amountOfStructureChecks: update_providers() + def handle(self, *args, **options): + # TODO maybey take as option + # amount_of_students = 10_000 + # amount_of_assistants = 1_000 + # amount_of_teachers = 1_000 + # amount_of_courses = 1_000 + # amount_of_projects = 5_000 + # amount_of_groups = 20_000 + # amount_of_submissions = 50_000 + # amount_of_file_extensions = 20 + # amount_of_structure_checks = 10_000 + + amount_of_students = 1 + amount_of_assistants = 0 + amount_of_teachers = 0 + amount_of_courses = 0 + amount_of_projects = 0 + amount_of_groups = 0 + amount_of_submissions = 0 + amount_of_file_extensions = 0 + amount_of_structure_checks = 0 + + self.seed_data(amount_of_students, fake.provide_student) + self.seed_data(amount_of_assistants, fake.provide_assistant) + self.seed_data(amount_of_teachers, fake.provide_teacher) + self.seed_data(amount_of_courses, fake.provide_course) + self.seed_data(amount_of_projects, fake.provide_project) + self.seed_data(amount_of_groups, fake.provide_group) + self.seed_data(amount_of_submissions, fake.provide_submission) + self.seed_data(amount_of_file_extensions, fake.provide_fileExtension) + self.seed_data(amount_of_structure_checks, fake.provide_structure_check) self.stdout.write(self.style.SUCCESS('Successfully seeded db!'))