-
Notifications
You must be signed in to change notification settings - Fork 4.3k
add a new django-admin command to dump out a course structure document. ... #294
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
Merged
Merged
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
55 changes: 55 additions & 0 deletions
55
cms/djangoapps/contentstore/management/commands/dump_course_structure.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,55 @@ | ||
| from django.core.management.base import BaseCommand, CommandError | ||
| from xmodule.course_module import CourseDescriptor | ||
| from xmodule.modulestore.django import modulestore | ||
| from json import dumps | ||
| from xmodule.modulestore.inheritance import own_metadata | ||
| from django.conf import settings | ||
|
|
||
| filter_list = ['xml_attributes', 'checklists'] | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = '''Write out to stdout a structural and metadata information about a course in a flat dictionary serialized | ||
| in a JSON format. This can be used for analytics.''' | ||
|
|
||
| def handle(self, *args, **options): | ||
| if len(args) < 2 or len(args) > 3: | ||
| raise CommandError("dump_course_structure requires two or more arguments: <location> <outfile> |<db>|") | ||
|
|
||
| course_id = args[0] | ||
| outfile = args[1] | ||
|
|
||
| # use a user-specified database name, if present | ||
| # this is useful for doing dumps from databases restored from prod backups | ||
| if len(args) == 3: | ||
| settings.MODULESTORE['direct']['OPTIONS']['db'] = args[2] | ||
|
|
||
| loc = CourseDescriptor.id_to_location(course_id) | ||
|
|
||
| store = modulestore() | ||
|
|
||
| course = None | ||
| try: | ||
| course = store.get_item(loc, depth=4) | ||
| except: | ||
| print 'Could not find course at {0}'.format(course_id) | ||
| return | ||
|
|
||
| info = {} | ||
|
|
||
| def dump_into_dict(module, info): | ||
| filtered_metadata = dict((key, value) for key, value in own_metadata(module).iteritems() | ||
| if key not in filter_list) | ||
| info[module.location.url()] = { | ||
| 'category': module.location.category, | ||
| 'children': module.children if hasattr(module, 'children') else [], | ||
| 'metadata': filtered_metadata | ||
| } | ||
|
|
||
| for child in module.get_children(): | ||
| dump_into_dict(child, info) | ||
|
|
||
| dump_into_dict(course, info) | ||
|
|
||
| with open(outfile, 'w') as f: | ||
| f.write(dumps(info)) | ||
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.
<location> <outfile> [<db>]is the usual syntax for optional arguments.