本文整理汇总了Python中xmodule.x_module.XModuleDescriptor.load_class方法的典型用法代码示例。如果您正苦于以下问题:Python XModuleDescriptor.load_class方法的具体用法?Python XModuleDescriptor.load_class怎么用?Python XModuleDescriptor.load_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xmodule.x_module.XModuleDescriptor
的用法示例。
在下文中一共展示了XModuleDescriptor.load_class方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def create_item(request):
parent_location = Location(request.POST["parent_location"])
category = request.POST["category"]
display_name = request.POST.get("display_name")
if not has_access(request.user, parent_location):
raise PermissionDenied()
parent = get_modulestore(category).get_item(parent_location)
dest_location = parent_location.replace(category=category, name=uuid4().hex)
# get the metadata, display_name, and definition from the request
metadata = {}
data = None
template_id = request.POST.get("boilerplate")
if template_id is not None:
clz = XModuleDescriptor.load_class(category)
if clz is not None:
template = clz.get_template(template_id)
if template is not None:
metadata = template.get("metadata", {})
data = template.get("data")
if display_name is not None:
metadata["display_name"] = display_name
get_modulestore(category).create_and_save_xmodule(
dest_location, definition_data=data, metadata=metadata, system=parent.system
)
if category not in DETACHED_CATEGORIES:
get_modulestore(parent.location).update_children(parent_location, parent.children + [dest_location.url()])
return JsonResponse({"id": dest_location.url()})
示例2: create_block_from_xml
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def create_block_from_xml(xml_data, system, org=None, course=None, default_class=None):
"""
Create an XBlock instance from XML data.
`xml_data' is a string containing valid xml.
`system` is an XMLParsingSystem.
`org` and `course` are optional strings that will be used in the generated
block's url identifiers.
`default_class` is the class to instantiate of the XML indicates a class
that can't be loaded.
Returns the fully instantiated XBlock.
"""
node = etree.fromstring(xml_data)
raw_class = XModuleDescriptor.load_class(node.tag, default_class)
xblock_class = system.mixologist.mix(raw_class)
# leave next line commented out - useful for low-level debugging
# log.debug('[create_block_from_xml] tag=%s, class=%s' % (node.tag, xblock_class))
url_name = node.get('url_name', node.get('slug'))
location = Location('i4x', org, course, node.tag, url_name)
scope_ids = ScopeIds(None, location.category, location, location)
xblock = xblock_class.parse_xml(node, system, scope_ids)
return xblock
示例3: load_from_json
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def load_from_json(json_data, system, default_class=None, parent_xblock=None):
"""
This method instantiates the correct subclass of XModuleDescriptor based
on the contents of json_data. It does not persist it and can create one which
has no usage id.
parent_xblock is used to compute inherited metadata as well as to append the new xblock.
json_data:
- 'location' : must have this field
- 'category': the xmodule category (required or location must be a Location)
- 'metadata': a dict of locally set metadata (not inherited)
- 'children': a list of children's usage_ids w/in this course
- 'definition':
- '_id' (optional): the usage_id of this. Will generate one if not given one.
"""
class_ = XModuleDescriptor.load_class(
json_data.get('category', json_data.get('location', {}).get('category')),
default_class
)
usage_id = json_data.get('_id', None)
if not '_inherited_settings' in json_data and parent_xblock is not None:
json_data['_inherited_settings'] = parent_xblock.xblock_kvs.inherited_settings.copy()
json_fields = json_data.get('fields', {})
for field_name in inheritance.InheritanceMixin.fields:
if field_name in json_fields:
json_data['_inherited_settings'][field_name] = json_fields[field_name]
new_block = system.xblock_from_json(class_, usage_id, json_data)
if parent_xblock is not None:
parent_xblock.children.append(new_block.scope_ids.usage_id)
# decache pending children field settings
parent_xblock.save()
return new_block
示例4: create_xmodule
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def create_xmodule(self, location, definition_data=None, metadata=None, system=None):
"""
Create the new xmodule but don't save it. Returns the new module.
:param location: a Location--must have a category
:param definition_data: can be empty. The initial definition_data for the kvs
:param metadata: can be empty, the initial metadata for the kvs
:param system: if you already have an xmodule from the course, the xmodule.system value
"""
if not isinstance(location, Location):
location = Location(location)
# differs from split mongo in that I believe most of this logic should be above the persistence
# layer but added it here to enable quick conversion. I'll need to reconcile these.
if metadata is None:
metadata = {}
if system is None:
system = CachingDescriptorSystem(
self,
{},
self.default_class,
None,
self.error_tracker,
self.render_template,
{}
)
xblock_class = XModuleDescriptor.load_class(location.category, self.default_class)
if definition_data is None:
if hasattr(xblock_class, 'data') and getattr(xblock_class, 'data').default is not None:
definition_data = getattr(xblock_class, 'data').default
else:
definition_data = {}
dbmodel = self._create_new_model_data(location.category, location, definition_data, metadata)
xmodule = xblock_class(system, dbmodel)
return xmodule
示例5: load_mixed_class
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def load_mixed_class(category):
"""
Load an XBlock by category name, and apply all defined mixins
"""
component_class = XModuleDescriptor.load_class(category)
mixologist = Mixologist(settings.XBLOCK_MIXINS)
return mixologist.mix(component_class)
示例6: _create
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def _create(cls, target_class, **kwargs):
"""
Uses ``**kwargs``:
:parent_location: (required): the location of the parent module
(e.g. the parent course or section)
:category: the category of the resulting item.
:data: (optional): the data for the item
(e.g. XML problem definition for a problem item)
:display_name: (optional): the display name of the item
:metadata: (optional): dictionary of metadata attributes
:boilerplate: (optional) the boilerplate for overriding field values
:target_class: is ignored
"""
DETACHED_CATEGORIES = ['about', 'static_tab', 'course_info']
# catch any old style users before they get into trouble
assert not 'template' in kwargs
data = kwargs.get('data')
category = kwargs.get('category')
display_name = kwargs.get('display_name')
metadata = kwargs.get('metadata', {})
location = kwargs.get('location')
if kwargs.get('boilerplate') is not None:
template_id = kwargs.get('boilerplate')
clz = XModuleDescriptor.load_class(category)
template = clz.get_template(template_id)
assert template is not None
metadata.update(template.get('metadata', {}))
if not isinstance(data, basestring):
data.update(template.get('data'))
store = kwargs.get('modulestore')
# replace the display name with an optional parameter passed in from the caller
if display_name is not None:
metadata['display_name'] = display_name
store.create_and_save_xmodule(location, metadata=metadata, definition_data=data)
if location.category not in DETACHED_CATEGORIES:
parent_location = Location(kwargs.get('parent_location'))
assert location != parent_location
# This code was based off that in cms/djangoapps/contentstore/views.py
parent = kwargs.get('parent') or store.get_item(parent_location)
parent.children.append(location.url())
store.update_children(parent_location, parent.children)
return store.get_item(location)
示例7: load_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def load_item(self, location):
"""
Return an XModule instance for the specified location
"""
location = Location(location)
json_data = self.module_data.get(location)
if json_data is None:
module = self.modulestore.get_item(location)
if module is not None:
# update our own cache after going to the DB to get cache miss
self.module_data.update(module.runtime.module_data)
return module
else:
# load the module and apply the inherited metadata
try:
category = json_data['location']['category']
class_ = XModuleDescriptor.load_class(
category,
self.default_class
)
definition = json_data.get('definition', {})
metadata = json_data.get('metadata', {})
for old_name, new_name in getattr(class_, 'metadata_translations', {}).items():
if old_name in metadata:
metadata[new_name] = metadata[old_name]
del metadata[old_name]
kvs = MongoKeyValueStore(
definition.get('data', {}),
definition.get('children', []),
metadata,
)
field_data = DbModel(kvs)
scope_ids = ScopeIds(None, category, location, location)
module = self.construct_xblock_from_class(class_, field_data, scope_ids)
if self.cached_metadata is not None:
# parent container pointers don't differentiate between draft and non-draft
# so when we do the lookup, we should do so with a non-draft location
non_draft_loc = location.replace(revision=None)
# Convert the serialized fields values in self.cached_metadata
# to python values
metadata_to_inherit = self.cached_metadata.get(non_draft_loc.url(), {})
inherit_metadata(module, metadata_to_inherit)
# decache any computed pending field settings
module.save()
return module
except:
log.warning("Failed to load descriptor", exc_info=True)
return ErrorDescriptor.from_json(
json_data,
self,
json_data['location'],
error_msg=exc_info_to_str(sys.exc_info())
)
示例8: _create_new_model_data
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def _create_new_model_data(self, category, location, definition_data, metadata):
"""
To instantiate a new xmodule which will be saved latter, set up the dbModel and kvs
"""
kvs = MongoKeyValueStore(definition_data, [], metadata, location, category)
class_ = XModuleDescriptor.load_class(category, self.default_class)
model_data = DbModel(kvs, class_, None, MongoUsage(None, location))
model_data["category"] = category
model_data["location"] = location
return model_data
示例9: load_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def load_item(self, location):
"""
Return an XModule instance for the specified location
"""
location = Location(location)
json_data = self.module_data.get(location)
if json_data is None:
module = self.modulestore.get_item(location)
if module is not None:
# update our own cache after going to the DB to get cache miss
self.module_data.update(module.system.module_data)
return module
else:
# load the module and apply the inherited metadata
try:
category = json_data['location']['category']
class_ = XModuleDescriptor.load_class(
category,
self.default_class
)
definition = json_data.get('definition', {})
metadata = json_data.get('metadata', {})
for old_name, new_name in class_.metadata_translations.items():
if old_name in metadata:
metadata[new_name] = metadata[old_name]
del metadata[old_name]
kvs = MongoKeyValueStore(
definition.get('data', {}),
definition.get('children', []),
metadata,
location,
category
)
model_data = DbModel(kvs, class_, None, MongoUsage(self.course_id, location))
model_data['category'] = category
model_data['location'] = location
module = class_(self, model_data)
if self.cached_metadata is not None:
# parent container pointers don't differentiate between draft and non-draft
# so when we do the lookup, we should do so with a non-draft location
non_draft_loc = location.replace(revision=None)
metadata_to_inherit = self.cached_metadata.get(non_draft_loc.url(), {})
inherit_metadata(module, metadata_to_inherit)
return module
except:
log.warning("Failed to load descriptor", exc_info=True)
return ErrorDescriptor.from_json(
json_data,
self,
json_data['location'],
error_msg=exc_info_to_str(sys.exc_info())
)
示例10: _load_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def _load_item(self, usage_id, course_entry_override=None):
# TODO ensure all callers of system.load_item pass just the id
json_data = self.module_data.get(usage_id)
if json_data is None:
# deeper than initial descendant fetch or doesn't exist
self.modulestore.cache_items(self, [usage_id], lazy=self.lazy)
json_data = self.module_data.get(usage_id)
if json_data is None:
raise ItemNotFoundError
class_ = XModuleDescriptor.load_class(json_data.get("category"), self.default_class)
return self.xblock_from_json(class_, usage_id, json_data, course_entry_override)
示例11: _load_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def _load_item(self, usage_id, course_entry_override=None):
if isinstance(usage_id, BlockUsageLocator) and isinstance(usage_id.usage_id, LocalId):
try:
return self.local_modules[usage_id]
except KeyError:
raise ItemNotFoundError
json_data = self.module_data.get(usage_id)
if json_data is None:
# deeper than initial descendant fetch or doesn't exist
self.modulestore.cache_items(self, [usage_id], lazy=self.lazy)
json_data = self.module_data.get(usage_id)
if json_data is None:
raise ItemNotFoundError(usage_id)
class_ = XModuleDescriptor.load_class(json_data.get("category"), self.default_class)
return self.xblock_from_json(class_, usage_id, json_data, course_entry_override)
示例12: create_xmodule
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def create_xmodule(self, location, definition_data=None, metadata=None, system=None):
"""
Create the new xmodule but don't save it. Returns the new module.
:param location: a Location--must have a category
:param definition_data: can be empty. The initial definition_data for the kvs
:param metadata: can be empty, the initial metadata for the kvs
:param system: if you already have an xblock from the course, the xblock.runtime value
"""
if not isinstance(location, Location):
location = Location(location)
# differs from split mongo in that I believe most of this logic should be above the persistence
# layer but added it here to enable quick conversion. I'll need to reconcile these.
if metadata is None:
metadata = {}
if system is None:
system = CachingDescriptorSystem(
modulestore=self,
module_data={},
default_class=self.default_class,
resources_fs=None,
error_tracker=self.error_tracker,
render_template=self.render_template,
cached_metadata={},
mixins=self.xblock_mixins,
)
xblock_class = XModuleDescriptor.load_class(location.category, self.default_class)
if definition_data is None:
if hasattr(xblock_class, 'data') and xblock_class.data.default is not None:
definition_data = xblock_class.data.default
else:
definition_data = {}
dbmodel = self._create_new_field_data(location.category, location, definition_data, metadata)
xmodule = system.construct_xblock_from_class(
xblock_class,
dbmodel,
# We're loading a descriptor, so student_id is meaningless
# We also don't have separate notions of definition and usage ids yet,
# so we use the location for both.
ScopeIds(None, location.category, location, location)
)
# decache any pending field settings from init
xmodule.save()
return xmodule
示例13: _create_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def _create_item(request):
"""View for create items."""
parent_locator = BlockUsageLocator(request.json['parent_locator'])
parent_location = loc_mapper().translate_locator_to_location(parent_locator)
category = request.json['category']
display_name = request.json.get('display_name')
if not has_access(request.user, parent_location):
raise PermissionDenied()
parent = get_modulestore(category).get_item(parent_location)
# Necessary to set revision=None or else metadata inheritance does not work
# (the ID with @draft will be used as the key in the inherited metadata map,
# and that is not expected by the code that later references it).
dest_location = parent_location.replace(category=category, name=uuid4().hex, revision=None)
# get the metadata, display_name, and definition from the request
metadata = {}
data = None
template_id = request.json.get('boilerplate')
if template_id is not None:
clz = XModuleDescriptor.load_class(category)
if clz is not None:
template = clz.get_template(template_id)
if template is not None:
metadata = template.get('metadata', {})
data = template.get('data')
if display_name is not None:
metadata['display_name'] = display_name
get_modulestore(category).create_and_save_xmodule(
dest_location,
definition_data=data,
metadata=metadata,
system=parent.system,
)
if category not in DETACHED_CATEGORIES:
get_modulestore(parent.location).update_children(parent_location, parent.children + [dest_location.url()])
course_location = loc_mapper().translate_locator_to_location(parent_locator, get_course=True)
locator = loc_mapper().translate_location(course_location.course_id, dest_location, False, True)
return JsonResponse({'id': dest_location.url(), "locator": unicode(locator)})
示例14: _create_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def _create_item(request):
"""View for create items."""
parent_locator = BlockUsageLocator(request.json['parent_locator'])
parent_location = loc_mapper().translate_locator_to_location(parent_locator)
try:
category = request.json['category']
except KeyError:
category = 'problem'
display_name = request.json.get('display_name')
if not has_access(request.user, parent_location):
raise PermissionDenied()
parent = get_modulestore(category).get_item(parent_location)
dest_location = parent_location.replace(category=category, name=uuid4().hex)
# get the metadata, display_name, and definition from the request
metadata = {}
data = None
template_id = request.json.get('boilerplate')
if template_id is not None:
clz = XModuleDescriptor.load_class(category)
if clz is not None:
template = clz.get_template(template_id)
if template is not None:
metadata = template.get('metadata', {})
data = template.get('data')
if display_name is not None:
metadata['display_name'] = display_name
get_modulestore(category).create_and_save_xmodule(
dest_location,
definition_data=data,
metadata=metadata,
system=parent.system,
)
if category not in DETACHED_CATEGORIES:
get_modulestore(parent.location).update_children(parent_location, parent.children + [dest_location.url()])
course_location = loc_mapper().translate_locator_to_location(parent_locator, get_course=True)
locator = loc_mapper().translate_location(course_location.course_id, dest_location, False, True)
return JsonResponse({"locator": unicode(locator)})
示例15: create_item
# 需要导入模块: from xmodule.x_module import XModuleDescriptor [as 别名]
# 或者: from xmodule.x_module.XModuleDescriptor import load_class [as 别名]
def create_item(request):
"""View for create items."""
parent_location = Location(request.json['parent_location'])
category = request.json['category']
display_name = request.json.get('display_name')
if not has_access(request.user, parent_location):
raise PermissionDenied()
parent = get_modulestore(category).get_item(parent_location)
dest_location = parent_location.replace(category=category, name=uuid4().hex)
# get the metadata, display_name, and definition from the request
metadata = {}
data = None
template_id = request.json.get('boilerplate')
if template_id is not None:
clz = XModuleDescriptor.load_class(category)
if clz is not None:
template = clz.get_template(template_id)
if template is not None:
metadata = template.get('metadata', {})
data = template.get('data')
if display_name is not None:
metadata['display_name'] = display_name
get_modulestore(category).create_and_save_xmodule(
dest_location,
definition_data=data,
metadata=metadata,
system=parent.system,
)
if category not in DETACHED_CATEGORIES:
get_modulestore(parent.location).update_children(parent_location, parent.children + [dest_location.url()])
locator = loc_mapper().translate_location(
get_course_for_item(parent_location).location.course_id, dest_location, False, True
)
return JsonResponse({'id': dest_location.url(), "update_url": locator.url_reverse("xblock")})