Commit f9c78e07 by Calen Pennington

Turn the course envelope into an actual object that just stores an existing course_key

parent eea1552e
...@@ -20,3 +20,5 @@ class BlockKey(namedtuple('BlockKey', 'type id')): ...@@ -20,3 +20,5 @@ class BlockKey(namedtuple('BlockKey', 'type id')):
def from_usage_key(cls, usage_key): def from_usage_key(cls, usage_key):
return cls(usage_key.block_type, usage_key.block_id) return cls(usage_key.block_type, usage_key.block_id)
CourseEnvelope = namedtuple('CourseEnvelope', 'course_key structure')
...@@ -14,12 +14,13 @@ from fs.osfs import OSFS ...@@ -14,12 +14,13 @@ from fs.osfs import OSFS
from .definition_lazy_loader import DefinitionLazyLoader from .definition_lazy_loader import DefinitionLazyLoader
from xmodule.modulestore.edit_info import EditInfoRuntimeMixin from xmodule.modulestore.edit_info import EditInfoRuntimeMixin
from xmodule.modulestore.inheritance import inheriting_field_data, InheritanceMixin from xmodule.modulestore.inheritance import inheriting_field_data, InheritanceMixin
from xmodule.modulestore.split_mongo import BlockKey from xmodule.modulestore.split_mongo import BlockKey, CourseEnvelope
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
new_contract('BlockUsageLocator', BlockUsageLocator) new_contract('BlockUsageLocator', BlockUsageLocator)
new_contract('BlockKey', BlockKey) new_contract('BlockKey', BlockKey)
new_contract('CourseEnvelope', CourseEnvelope)
class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
...@@ -29,6 +30,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -29,6 +30,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
Computes the settings (nee 'metadata') inheritance upon creation. Computes the settings (nee 'metadata') inheritance upon creation.
""" """
@contract(course_entry=CourseEnvelope)
def __init__(self, modulestore, course_entry, default_class, module_data, lazy, **kwargs): def __init__(self, modulestore, course_entry, default_class, module_data, lazy, **kwargs):
""" """
Computes the settings inheritance and sets up the cache. Computes the settings inheritance and sets up the cache.
...@@ -44,10 +46,10 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -44,10 +46,10 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
underlying modulestore underlying modulestore
""" """
# needed by capa_problem (as runtime.filestore via this.resources_fs) # needed by capa_problem (as runtime.filestore via this.resources_fs)
if 'course' in course_entry: if course_entry.course_key.course:
root = modulestore.fs_root / course_entry['org'] / course_entry['course'] / course_entry['run'] root = modulestore.fs_root / course_entry.course_key.org / course_entry.course_key.course / course_entry.course_key.run
else: else:
root = modulestore.fs_root / course_entry['structure']['_id'] root = modulestore.fs_root / course_entry.structure['_id']
root.makedirs_p() # create directory if it doesn't exist root.makedirs_p() # create directory if it doesn't exist
super(CachingDescriptorSystem, self).__init__( super(CachingDescriptorSystem, self).__init__(
...@@ -67,12 +69,12 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -67,12 +69,12 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
@contract(returns="dict(BlockKey: BlockKey)") @contract(returns="dict(BlockKey: BlockKey)")
def _parent_map(self): def _parent_map(self):
parent_map = {} parent_map = {}
for block_key, block in self.course_entry['structure']['blocks'].iteritems(): for block_key, block in self.course_entry.structure['blocks'].iteritems():
for child in block['fields'].get('children', []): for child in block['fields'].get('children', []):
parent_map[child] = block_key parent_map[child] = block_key
return parent_map return parent_map
@contract(usage_key="BlockUsageLocator | BlockKey") @contract(usage_key="BlockUsageLocator | BlockKey", course_entry_override="CourseEnvelope | None")
def _load_item(self, usage_key, course_entry_override=None, **kwargs): def _load_item(self, usage_key, course_entry_override=None, **kwargs):
# usage_key is either a UsageKey or just the block_key. if a usage_key, # usage_key is either a UsageKey or just the block_key. if a usage_key,
if isinstance(usage_key, BlockUsageLocator): if isinstance(usage_key, BlockUsageLocator):
...@@ -92,18 +94,12 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -92,18 +94,12 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
block_key = usage_key block_key = usage_key
course_info = course_entry_override or self.course_entry course_info = course_entry_override or self.course_entry
course_key = CourseLocator( course_key = course_info.course_key
version_guid=course_info['structure']['_id'],
org=course_info.get('org'),
course=course_info.get('course'),
run=course_info.get('run'),
branch=course_info.get('branch'),
)
if course_entry_override: if course_entry_override:
structure_id = course_entry_override.get('_id') structure_id = course_entry_override.structure.get('_id')
else: else:
structure_id = self.course_entry.get('_id') structure_id = self.course_entry.structure.get('_id')
json_data = self.get_module_data(block_key, course_key) json_data = self.get_module_data(block_key, course_key)
...@@ -147,10 +143,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -147,10 +143,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
course_entry_override = self.course_entry course_entry_override = self.course_entry
else: else:
# most recent retrieval is most likely the right one for next caller (see comment above fn) # most recent retrieval is most likely the right one for next caller (see comment above fn)
self.course_entry['branch'] = course_entry_override['branch'] self.course_entry = CourseEnvelope(course_entry_override.course_key, self.course_entry.structure)
self.course_entry['org'] = course_entry_override['org']
self.course_entry['course'] = course_entry_override['course']
self.course_entry['run'] = course_entry_override['run']
definition_id = json_data.get('definition') definition_id = json_data.get('definition')
...@@ -163,7 +156,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -163,7 +156,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
self.modulestore, block_key.type, definition_id, self.modulestore, block_key.type, definition_id,
lambda fields: self.modulestore.convert_references_to_keys( lambda fields: self.modulestore.convert_references_to_keys(
course_key, self.load_block_type(block_key.type), course_key, self.load_block_type(block_key.type),
fields, self.course_entry['structure']['blocks'], fields, self.course_entry.structure['blocks'],
) )
) )
else: else:
...@@ -180,7 +173,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -180,7 +173,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
) )
converted_fields = self.modulestore.convert_references_to_keys( converted_fields = self.modulestore.convert_references_to_keys(
block_locator.course_key, class_, json_data.get('fields', {}), self.course_entry['structure']['blocks'], block_locator.course_key, class_, json_data.get('fields', {}), self.course_entry.structure['blocks'],
) )
if block_key in self._parent_map: if block_key in self._parent_map:
parent_key = self._parent_map[block_key] parent_key = self._parent_map[block_key]
...@@ -210,8 +203,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin): ...@@ -210,8 +203,7 @@ class CachingDescriptorSystem(MakoDescriptorSystem, EditInfoRuntimeMixin):
return ErrorDescriptor.from_json( return ErrorDescriptor.from_json(
json_data, json_data,
self, self,
BlockUsageLocator( course_entry_override.course_key.make_usage_key(
CourseLocator(version_guid=course_entry_override['structure']['_id']),
block_type='error', block_type='error',
block_id=block_key.id block_id=block_key.id
), ),
......
...@@ -242,7 +242,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS ...@@ -242,7 +242,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS
:return: True if the draft and published versions differ :return: True if the draft and published versions differ
""" """
def get_course(branch_name): def get_course(branch_name):
return self._lookup_course(xblock.location.course_key.for_branch(branch_name))['structure'] return self._lookup_course(xblock.location.course_key.for_branch(branch_name)).structure
def get_block(course_structure, block_key): def get_block(course_structure, block_key):
return self._get_block_from_structure(course_structure, block_key) return self._get_block_from_structure(course_structure, block_key)
...@@ -318,7 +318,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS ...@@ -318,7 +318,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS
# get head version of Published branch # get head version of Published branch
published_course_structure = self._lookup_course( published_course_structure = self._lookup_course(
location.course_key.for_branch(ModuleStoreEnum.BranchName.published) location.course_key.for_branch(ModuleStoreEnum.BranchName.published)
)['structure'] ).structure
published_block = self._get_block_from_structure( published_block = self._get_block_from_structure(
published_course_structure, published_course_structure,
BlockKey.from_usage_key(location) BlockKey.from_usage_key(location)
...@@ -327,7 +327,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS ...@@ -327,7 +327,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS
raise InvalidVersionError(location) raise InvalidVersionError(location)
# create a new versioned draft structure # create a new versioned draft structure
draft_course_structure = self._lookup_course(draft_course_key)['structure'] draft_course_structure = self._lookup_course(draft_course_key).structure
new_structure = self.version_structure(draft_course_key, draft_course_structure, user_id) new_structure = self.version_structure(draft_course_key, draft_course_structure, user_id)
# remove the block and its descendants from the new structure # remove the block and its descendants from the new structure
...@@ -394,7 +394,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS ...@@ -394,7 +394,7 @@ class DraftVersioningModuleStore(ModuleStoreDraftAndPublished, SplitMongoModuleS
pass pass
def _get_head(self, xblock, branch): def _get_head(self, xblock, branch):
course_structure = self._lookup_course(xblock.location.course_key.for_branch(branch))['structure'] course_structure = self._lookup_course(xblock.location.course_key.for_branch(branch)).structure
return self._get_block_from_structure(course_structure, BlockKey.from_usage_key(xblock.location)) return self._get_block_from_structure(course_structure, BlockKey.from_usage_key(xblock.location))
def _get_version(self, block): def _get_version(self, block):
......
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