Add get_courses() to mongo modulestore

* Note: This doesn't run yet
* add config file for lms on mongo
* Some comment additions and cleanups
This commit is contained in:
Victor Shnayder
2012-07-18 14:43:25 -04:00
committed by Calen Pennington
parent 51ae0d5484
commit 49e8d08c1e
5 changed files with 55 additions and 10 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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')

View File

@@ -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')

16
lms/envs/dev_mongo.py Normal file
View File

@@ -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',
}
}
}