Base class for XBlocks.
Derive from this class to create a new kind of XBlock. There are no required methods, but you will probably need at least one view.
Don’t provide the
__init__ method when deriving from this class.
Construct a new XBlock.
This class should only be instantiated by runtimes.
Add children to etree.Element node.
For exporting, set data on etree.Element node.
Reset the cache of children stored on this XBlock.
Save all fields that are specified in field_names, even if they are not dirty.
Return the child identified by
Return instantiated XBlocks for each of this blocks
Return the parent block of this block, or None if there isn’t one.
Gets the public directory for this XBlock.
Gets the resource directory for this XBlock.
Handle request with this block’s runtime.
A decorator to indicate a function is usable as a handler.
The wrapped function must return a webob.Response object.
Return whether this block has a cached parent block.
Returns whether the given view has support for the given functionality.
An XBlock view declares support for a functionality with the @XBlock.supports decorator. The decorator stores information on the view.
Note: We implement this as an instance method to allow xBlocks to override it, if necessary.
return key/value fields to feed an index within in a Python dict object values may be numeric / string or dict
Wrap a handler to consume and produce JSON.
Rather than a Request object, the method will now be passed the JSON-decoded body of the request. The request should be a POST request in order to use this method. Any data returned by the function will be JSON-encoded and returned as the response.
The wrapped function can raise JsonHandlerError to return an error response with a non-200 status code.
This decorator will return a 405 HTTP status code if the method is not POST. This decorator will return a 400 status code if the body contains invalid JSON.
Load a single class specified by identifier.
If identifier specifies more than a single class, and select is not None, then call select on the list of entry_points. Otherwise, choose the first one and log a warning.
If default is provided, return it if no entry_point matching identifier is found. Otherwise, will raise a PluginMissingError
If select is provided, it should be a callable of the form:
def select(identifier, all_entry_points): # ... return an_entry_point
The all_entry_points argument will be a list of all entry_points matching identifier that were found, and select should return one of those entry_points to be loaded. select should raise PluginMissingError if no plugin is found, or AmbiguousPluginError if too many plugins are found
Load all the classes for a plugin.
Produces a sequence containing the identifiers and their corresponding classes for all of the available instances of this plugin.
fail_silently causes the code to simply log warnings if a plugin cannot import. The goal is to be able to use part of libraries from an XBlock (and thus have it installed), even if the overall XBlock cannot be used (e.g. depends on Django in a non-Django application). There is disagreement about whether this is a good idea, or whether we should see failures early (e.g. on startup or first page load), and in what contexts. Hence, the flag.
Produce a sequence of all XBlock classes tagged with tag.
fail_silently causes the code to simply log warnings if a plugin cannot import. The goal is to be able to use part of libraries from an XBlock (and thus have it installed), even if the overall XBlock cannot be used (e.g. depends on Django in a non-Django application). There is diagreement about whether this is a good idea, or whether we should see failures early (e.g. on startup or first page load), and in what contexts. Hence, the flag.
A class decorator to indicate that an XBlock class needs particular services.
Open a local resource.
The container calls this method when it receives a request for a resource on a URL which was generated by Runtime.local_resource_url(). It will pass the URI from the original call to local_resource_url() back to this method. The XBlock must parse this URI and return an open file-like object for the resource.
For security reasons, the default implementation will return only a very restricted set of file types, which must be located in a folder that defaults to “public”. The location used for public resources can be changed on a per-XBlock basis. XBlock authors who want to override this behavior will need to take care to ensure that the method only serves legitimate public resources. At the least, the URI should be matched against a whitelist regex to ensure that you do not serve an unauthorized resource.
Use node to construct a new block.
Element) – The xml node to parse into an xblock.
Runtime) – The runtime to use while parsing.
ScopeIds) – The keys identifying where this block
will store its data.
IdGenerator) – An object that will allow the
runtime to generate correct definition and usage ids for
children of this block.
Decorate a function to run with a temporary plugin available.
Use it like this in tests:
@register_temp_plugin(MyXBlockClass): def test_the_thing(): # Here I can load MyXBlockClass by name.
Render view with this block’s runtime and the supplied context
Save all dirty fields attached to this XBlock.
Find and return a service declaration.
XBlocks declare their service requirements with @XBlock.needs and @XBlock.wants decorators. These store information on the class. This function finds those declarations for a block.
service_name (str) – the name of the service requested.
One of “need”, “want”, or None.
A view decorator to indicate that an xBlock view has support for the given functionalities.
functionalities – String identifiers for the functionalities of the view. For example: “multi_device”.
Returns a function that adds the words in tags as class tags to this class.
Translates message/text and returns it in a unicode string. Using runtime to get i18n service.
Ask this xblock to validate itself. Subclasses are expected to override this method, as there is currently only a no-op implementation. Any overriding method should call super to collect validation results from its superclasses, and then add any additional results as necessary.
A class decorator to indicate that an XBlock class wants particular services.
What XML element name should be used for this block?
What is the text content for this block’s XML node?