forked from openedx/edx-platform
    
        
        - 
                Notifications
    
You must be signed in to change notification settings  - Fork 7
 
Adds management command to populate course cohorts for a course #269
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
          
     Open
      
      
            rehan99000
  wants to merge
  1
  commit into
  develop-juniper
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
rehan/EDLY-3608
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop-juniper
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            52 changes: 52 additions & 0 deletions
          
          52 
        
  openedx/features/edly/management/commands/populate_cohorts_for_course.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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) | ||
| 
     | 
||
| logger.info('Cohorts populated successfully') | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool.