Skip to content

Commit 5e792aa

Browse files
committed
Added enrollment generation function. Added docstrings
1 parent ba8dda0 commit 5e792aa

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

canvas_mock_data.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
fake = Faker()
99

1010

11-
def generate_users(num_users, account_id):
11+
def generate_users(account_id, num_users):
12+
"""
13+
Create realistic-looking mock users.
14+
15+
:param account_id: The Canvas Account to create users in.
16+
:type account_id: int
17+
:param num_users: The number of users to create.
18+
:type num_users: int
19+
"""
1220
account = canvas.get_account(account_id)
1321

1422
for i in range(1, num_users + 1):
@@ -28,7 +36,15 @@ def generate_users(num_users, account_id):
2836
account.create_user(pseudonym, user=user)
2937

3038

31-
def generate_courses(num_courses, account_id):
39+
def generate_courses(account_id, num_courses):
40+
"""
41+
Create realistic-looking mock courses.
42+
43+
:param account_id: The Canvas Account to create courses in.
44+
:type account_id: int
45+
:param num_courses: The number of courses to create.
46+
:type num_courses: int
47+
"""
3248
account = canvas.get_account(account_id)
3349

3450
for i in range(1, num_courses + 1):
@@ -39,3 +55,41 @@ def generate_courses(num_courses, account_id):
3955
}
4056

4157
account.create_course(course=course_dict)
58+
59+
60+
def generate_enrollments(account_id, min_students=1, max_students=5):
61+
"""
62+
Enroll random users into each course.
63+
64+
:param account_id: The Canvas Account to create enrollments in.
65+
:type account_id: int
66+
:param min_students: The minimum number of student enrollments to add per course.
67+
:type min_students: int
68+
:param max_students: The maximum number of student enrollments to add per course.
69+
:type max_students: int
70+
"""
71+
account = canvas.get_account(account_id)
72+
73+
user_list = [user for user in account.get_users()]
74+
courses = account.get_courses()
75+
76+
already_enrolled = []
77+
78+
for course in courses:
79+
num_students = fake.random_int(min=min_students, max=max_students)
80+
for i in range(0, num_students):
81+
enroll_user = fake.random_element(user_list)
82+
83+
if enroll_user.id in already_enrolled:
84+
continue
85+
86+
enroll_type = 'StudentEnrollment'
87+
enrollment = {
88+
'notify': False,
89+
'enrollment_state': 'active'
90+
}
91+
92+
course.enroll_user(enroll_user, enroll_type, enrollment=enrollment)
93+
already_enrolled.append(enroll_user.id)
94+
95+
# TODO: Add support for enrolling Teachers/ TAs

0 commit comments

Comments
 (0)