Update fake data generator

This commit is contained in:
Adil Mohak 2024-10-13 01:06:35 +03:00
parent bdcdbddebe
commit 6c32d16acb
7 changed files with 66 additions and 16 deletions

View File

@ -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)

View File

@ -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())

View File

@ -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)

View File

@ -55,12 +55,12 @@
<button class="btn btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fa fa-ellipsis-vertical"></i>
</button>
<ul class="dropdown-menu position-fixed">
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="{% url 'staff_edit' pk=lecturer.pk %}"><i class="unstyled me-2 fas fa-edit"></i>{% trans 'Update' %}</a></li>
<li><a class="dropdown-item" target="_blank" href="{% url 'profile_single' lecturer.id %}?download_pdf=1"><i class="unstyled me-2 fas fa-download"></i>{% trans 'Download PDF' %}</a></li>
<li><a class="dropdown-item text-danger" href="{% url 'lecturer_delete' pk=lecturer.pk %}"><i class="unstyled me-2 fas fa-trash-alt"></i> {% trans 'Delete' %}</a></li>
</ul>
</div>
</div>
</td>
{% endif %}

View File

@ -57,7 +57,7 @@
<button class="btn btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fa fa-ellipsis-vertical"></i>
</button>
<ul class="dropdown-menu position-fixed">
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="{% url 'student_edit' student.student.pk %}"><i class="unstyled me-2 fas fa-edit"></i>{% trans 'Update' %}</a></li>
<li><a class="dropdown-item" target="_blank" href="{% url 'profile_single' student.student.id %}?download_pdf=1"><i class="unstyled me-2 fas fa-download"></i>{% trans 'Download PDF' %}</a></li>
<li><a class="dropdown-item text-danger" href="{% url 'student_delete' student.pk %}"><i class="unstyled me-2 fas fa-trash-alt"></i>{% trans 'Delete' %}</a></li>

View File

@ -52,7 +52,7 @@
<button class="btn btn-sm" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fa fa-ellipsis-vertical"></i>
</button>
<ul class="dropdown-menu position-fixed">
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="{% url 'edit_program' pk=program.pk %}"><i class="unstyled me-2 fas fa-edit"></i>{% trans 'Update' %}</a></li>
<li><a class="dropdown-item text-danger" href="{% url 'program_delete' pk=program.pk %}"><i class="unstyled me-2 fas fa-trash-alt"></i>{% trans 'Delete' %}</a></li>
</ul>

View File

@ -77,7 +77,7 @@
data-bs-boundary="window" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-ellipsis-v m-0"></i>
</button>
<div class="dropdown-menu position-fixed">
<div class="dropdown-menu">
<a class="dropdown-item" href="{% url 'edit_course' slug=course.slug %}">
<i class="unstyled me-2 fas fa-pencil-alt"></i> {% trans 'Edit' %}
</a>