From 6c32d16acb2ae27a25b4258926af2012745b2cda Mon Sep 17 00:00:00 2001 From: Adil Mohak Date: Sun, 13 Oct 2024 01:06:35 +0300 Subject: [PATCH] Update fake data generator --- scripts/generate_fake_accounts_data.py | 3 -- scripts/generate_fake_core_data.py | 12 +++--- scripts/generate_fake_data.py | 57 ++++++++++++++++++++++++-- templates/accounts/lecturer_list.html | 4 +- templates/accounts/student_list.html | 2 +- templates/course/program_list.html | 2 +- templates/course/program_single.html | 2 +- 7 files changed, 66 insertions(+), 16 deletions(-) diff --git a/scripts/generate_fake_accounts_data.py b/scripts/generate_fake_accounts_data.py index 8feb75d..b5488a5 100644 --- a/scripts/generate_fake_accounts_data.py +++ b/scripts/generate_fake_accounts_data.py @@ -167,6 +167,3 @@ def generate_fake_accounts_data( print(f"Created {len(programs)} programs.") print(f"Created {len(students)} students.") print(f"Created {len(parents)} parents.") - - -generate_fake_accounts_data(10, 10, 10) diff --git a/scripts/generate_fake_core_data.py b/scripts/generate_fake_core_data.py index c9e9f24..b76a368 100644 --- a/scripts/generate_fake_core_data.py +++ b/scripts/generate_fake_core_data.py @@ -5,7 +5,7 @@ from typing import List from django.utils import timezone from faker import Faker from factory.django import DjangoModelFactory -from factory import SubFactory, LazyAttribute, Iterator +from factory import SubFactory, LazyAttribute, Iterator, LazyFunction from core.models import ActivityLog, NewsAndEvents, Session, Semester, SEMESTER, POST # Set up Django environment @@ -32,9 +32,11 @@ class NewsAndEventsFactory(DjangoModelFactory): title: str = LazyAttribute(lambda x: fake.sentence(nb_words=4)) summary: str = LazyAttribute(lambda x: fake.paragraph(nb_sentences=3)) - posted_as: str = fake.random_element(elements=[choice[0] for choice in POST]) - updated_date: timezone.datetime = fake.date_time_this_year() - upload_time: timezone.datetime = fake.date_time_this_year() + posted_as: str = LazyFunction( + lambda: fake.random_element(elements=[choice[0] for choice in POST]) + ) + # updated_date: timezone.datetime = fake.date_time_this_year() + # upload_time: timezone.datetime = fake.date_time_this_year() class SessionFactory(DjangoModelFactory): @@ -50,7 +52,7 @@ class SessionFactory(DjangoModelFactory): class Meta: model = Session - session: str = LazyAttribute(lambda x: fake.sentence(nb_words=2)) + session: str = LazyAttribute(lambda x: str(fake.random_int(min=2020, max=2030))) is_current_session: bool = fake.boolean(chance_of_getting_true=50) next_session_begins = LazyAttribute(lambda x: fake.future_datetime()) diff --git a/scripts/generate_fake_data.py b/scripts/generate_fake_data.py index c963006..8a1aa95 100644 --- a/scripts/generate_fake_data.py +++ b/scripts/generate_fake_data.py @@ -1,3 +1,4 @@ +import random from typing import Type from factory.django import DjangoModelFactory from factory import SubFactory, LazyAttribute, Iterator @@ -152,6 +153,59 @@ class CourseOfferFactory(DjangoModelFactory): dep_head = SubFactory(DepartmentHeadFactory) +def populate_course_allocation(num_allocations: int) -> None: + """ + Populate the CourseAllocation model with fake data. + + Args: + num_allocations (int): The number of CourseAllocation instances to generate. + """ + # Fetch all available courses and sessions + courses = list(Course.objects.all()) + sessions = list(Session.objects.all()) + + if not courses: + print("No courses found. Please add some courses before running this script.") + return + + if not sessions: + print("No sessions found. Please add some sessions before running this script.") + return + + # Fetch all available users (lecturers) + lecturers = list( + User.objects.filter(is_lecturer=True) + ) # Assuming lecturers are lecturer + + if not lecturers: + print( + "No lecturers found. Please add some lecturers before running this script." + ) + return + + for _ in range(num_allocations): + lecturer = random.choice(lecturers) + session = random.choice(sessions) + + # Create a CourseAllocation instance + allocation = CourseAllocation.objects.create( + lecturer=lecturer, + session=session, + ) + + # Assign random courses to the course allocation + random_courses = random.sample( + courses, random.randint(1, len(courses)) + ) # Pick 1 to n random courses + allocation.courses.set(random_courses) + + print( + f"Created CourseAllocation for lecturer: {lecturer.username} with {len(random_courses)} courses." + ) + + print(f"Successfully populated {num_allocations} CourseAllocation instances.") + + def generate_fake_course_data( num_programs: int, num_courses: int, @@ -193,6 +247,3 @@ def generate_fake_course_data( # Generate fake course offers course_offers = CourseOfferFactory.create_batch(num_course_offers) print(f"Created {len(course_offers)} course offers.") - - -generate_fake_course_data(10, 10, 10, 10, 10, 10) diff --git a/templates/accounts/lecturer_list.html b/templates/accounts/lecturer_list.html index 33fcee0..8823ca1 100644 --- a/templates/accounts/lecturer_list.html +++ b/templates/accounts/lecturer_list.html @@ -55,12 +55,12 @@ -