-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add options to dump_course_structure to output inherited metadata. #2008
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,11 @@ | |
| from django.core.management.base import BaseCommand, CommandError | ||
|
|
||
| from xmodule.modulestore.django import modulestore | ||
| from xmodule.modulestore.inheritance import own_metadata | ||
|
|
||
| from xmodule.modulestore.inheritance import own_metadata, compute_inherited_metadata | ||
| from xblock.fields import Scope | ||
|
|
||
| FILTER_LIST = ['xml_attributes', 'checklists'] | ||
| INHERITED_FILTER_LIST = ['children', 'xml_attributes', 'checklists'] | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
|
|
@@ -41,6 +42,14 @@ class Command(BaseCommand): | |
| action='store', | ||
| default='default', | ||
| help='Name of the modulestore'), | ||
| make_option('--inherited', | ||
| action='store_true', | ||
| default=False, | ||
| help='Whether to include inherited metadata'), | ||
| make_option('--inherited_defaults', | ||
| action='store_true', | ||
| default=False, | ||
| help='Whether to include default values of inherited metadata'), | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
|
|
@@ -62,14 +71,18 @@ def handle(self, *args, **options): | |
| if course is None: | ||
| raise CommandError("Invalid course_id") | ||
|
|
||
| # precompute inherited metadata at the course level, if needed: | ||
| if options['inherited']: | ||
| compute_inherited_metadata(course) | ||
|
|
||
| # Convert course data to dictionary and dump it as JSON to stdout | ||
|
|
||
| info = dump_module(course) | ||
| info = dump_module(course, inherited=options['inherited'], defaults=options['inherited_defaults']) | ||
|
|
||
| return json.dumps(info, indent=2, sort_keys=True) | ||
|
|
||
|
|
||
| def dump_module(module, destination=None): | ||
| def dump_module(module, destination=None, inherited=False, defaults=False): | ||
| """ | ||
| Add the module and all its children to the destination dictionary in | ||
| as a flat structure. | ||
|
|
@@ -83,10 +96,29 @@ def dump_module(module, destination=None): | |
| destination[module.location.url()] = { | ||
| 'category': module.location.category, | ||
| 'children': module.children if hasattr(module, 'children') else [], | ||
| 'metadata': filtered_metadata | ||
| 'metadata': filtered_metadata, | ||
| } | ||
|
|
||
| if inherited: | ||
| # when calculating inherited metadata, don't include existing | ||
| # locally-defined metadata | ||
| inherited_metadata_filter_list = list(filtered_metadata.keys()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [OPTIONAL] inherited_metadata_filter = frozenset(filtered_metadata.keys() + INHERITED_FILTER_LIST) |
||
| inherited_metadata_filter_list.extend(INHERITED_FILTER_LIST) | ||
|
|
||
| def is_inherited(field): | ||
| if field.name in inherited_metadata_filter_list: | ||
| return False | ||
| elif field.scope != Scope.settings: | ||
| return False | ||
| elif defaults == True: | ||
| return True | ||
| else: | ||
| return field.values != field.default | ||
|
|
||
| inherited_metadata = {field.name: field.read_json(module) for field in module.fields.values() if is_inherited(field)} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [OPTIONAL] inherited_metadata = {}
for field_name, field in module.fields.iteritems(): # this makes it more clear to me why you are only using the values
if is_inherited(field):
inherited_metadata[field_name] = field.read_json(module) |
||
| destination[module.location.url()]['inherited_metadata'] = inherited_metadata | ||
|
|
||
| for child in module.get_children(): | ||
| dump_module(child, destination) | ||
| dump_module(child, destination, inherited, defaults) | ||
|
|
||
| return destination | ||
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.
We can't do this inside dump_module?
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.
This is a one-time call -- it should be called only once for the entire course. dump_module is called recursively.