Commit 49e8d08c by Victor Shnayder Committed by Calen Pennington

Add get_courses() to mongo modulestore

* Note: This doesn't run yet
* add config file for lms on mongo
* Some comment additions and cleanups
parent 51ae0d54
...@@ -221,3 +221,11 @@ class ModuleStore(object): ...@@ -221,3 +221,11 @@ class ModuleStore(object):
metadata: A nested dictionary of module metadata metadata: A nested dictionary of module metadata
""" """
raise NotImplementedError raise NotImplementedError
def get_courses(self):
'''
Returns a list containing the top level XModuleDescriptors of the courses
in this modulestore.
'''
raise NotImplementedError
import pymongo import pymongo
from bson.objectid import ObjectId from bson.objectid import ObjectId
from fs.osfs import OSFS
from importlib import import_module from importlib import import_module
from xmodule.x_module import XModuleDescriptor from xmodule.x_module import XModuleDescriptor
from xmodule.mako_module import MakoDescriptorSystem from xmodule.mako_module import MakoDescriptorSystem
...@@ -77,6 +79,7 @@ class MongoModuleStore(ModuleStore): ...@@ -77,6 +79,7 @@ class MongoModuleStore(ModuleStore):
# that is used when querying by a location # that is used when querying by a location
self.collection.ensure_index(zip(('_id.' + field for field in Location._fields), repeat(1))) 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('.') module_path, _, class_name = default_class.rpartition('.')
class_ = getattr(import_module(module_path), class_name) class_ = getattr(import_module(module_path), class_name)
self.default_class = class_ self.default_class = class_
...@@ -142,6 +145,14 @@ class MongoModuleStore(ModuleStore): ...@@ -142,6 +145,14 @@ class MongoModuleStore(ModuleStore):
return [self._load_item(item, data_cache) for item in items] 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): def get_item(self, location, depth=0):
""" """
Returns an XModuleDescriptor instance for the item at location. Returns an XModuleDescriptor instance for the item at location.
......
...@@ -42,8 +42,8 @@ class XMLModuleStore(ModuleStore): ...@@ -42,8 +42,8 @@ class XMLModuleStore(ModuleStore):
self.eager = eager self.eager = eager
self.data_dir = path(data_dir) self.data_dir = path(data_dir)
self.modules = {} self.modules = {} # location -> XModuleDescriptor
self.courses = {} self.courses = {} # course_dir -> XModuleDescriptor for the course
if default_class is None: if default_class is None:
self.default_class = None self.default_class = None
...@@ -77,20 +77,24 @@ class XMLModuleStore(ModuleStore): ...@@ -77,20 +77,24 @@ class XMLModuleStore(ModuleStore):
with open(self.data_dir / course_dir / "course.xml") as course_file: 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_file = StringIO(clean_out_mako_templating(course_file.read()))
course_data = etree.parse(course_file).getroot() course_data = etree.parse(course_file).getroot()
org = course_data.get('org') org = course_data.get('org')
if org is None: 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' org = 'edx'
course = course_data.get('course') course = course_data.get('course')
if course is None: 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, dir=course_dir,
default=course_dir default=course_dir
)) ))
...@@ -106,7 +110,8 @@ class XMLModuleStore(ModuleStore): ...@@ -106,7 +110,8 @@ class XMLModuleStore(ModuleStore):
def process_xml(xml): def process_xml(xml):
try: 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 = clean_out_mako_templating(xml)
xml_data = etree.fromstring(xml) xml_data = etree.fromstring(xml)
except: except:
...@@ -117,17 +122,21 @@ class XMLModuleStore(ModuleStore): ...@@ -117,17 +122,21 @@ class XMLModuleStore(ModuleStore):
slug = Location.clean(xml_data.get('name')) slug = Location.clean(xml_data.get('name'))
else: else:
self.unnamed_modules += 1 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: if slug in self.used_slugs:
self.unnamed_modules += 1 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) self.used_slugs.add(slug)
# log.debug('-> slug=%s' % slug) # log.debug('-> slug=%s' % slug)
xml_data.set('slug', 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)) log.debug('==> importing module location %s' % repr(module.location))
module.metadata['data_dir'] = course_dir module.metadata['data_dir'] = course_dir
...@@ -145,6 +154,7 @@ class XMLModuleStore(ModuleStore): ...@@ -145,6 +154,7 @@ class XMLModuleStore(ModuleStore):
) )
MakoDescriptorSystem.__init__(self, **system_kwargs) MakoDescriptorSystem.__init__(self, **system_kwargs)
XMLParsingSystem.__init__(self, **system_kwargs) XMLParsingSystem.__init__(self, **system_kwargs)
course_descriptor = ImportSystem(self).process_xml(etree.tostring(course_data)) course_descriptor = ImportSystem(self).process_xml(etree.tostring(course_data))
log.debug('========> Done with course import') log.debug('========> Done with course import')
......
...@@ -241,7 +241,7 @@ class XModuleDescriptor(Plugin): ...@@ -241,7 +241,7 @@ class XModuleDescriptor(Plugin):
'data_dir' '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 # equal
equality_attributes = ('definition', 'metadata', 'location', 'shared_state_key', '_inherited_metadata') equality_attributes = ('definition', 'metadata', 'location', 'shared_state_key', '_inherited_metadata')
......
"""
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',
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment