Files
edx-platform/lms/djangoapps/debug/management/commands/dump_xml_courses.py
Calen Pennington cd746bf8e5 Make course ids and usage ids opaque to LMS and Studio [partial commit]
This commit adds the non-courseware lms/djangoapps and lms/lib.

These keys are now objects with a limited interface, and the particular
internal representation is managed by the data storage layer (the
modulestore).

For the LMS, there should be no outward-facing changes to the system.
The keys are, for now, a change to internal representation only. For
Studio, the new serialized form of the keys is used in urls, to allow
for further migration in the future.

Co-Author: Andy Armstrong <andya@edx.org>
Co-Author: Christina Roberts <christina@edx.org>
Co-Author: David Baumgold <db@edx.org>
Co-Author: Diana Huang <dkh@edx.org>
Co-Author: Don Mitchell <dmitchell@edx.org>
Co-Author: Julia Hansbrough <julia@edx.org>
Co-Author: Nimisha Asthagiri <nasthagiri@edx.org>
Co-Author: Sarina Canelake <sarina@edx.org>

[LMS-2370]
2014-05-08 12:09:23 -04:00

61 lines
2.3 KiB
Python

"""
Export all xml courses in a diffable format.
This command loads all of the xml courses in the configured DATA_DIR.
For each of the courses, it loops through all of the modules, and dumps
each as a separate output file containing the json representation
of each of its fields (including those fields that are set as default values).
"""
from __future__ import print_function
import json
from path import path
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from xmodule.modulestore.xml import XMLModuleStore
class Command(BaseCommand):
"""
Django management command to export diffable representations of all xml courses
"""
help = '''Dump the in-memory representation of all xml courses in a diff-able format'''
args = '<export path>'
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError('Must called with arguments: {}'.format(self.args))
xml_module_store = XMLModuleStore(
data_dir=settings.DATA_DIR,
default_class='xmodule.hidden_module.HiddenDescriptor',
load_error_modules=True,
xblock_mixins=settings.XBLOCK_MIXINS,
xblock_select=settings.XBLOCK_SELECT_FUNCTION,
)
export_dir = path(args[0])
for course_id, course_modules in xml_module_store.modules.iteritems():
course_path = course_id.replace('/', '_')
for location, descriptor in course_modules.iteritems():
location_path = location.to_deprecated_string().replace('/', '_')
data = {}
for field_name, field in descriptor.fields.iteritems():
try:
data[field_name] = field.read_json(descriptor)
except Exception as exc: # pylint: disable=broad-except
data[field_name] = {
'$type': str(type(exc)),
'$value': descriptor._field_data.get(descriptor, field_name) # pylint: disable=protected-access
}
outdir = export_dir / course_path
outdir.makedirs_p()
with open(outdir / location_path + '.json', 'w') as outfile:
json.dump(data, outfile, sort_keys=True, indent=4)
print('', file=outfile)