Commit e972b230 by Don Mitchell

Requesting an xmodule field in wrong app context raises exception rather than assertion

parent 563b713a
...@@ -31,3 +31,10 @@ class SerializationError(Exception): ...@@ -31,3 +31,10 @@ class SerializationError(Exception):
def __init__(self, location, msg): def __init__(self, location, msg):
super(SerializationError, self).__init__(msg) super(SerializationError, self).__init__(msg)
self.location = location self.location = location
class UndefinedContext(Exception):
"""
Tried to access an xmodule field which needs a different context (runtime) to have a value.
"""
pass
...@@ -15,6 +15,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError, InvalidLocationErr ...@@ -15,6 +15,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError, InvalidLocationErr
from uuid import uuid4 from uuid import uuid4
from xmodule.modulestore.mongo.base import MongoModuleStore from xmodule.modulestore.mongo.base import MongoModuleStore
from xmodule.modulestore.split_mongo.split import SplitMongoModuleStore from xmodule.modulestore.split_mongo.split import SplitMongoModuleStore
from xmodule.exceptions import UndefinedContext
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -207,9 +208,10 @@ class MixedModuleStore(ModuleStoreWriteBase): ...@@ -207,9 +208,10 @@ class MixedModuleStore(ModuleStoreWriteBase):
""" """
Get the course_id from the block or from asking its store. Expensive. Get the course_id from the block or from asking its store. Expensive.
""" """
# HACK: check xmodule_runtime to avoid an assertion error try:
if block.xmodule_runtime is not None and block.course_id is not None:
return block.course_id return block.course_id
except UndefinedContext:
pass
try: try:
course = store._get_course_for_item(block.scope_ids.usage_id) course = store._get_course_for_item(block.scope_ids.usage_id)
if course is not None: if course is not None:
...@@ -262,9 +264,10 @@ class MixedModuleStore(ModuleStoreWriteBase): ...@@ -262,9 +264,10 @@ class MixedModuleStore(ModuleStoreWriteBase):
blocks = store.get_items(location) blocks = store.get_items(location)
if len(blocks) == 1: if len(blocks) == 1:
block = blocks[0] block = blocks[0]
# HACK violate abstraction to avoid assertion error try:
if block.xmodule_runtime is not None and block.course_id is not None:
return block.course_id return block.course_id
except UndefinedContext:
pass
except ItemNotFoundError: except ItemNotFoundError:
pass pass
# if we get here, it must be in a Locator based store, but we won't be able to find # if we get here, it must be in a Locator based store, but we won't be able to find
......
...@@ -26,6 +26,7 @@ from xmodule.errortracker import exc_info_to_str ...@@ -26,6 +26,7 @@ from xmodule.errortracker import exc_info_to_str
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.modulestore.exceptions import ItemNotFoundError, InsufficientSpecificationError, InvalidLocationError from xmodule.modulestore.exceptions import ItemNotFoundError, InsufficientSpecificationError, InvalidLocationError
from xmodule.modulestore.locator import BlockUsageLocator from xmodule.modulestore.locator import BlockUsageLocator
from xmodule.exceptions import UndefinedContext
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -795,7 +796,8 @@ class XModuleDescriptor(XModuleMixin, HTMLSnippet, ResourceTemplates, XBlock): ...@@ -795,7 +796,8 @@ class XModuleDescriptor(XModuleMixin, HTMLSnippet, ResourceTemplates, XBlock):
Returns the XModule corresponding to this descriptor. Expects that the system Returns the XModule corresponding to this descriptor. Expects that the system
already supports all of the attributes needed by xmodules already supports all of the attributes needed by xmodules
""" """
assert self.xmodule_runtime is not None if self.xmodule_runtime is None:
raise UndefinedContext()
assert self.xmodule_runtime.error_descriptor_class is not None assert self.xmodule_runtime.error_descriptor_class is not None
if self.xmodule_runtime.xmodule_instance is None: if self.xmodule_runtime.xmodule_instance is None:
try: try:
......
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