Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds management command to populate course cohorts for a course #269

Open
wants to merge 1 commit into
base: develop-juniper
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Populate cohorts for a course if cohorts tab is broken.
"""

import logging

from django.core.management import BaseCommand, CommandError

from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey

from lms.djangoapps.courseware.courses import get_course
from openedx.core.djangoapps.course_groups.models import CourseCohort, CourseUserGroup


logger = logging.getLogger(__name__)


class Command(BaseCommand):
"""
Populate cohorts for a course.
"""
help = 'Populate cohorts for a course'

def add_arguments(self, parser):
"""
Add arguments to the command parser.
"""
parser.add_argument(
'--course',
action='store',
type=str,
required=True,
help='The course ID of the course whose cohorts need to be populated.'
)

def handle(self, *args, **options):
course_id = options['course']
try:
course_key = CourseKey.from_string(course_id)
except InvalidKeyError:
raise CommandError('Course ID %s is incorrect' % course_id)

course = get_course(course_key)
cohorts = CourseUserGroup.objects.filter(
course_id=course.id, group_type=CourseUserGroup.COHORT).exclude(name__in=course.auto_cohort_groups)
logger.info('Number of cohorts: %s', cohorts.count())
logger.info('Cohorts: %s', ', '.join(cohorts.values_list('name', flat=True)))
for cohort in cohorts:
CourseCohort.create(course_user_group=cohort)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we have some cohort groups already created? Maybe we need to use get/create

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zubair-arbi that is already handled inside this method.
image

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.


logger.info('Cohorts populated successfully')