From dd610a14c0a9c2ea3c49fdcc99f37459a28e2cb6 Mon Sep 17 00:00:00 2001 From: tyboro2002 Date: Tue, 9 Apr 2024 15:32:07 +0200 Subject: [PATCH 1/2] chore: hotfix --- .../api/management/commands/makeTeacher.py | 21 --- backend/api/management/commands/make_admin.py | 3 +- backend/api/management/commands/seed_db.py | 162 +++++++++++++----- frontend/src/store/authentication.store.ts | 2 +- 4 files changed, 122 insertions(+), 66 deletions(-) delete mode 100644 backend/api/management/commands/makeTeacher.py diff --git a/backend/api/management/commands/makeTeacher.py b/backend/api/management/commands/makeTeacher.py deleted file mode 100644 index 378675bc..00000000 --- a/backend/api/management/commands/makeTeacher.py +++ /dev/null @@ -1,21 +0,0 @@ -from django.core.management.base import BaseCommand -from authentication.models import User -from api.models.teacher import Teacher - - -class Command(BaseCommand): - - help = 'make yourself teacher' - - def add_arguments(self, parser): - parser.add_argument('username', type=str, help='The username of the student user to make teacher') - - def handle(self, *args, **options): - username = options['username'] - user = User.objects.filter(username=username) - if user.count() == 0: - self.stdout.write(self.style.ERROR('User not found, first log in !')) - return - user = user.get() - Teacher(user_ptr=user).save_base(raw=True) - self.stdout.write(self.style.SUCCESS('Successfully made the user teacher!')) diff --git a/backend/api/management/commands/make_admin.py b/backend/api/management/commands/make_admin.py index c99b7053..33d1702e 100644 --- a/backend/api/management/commands/make_admin.py +++ b/backend/api/management/commands/make_admin.py @@ -16,6 +16,5 @@ def handle(self, *args, **options): self.stdout.write(self.style.ERROR('User not found, first log in !')) return student = student.get() - student.is_staff = True - student.save() + student.make_admin() self.stdout.write(self.style.SUCCESS('Successfully made the user admin!')) diff --git a/backend/api/management/commands/seed_db.py b/backend/api/management/commands/seed_db.py index c1261534..58425bd3 100644 --- a/backend/api/management/commands/seed_db.py +++ b/backend/api/management/commands/seed_db.py @@ -4,6 +4,7 @@ from django.db.models import Max from faker.providers import BaseProvider, DynamicProvider import random +import time from authentication.models import Faculty from api.models.student import Student @@ -105,8 +106,7 @@ def provide_teacher(self, errHandler, min_faculty=1, max_faculty=2, staf_prob=0. ) if faculty is not None: - for fac in faculty: - teacher.faculties.add(fac) + teacher.faculties.add(*faculty) # Add faculties in bulk return teacher except Exception: @@ -140,8 +140,7 @@ def provide_assistant(self, errHandler, min_faculty=1, max_faculty=3, staf_prob= ) if faculty is not None: - for fac in faculty: - assistant.faculties.add(fac) + assistant.faculties.add(*faculty) # Add faculties in bulk return assistant except Exception: @@ -176,8 +175,7 @@ def provide_student(self, errHandler, min_faculty=1, max_faculty=3, staf_prob=0. ) if faculty is not None: - for fac in faculty: - student.faculties.add(fac) + student.faculties.add(*faculty) # Add faculties in bulk return student except Exception: @@ -214,28 +212,34 @@ def provide_course( # add students student_count = fake.random_int(min=min_students, max=max_students) - while course.students.count() < student_count: + students_list = [] + while len(students_list) < student_count: student = fake.student_provider() - if student not in course.students.all(): - course.students.add(student) + if student not in students_list: + students_list.append(student) + course.students.add(*students_list) # Add students in bulk # add teachers teacher_count = fake.random_int(min=min_teachers, max=max_teachers) - while course.teachers.count() < teacher_count: + teachers_list = [] + while len(teachers_list) < teacher_count: teacher = fake.teacher_provider() - if teacher not in course.teachers.all(): - course.teachers.add(teacher) + if teacher not in teachers_list: + teachers_list.append(teacher) + course.teachers.add(*teachers_list) # Add teachers in bulk # add assistants assistant_count = fake.random_int(min=min_assistants, max=max_assistants) - while course.assistants.count() < assistant_count: + assistants_list = [] + while len(assistants_list) < assistant_count: assistant = fake.assistant_provider() - if assistant not in course.assistants.all(): - course.assistants.add(assistant) + if assistant not in assistants_list: + assistants_list.append(assistant) + course.assistants.add(*assistants_list) # Add assistants in bulk - # print(course_name) return course - except Exception: + except Exception as e: + print(e) tries += 1 errHandler.stdout.write(errHandler.style.WARNING("Exceeded maximum number of attempts to generate a unique Course.")) @@ -307,10 +311,12 @@ def provide_group(self, errHandler, min_score=0): elif len(students_not_in_group) < max_group_size: group.students.extend(students_not_in_group) else: + choosen_students = [] for _ in range(0, max_group_size): random_student = students_not_in_group[fake.random_int(min=0, max=len(students_not_in_group))] - group.students.add(random_student) + choosen_students.append(random_student) students_not_in_group.remove(random_student) + group.students.add(*choosen_students) # bulk add the students return group except Exception: @@ -386,10 +392,9 @@ def provide_structure_check(self, errHandler, min_extensions=1, max_extensions=5 if extension not in blocked_extensions and extension not in obligated_extensions: blocked_extensions.append(extension) - for ext in obligated_extensions: - check.obligated_extensions.add(ext) - for ext in blocked_extensions: - check.blocked_extensions.add(ext) + check.obligated_extensions.add(*obligated_extensions) + check.blocked_extensions.add(*blocked_extensions) + return check except Exception: tries += 1 @@ -410,6 +415,46 @@ def update_providers(): structureCheck_provider.elements = StructureCheck.objects.all() +def update_Faculty_providers(): + faculty_provider.elements = Faculty.objects.all() + + +def update_Student_providers(): + student_provider.elements = Student.objects.all() + + +def update_Assistant_providers(): + assistant_provider.elements = Assistant.objects.all() + + +def update_Teacher_providers(): + teacher_provider.elements = Teacher.objects.all() + + +def update_Course_providers(): + course_provider.elements = Course.objects.all() + + +def update_Project_providers(): + project_provider.elements = Project.objects.all() + + +def update_Group_providers(): + group_provider.elements = Group.objects.all() + + +def update_Submission_providers(): + Submission_provider.elements = Submission.objects.all() + + +def update_FileExtension_providers(): + fileExtension_provider.elements = FileExtension.objects.all() + + +def update_StructureCheck_providers(): + structureCheck_provider.elements = StructureCheck.objects.all() + + # add new providers to faker instance fake.add_provider(Providers) fake.add_provider(faculty_provider) @@ -424,34 +469,67 @@ def update_providers(): fake.add_provider(structureCheck_provider) +def format_time(execution_time): + if execution_time < 1: + return f"{execution_time * 1000:.2f} milliseconds" + elif execution_time < 60: + return f"{execution_time:.2f} seconds" + elif execution_time < 3600: + return f"{execution_time / 60:.2f} minutes" + else: + return f"{execution_time / 3600:.2f} hours" + + class Command(BaseCommand): help = 'seed the db with data' - def seed_data(self, amount, provider_function): + def seed_data(self, amount, provider_function, update_function): for _ in range(amount): provider_function(self) - update_providers() + update_function() def handle(self, *args, **options): + start_time = time.time() # TODO maybey take as option - amount_of_students = 10_000 - amount_of_assistants = 1_000 - amount_of_teachers = 1_000 + amount_of_students = 50_000 + amount_of_assistants = 300 + amount_of_teachers = 500 amount_of_courses = 1_000 - amount_of_projects = 5_000 - amount_of_groups = 20_000 + amount_of_projects = 3_000 + amount_of_groups = 9_000 amount_of_submissions = 50_000 amount_of_file_extensions = 20 - amount_of_structure_checks = 10_000 - - 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!')) + amount_of_structure_checks = 12_000 + + # amount_of_students = 0 + # amount_of_assistants = 0 + # amount_of_teachers = 0 + # amount_of_courses = 1 + # 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, update_Student_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded students!')) + self.seed_data(amount_of_assistants, fake.provide_assistant, update_Assistant_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded assistants!')) + self.seed_data(amount_of_teachers, fake.provide_teacher, update_Teacher_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded teachers!')) + self.seed_data(amount_of_courses, fake.provide_course, update_Course_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded courses!')) + self.seed_data(amount_of_projects, fake.provide_project, update_Project_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded projects!')) + self.seed_data(amount_of_groups, fake.provide_group, update_Group_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded groups!')) + self.seed_data(amount_of_submissions, fake.provide_submission, update_Submission_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded submissions!')) + self.seed_data(amount_of_file_extensions, fake.provide_fileExtension, update_FileExtension_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded fileExtensions!')) + self.seed_data(amount_of_structure_checks, fake.provide_structure_check, update_StructureCheck_providers) + self.stdout.write(self.style.SUCCESS('Successfully seeded structure_checks!')) + + end_time = time.time() + execution_time = end_time - start_time + self.stdout.write(self.style.SUCCESS(f"Successfully seeded db in {format_time(execution_time)}!")) diff --git a/frontend/src/store/authentication.store.ts b/frontend/src/store/authentication.store.ts index e6bf4ef3..825d11fa 100644 --- a/frontend/src/store/authentication.store.ts +++ b/frontend/src/store/authentication.store.ts @@ -84,7 +84,7 @@ export const useAuthStore = defineStore('auth', () => { user.value = User.fromJSON(response.data as User); } - if (view.value === null) { + if (view.value === null || !user.value.hasRoles(view.value)) { view.value = user.value.roles[0]; } From ebecd8a6e234bcf3c4e6a27bbd31ff8745e001ad Mon Sep 17 00:00:00 2001 From: tyboro2002 Date: Tue, 9 Apr 2024 17:49:45 +0200 Subject: [PATCH 2/2] chore: commit changes --- backend/api/management/commands/make_admin.py | 10 ++--- backend/api/management/commands/seed_db.py | 38 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/backend/api/management/commands/make_admin.py b/backend/api/management/commands/make_admin.py index 33d1702e..5a590e01 100644 --- a/backend/api/management/commands/make_admin.py +++ b/backend/api/management/commands/make_admin.py @@ -1,5 +1,5 @@ from django.core.management.base import BaseCommand -from api.models.student import Student +from authentication.models import User class Command(BaseCommand): @@ -11,10 +11,10 @@ def add_arguments(self, parser): def handle(self, *args, **options): username = options['username'] - student = Student.objects.filter(username=username) - if student.count() == 0: + user = User.objects.filter(username=username) + if user.count() == 0: self.stdout.write(self.style.ERROR('User not found, first log in !')) return - student = student.get() - student.make_admin() + user = user.get() + user.make_admin() self.stdout.write(self.style.SUCCESS('Successfully made the user admin!')) diff --git a/backend/api/management/commands/seed_db.py b/backend/api/management/commands/seed_db.py index 58425bd3..923fc321 100644 --- a/backend/api/management/commands/seed_db.py +++ b/backend/api/management/commands/seed_db.py @@ -491,25 +491,25 @@ def seed_data(self, amount, provider_function, update_function): def handle(self, *args, **options): start_time = time.time() # TODO maybey take as option - amount_of_students = 50_000 - amount_of_assistants = 300 - amount_of_teachers = 500 - amount_of_courses = 1_000 - amount_of_projects = 3_000 - amount_of_groups = 9_000 - amount_of_submissions = 50_000 - amount_of_file_extensions = 20 - amount_of_structure_checks = 12_000 - - # amount_of_students = 0 - # amount_of_assistants = 0 - # amount_of_teachers = 0 - # amount_of_courses = 1 - # amount_of_projects = 0 - # amount_of_groups = 0 - # amount_of_submissions = 0 - # amount_of_file_extensions = 0 - # amount_of_structure_checks = 0 + # amount_of_students = 50_000 + # amount_of_assistants = 300 + # amount_of_teachers = 500 + # amount_of_courses = 1_000 + # amount_of_projects = 3_000 + # amount_of_groups = 9_000 + # amount_of_submissions = 50_000 + # amount_of_file_extensions = 20 + # amount_of_structure_checks = 12_000 + + amount_of_students = 0 + amount_of_assistants = 0 + amount_of_teachers = 0 + amount_of_courses = 0 + amount_of_projects = 30_000 + 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, update_Student_providers) self.stdout.write(self.style.SUCCESS('Successfully seeded students!'))