Skip to content

Commit 2ae204a

Browse files
committed
added management command to populate course cohorts for a course
1 parent 28b3f42 commit 2ae204a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Populate cohorts for a course if cohorts tab is broken.
3+
"""
4+
5+
import logging
6+
7+
from django.core.management import BaseCommand, CommandError
8+
9+
from opaque_keys import InvalidKeyError
10+
from opaque_keys.edx.keys import CourseKey
11+
12+
from lms.djangoapps.courseware.courses import get_course
13+
from openedx.core.djangoapps.course_groups.models import CourseCohort, CourseUserGroup
14+
15+
16+
logger = logging.getLogger(__name__)
17+
18+
19+
class Command(BaseCommand):
20+
"""
21+
Populate cohorts for a course.
22+
"""
23+
help = 'Populate cohorts for a course'
24+
25+
def add_arguments(self, parser):
26+
"""
27+
Add arguments to the command parser.
28+
"""
29+
parser.add_argument(
30+
'--course',
31+
action='store',
32+
type=str,
33+
required=True,
34+
help='The course ID of the course whose cohorts need to be populated.'
35+
)
36+
37+
def handle(self, *args, **options):
38+
course_id = options['course']
39+
try:
40+
course_key = CourseKey.from_string(course_id)
41+
except InvalidKeyError:
42+
raise CommandError('Course ID %s is incorrect' % course_id)
43+
44+
course = get_course(course_key)
45+
cohorts = CourseUserGroup.objects.filter(
46+
course_id=course.id, group_type=CourseUserGroup.COHORT).exclude(name__in=course.auto_cohort_groups)
47+
logger.info('Number of cohorts: %s', cohorts.count())
48+
logger.info('Cohorts: %s', ', '.join(cohorts.values_list('name', flat=True)))
49+
for cohort in cohorts:
50+
CourseCohort.create(course_user_group=cohort)
51+
52+
logger.info('Cohorts populated successfully')

0 commit comments

Comments
 (0)