|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Management command for refreshing the skills associated with xblocks. |
| 4 | +""" |
| 5 | + |
| 6 | +import logging |
| 7 | + |
| 8 | +from django.core.management.base import BaseCommand |
| 9 | +from django.utils.translation import gettext as _ |
| 10 | +from opaque_keys import InvalidKeyError |
| 11 | +from opaque_keys.edx.keys import CourseKey, UsageKey |
| 12 | + |
| 13 | +from taxonomy import utils |
| 14 | +from taxonomy.choices import ProductTypes |
| 15 | +from taxonomy.exceptions import InvalidCommandOptionsError, XBlockMetadataNotFoundError |
| 16 | +from taxonomy.models import RefreshXBlockSkillsConfig |
| 17 | +from taxonomy.providers.utils import get_course_metadata_provider, get_xblock_metadata_provider |
| 18 | + |
| 19 | +LOGGER = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class Command(BaseCommand): |
| 23 | + """ |
| 24 | + Command to refresh skills associated with the XBlocks. |
| 25 | +
|
| 26 | + Example usage: |
| 27 | + $ ./manage.py refresh_xblock_skills --xblock 'xblock-usage-key1' --xblock 'xblock-usage-key2' --commit |
| 28 | + $ # To refresh all xblock skills under given courses. |
| 29 | + $ ./manage.py refresh_xblock_skills --course 'course-v1:edX+DemoX+1' --course 'course-v1:edX+DemoY+1' --commit |
| 30 | + $ # args-from-database means command line arguments will be picked from the database. |
| 31 | + $ ./manage.py refresh_xblock_skills --args-from-database |
| 32 | + $ # To update all xblocks in all the courses |
| 33 | + $ ./manage.py refresh_xblock_skills --all --commit |
| 34 | + """ |
| 35 | + help = 'Refreshes the skills associated with XBlocks.' |
| 36 | + product_type = ProductTypes.XBlock |
| 37 | + |
| 38 | + def add_arguments(self, parser): |
| 39 | + """ |
| 40 | + Add arguments to the command parser. |
| 41 | + """ |
| 42 | + parser.add_argument( |
| 43 | + '--course', |
| 44 | + metavar=_('COURSE_KEY'), |
| 45 | + action='append', |
| 46 | + help=_('Update skills for XBlocks under given course keys. For eg. course-v1:edX+DemoX.1+2014'), |
| 47 | + default=[], |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + '--xblock', |
| 51 | + metavar=_('USAGE_KEY'), |
| 52 | + action='append', |
| 53 | + help=_('Update skills for given Xblock usage keys.'), |
| 54 | + default=[], |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + '--args-from-database', |
| 58 | + action='store_true', |
| 59 | + help=_('Use arguments from the RefreshXBlockSkillsConfig model instead of the command line.'), |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + '--all', |
| 63 | + action='store_true', |
| 64 | + help=_('Create xblock skill mapping for all xblocks in all the courses.'), |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + '--commit', |
| 68 | + action='store_true', |
| 69 | + default=False, |
| 70 | + help=_('Commits the skills to storage.') |
| 71 | + ) |
| 72 | + |
| 73 | + def get_args_from_database(self): |
| 74 | + """ |
| 75 | + Return an options dictionary from the current RefreshXBlockSkillsConfig model. |
| 76 | + """ |
| 77 | + config = RefreshXBlockSkillsConfig.get_solo() |
| 78 | + argv = config.arguments.split() |
| 79 | + parser = self.create_parser('manage.py', 'refresh_xblock_skills') |
| 80 | + return parser.parse_args(argv).__dict__ |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def is_valid_key(key, key_cls, key_cls_str): |
| 84 | + """ |
| 85 | + Validates usage and course keys. |
| 86 | + """ |
| 87 | + try: |
| 88 | + key_cls.from_string(key) |
| 89 | + return True |
| 90 | + except InvalidKeyError: |
| 91 | + LOGGER.error('[TAXONOMY] Invalid %s: [%s]', key_cls_str, key) |
| 92 | + return False |
| 93 | + |
| 94 | + def handle(self, *args, **options): |
| 95 | + """ |
| 96 | + Entry point for management command execution. |
| 97 | + """ |
| 98 | + if not (options['args_from_database'] or options['all'] or options['course'] or options['xblock']): |
| 99 | + raise InvalidCommandOptionsError( |
| 100 | + 'Either course, xblock, args_from_database or all argument must be provided.', |
| 101 | + ) |
| 102 | + |
| 103 | + if options['args_from_database']: |
| 104 | + options = self.get_args_from_database() |
| 105 | + |
| 106 | + if options['course'] and options['xblock']: |
| 107 | + raise InvalidCommandOptionsError('Either course or xblock argument should be provided and not both.') |
| 108 | + |
| 109 | + LOGGER.info('[TAXONOMY] Refresh XBlock Skills. Options: [%s]', options) |
| 110 | + |
| 111 | + courses = [] |
| 112 | + xblocks_from_args = [] |
| 113 | + xblock_provider = get_xblock_metadata_provider() |
| 114 | + if options['all']: |
| 115 | + courses = get_course_metadata_provider().get_all_courses() |
| 116 | + elif options['course']: |
| 117 | + courses = [{"key": course} for course in options['course']] |
| 118 | + elif options['xblock']: |
| 119 | + valid_usage_keys = set(key for key in options['xblock'] if self.is_valid_key(key, UsageKey, "UsageKey")) |
| 120 | + xblocks_from_args = xblock_provider.get_xblocks(xblock_ids=list(valid_usage_keys)) |
| 121 | + if not xblocks_from_args: |
| 122 | + raise XBlockMetadataNotFoundError( |
| 123 | + 'No xblock metadata was found for following xblocks. {}'.format(options['xblock']) |
| 124 | + ) |
| 125 | + else: |
| 126 | + raise InvalidCommandOptionsError('Either course, xblock or --all argument must be provided.') |
| 127 | + |
| 128 | + for course in courses: |
| 129 | + course_key = course.get("key") |
| 130 | + if self.is_valid_key(course_key, CourseKey, "CourseKey"): |
| 131 | + xblocks = xblock_provider.get_all_xblocks_in_course(course_key) |
| 132 | + LOGGER.info('[TAXONOMY] Refresh xblocks skills process started for course: {course_key}.') |
| 133 | + utils.refresh_product_skills(xblocks, options['commit'], self.product_type) |
| 134 | + |
| 135 | + if xblocks_from_args: |
| 136 | + LOGGER.info('[TAXONOMY] Refresh XBlock skills process started for xblocks: [%s]', options['xblock']) |
| 137 | + utils.refresh_product_skills(xblocks_from_args, options['commit'], self.product_type) |
0 commit comments