From 49e8d08c1e47a696e3198ae1e96d55fe798e482a Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Wed, 18 Jul 2012 14:43:25 -0400 Subject: [PATCH] Add get_courses() to mongo modulestore * Note: This doesn't run yet * add config file for lms on mongo * Some comment additions and cleanups --- .../xmodule/xmodule/modulestore/__init__.py | 8 ++++++ .../lib/xmodule/xmodule/modulestore/mongo.py | 11 ++++++++ common/lib/xmodule/xmodule/modulestore/xml.py | 28 +++++++++++++------ common/lib/xmodule/xmodule/x_module.py | 2 +- lms/envs/dev_mongo.py | 16 +++++++++++ 5 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 lms/envs/dev_mongo.py diff --git a/common/lib/xmodule/xmodule/modulestore/__init__.py b/common/lib/xmodule/xmodule/modulestore/__init__.py index 0390c314ab..5527d4108e 100644 --- a/common/lib/xmodule/xmodule/modulestore/__init__.py +++ b/common/lib/xmodule/xmodule/modulestore/__init__.py @@ -221,3 +221,11 @@ class ModuleStore(object): metadata: A nested dictionary of module metadata """ raise NotImplementedError + + def get_courses(self): + ''' + Returns a list containing the top level XModuleDescriptors of the courses + in this modulestore. + ''' + raise NotImplementedError + diff --git a/common/lib/xmodule/xmodule/modulestore/mongo.py b/common/lib/xmodule/xmodule/modulestore/mongo.py index ac265549a8..3ab0e7c634 100644 --- a/common/lib/xmodule/xmodule/modulestore/mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/mongo.py @@ -1,5 +1,7 @@ import pymongo + from bson.objectid import ObjectId +from fs.osfs import OSFS from importlib import import_module from xmodule.x_module import XModuleDescriptor from xmodule.mako_module import MakoDescriptorSystem @@ -77,6 +79,7 @@ class MongoModuleStore(ModuleStore): # that is used when querying by a location self.collection.ensure_index(zip(('_id.' + field for field in Location._fields), repeat(1))) + # TODO (vshnayder): default arg default_class=None will make this error module_path, _, class_name = default_class.rpartition('.') class_ = getattr(import_module(module_path), class_name) self.default_class = class_ @@ -142,6 +145,14 @@ class MongoModuleStore(ModuleStore): return [self._load_item(item, data_cache) for item in items] + def get_courses(self): + ''' + Returns a list of course descriptors. + ''' + # TODO (vshnayder): Why do I have to specify i4x here? + course_filter = Location("i4x", category="course") + return self.get_items(course_filter) + def get_item(self, location, depth=0): """ Returns an XModuleDescriptor instance for the item at location. diff --git a/common/lib/xmodule/xmodule/modulestore/xml.py b/common/lib/xmodule/xmodule/modulestore/xml.py index 4c5fe43e1d..b8e86b97f1 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml.py +++ b/common/lib/xmodule/xmodule/modulestore/xml.py @@ -42,8 +42,8 @@ class XMLModuleStore(ModuleStore): self.eager = eager self.data_dir = path(data_dir) - self.modules = {} - self.courses = {} + self.modules = {} # location -> XModuleDescriptor + self.courses = {} # course_dir -> XModuleDescriptor for the course if default_class is None: self.default_class = None @@ -77,20 +77,24 @@ class XMLModuleStore(ModuleStore): with open(self.data_dir / course_dir / "course.xml") as course_file: - # TODO (cpennington): Remove this once all fall 2012 courses have been imported into the cms from xml + # TODO (cpennington): Remove this once all fall 2012 courses have been imported + # into the cms from xml course_file = StringIO(clean_out_mako_templating(course_file.read())) course_data = etree.parse(course_file).getroot() org = course_data.get('org') if org is None: - log.error("No 'org' attribute set for course in {dir}. Using default 'edx'".format(dir=course_dir)) + log.error( + "No 'org' attribute set for course in {dir}. Using default 'edx'".format( + dir=course_dir)) org = 'edx' course = course_data.get('course') if course is None: - log.error("No 'course' attribute set for course in {dir}. Using default '{default}'".format( + log.error( + "No 'course' attribute set for course in {dir}. Using default '{default}'".format( dir=course_dir, default=course_dir )) @@ -106,7 +110,8 @@ class XMLModuleStore(ModuleStore): def process_xml(xml): try: - # TODO (cpennington): Remove this once all fall 2012 courses have been imported into the cms from xml + # TODO (cpennington): Remove this once all fall 2012 courses + # have been imported into the cms from xml xml = clean_out_mako_templating(xml) xml_data = etree.fromstring(xml) except: @@ -117,17 +122,21 @@ class XMLModuleStore(ModuleStore): slug = Location.clean(xml_data.get('name')) else: self.unnamed_modules += 1 - slug = '{tag}_{count}'.format(tag=xml_data.tag, count=self.unnamed_modules) + slug = '{tag}_{count}'.format(tag=xml_data.tag, + count=self.unnamed_modules) if slug in self.used_slugs: self.unnamed_modules += 1 - slug = '{slug}_{count}'.format(slug=slug, count=self.unnamed_modules) + slug = '{slug}_{count}'.format(slug=slug, + count=self.unnamed_modules) self.used_slugs.add(slug) # log.debug('-> slug=%s' % slug) xml_data.set('slug', slug) - module = XModuleDescriptor.load_from_xml(etree.tostring(xml_data), self, org, course, xmlstore.default_class) + module = XModuleDescriptor.load_from_xml( + etree.tostring(xml_data), self, org, + course, xmlstore.default_class) log.debug('==> importing module location %s' % repr(module.location)) module.metadata['data_dir'] = course_dir @@ -145,6 +154,7 @@ class XMLModuleStore(ModuleStore): ) MakoDescriptorSystem.__init__(self, **system_kwargs) XMLParsingSystem.__init__(self, **system_kwargs) + course_descriptor = ImportSystem(self).process_xml(etree.tostring(course_data)) log.debug('========> Done with course import') diff --git a/common/lib/xmodule/xmodule/x_module.py b/common/lib/xmodule/xmodule/x_module.py index 5e9370d1c8..fa904367f0 100644 --- a/common/lib/xmodule/xmodule/x_module.py +++ b/common/lib/xmodule/xmodule/x_module.py @@ -241,7 +241,7 @@ class XModuleDescriptor(Plugin): 'data_dir' ) - # A list of descriptor attributes that must be equal for the discriptors to be + # A list of descriptor attributes that must be equal for the descriptors to be # equal equality_attributes = ('definition', 'metadata', 'location', 'shared_state_key', '_inherited_metadata') diff --git a/lms/envs/dev_mongo.py b/lms/envs/dev_mongo.py new file mode 100644 index 0000000000..f690032aed --- /dev/null +++ b/lms/envs/dev_mongo.py @@ -0,0 +1,16 @@ +""" +This config file runs the dev environment, but with mongo as the datastore +""" +from .dev import * + +MODULESTORE = { + 'default': { + 'ENGINE': 'xmodule.modulestore.mongo.MongoModuleStore', + 'OPTIONS': { + 'default_class': 'xmodule.raw_module.RawDescriptor', + 'host': 'localhost', + 'db': 'xmodule', + 'collection': 'modulestore', + } + } +}