Skip to content

Instantly share code, notes, and snippets.

@viadanna
Created September 16, 2021 21:22
Show Gist options
  • Save viadanna/c835ad2cc5684b2a68575400640a00a9 to your computer and use it in GitHub Desktop.
Save viadanna/c835ad2cc5684b2a68575400640a00a9 to your computer and use it in GitHub Desktop.
Testing Gendered Cohort assignment during enrollment
from openedx.core.djangoapps.course_groups.models import CohortMembership
from common.djangoapps.course_modes.models import CourseMode
from opaque_keys.edx.keys import CourseKey
from common.djangoapps.student.models import CourseEnrollment
from django.contrib.auth import get_user_model
def enroll(username='edx', course_key='course-v1:edX+DemoX+Demo_Course'):
user = get_user_model().objects.get(username=username)
course_id = CourseKey.from_string(course_key)
available_modes = CourseMode.modes_for_course_dict(course_id)
enroll_mode = CourseMode.auto_enroll_mode(course_id, available_modes)
check_access = True
if enroll_mode:
CourseEnrollment.enroll(user, course_id, check_access=check_access, mode=enroll_mode)
def unenroll(username='edx', course_key='course-v1:edX+DemoX+Demo_Course'):
user = get_user_model().objects.get(username=username)
course_id = CourseKey.from_string(course_key)
CourseEnrollment.unenroll(user, course_id)
def test():
users = ['farahnaji_adam', 'farahnaji_jenna']
courses = [
'course-v1:YTP_English+LDR101_en+Q7',
'course-v1:YTP_English+STG101_en+Q16',
'course-v1:YTP_English+CCP300_en+Q5',
]
print('Unenroll\n')
for user in users:
for course in courses:
try:
unenroll(user, course)
except Exception as e:
print(str(e))
print('Enroll\n')
for user in users:
for course in courses:
try:
enroll(user, course)
except Exception as e:
print(str(e))
print('Check\n')
for user in users:
for course in courses:
print(CohortMembership.objects.get(user__username=user, course_id=course).course_user_group)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment