How to use the xblock.runtime function in XBlock

To help you get started, we’ve selected a few XBlock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / coursebuilder_xblock_module / tests / xblock_module.py View on Github external
def get_key(self, scope, user_id, block_scope_id, field_name):
        xblock_key = xblock.runtime.KeyValueStore.Key(
            scope=scope,
            user_id=user_id,
            block_scope_id=block_scope_id,
            field_name=field_name)
        key_str = xblock_module.store.key_string(xblock_key)
        db_key = db.Key.from_path('KeyValueEntity', key_str)
        return db_key
github google / appengine_xblock_runtime / tests / test_runtime.py View on Github external
def test_slider_block(self):
        """Test peristence of fields in user scope."""
        usage_id = self.runtime.parse_xml_string(
            '', self.id_generator)
        block = self.runtime.get_block(usage_id)

        block.value = 50
        block.save()

        key = xblock.runtime.KeyValueStore.Key(
            scope=xblock.fields.Scope.user_state,
            user_id=self.STUDENT_ID,
            block_scope_id=block.scope_ids.usage_id,
            field_name='value')

        self.assertEqual(50, store.KeyValueStore().get(key))
github google / appengine_xblock_runtime / tests / test_runtime.py View on Github external
def test_html_block(self):
        """Test persistence of fields in content scope."""
        usage_id = self.runtime.parse_xml_string(
            'text', self.id_generator)
        block = self.runtime.get_block(usage_id)

        key = xblock.runtime.KeyValueStore.Key(
            scope=xblock.fields.Scope.content,
            user_id=None,
            block_scope_id=block.scope_ids.def_id,
            field_name='content')

        self.assertEqual('text', store.KeyValueStore().get(key))
github google / coursebuilder_xblock_module / src / modules / xblock_module / xblock_module.py View on Github external
class ForbiddenXBlockError(Exception):
    """Raised when a non-whitelisted XBlock is requested."""


def select_xblock(identifier, entry_points):
    """Hook called when loading XBlock classes, which enforces whitelist."""
    entry_point = xblock.plugin.default_select(identifier, entry_points)
    if str(entry_point) not in XBLOCK_WHITELIST:
        raise ForbiddenXBlockError(
            'Attempted to load forbidden XBlock: %s' % str(entry_point))
    return entry_point


class MemoryIdManager(xblock.runtime.MemoryIdManager):

    def create_usage(self, def_id, usage_id=None):
        """Extend the method definition to allow a specified usage_id."""
        usage_id = usage_id or appengine_xblock_runtime.runtime.generate_id()
        self._usages[usage_id] = def_id
        return usage_id

    def create_definition(self, block_type, def_id=None):
        """Extend the method definition to allow a specified def_id."""
        def_id = def_id or appengine_xblock_runtime.runtime.generate_id()
        self._definitions[def_id] = block_type
        return def_id


class Runtime(appengine_xblock_runtime.runtime.Runtime):
    """A XBlock runtime which uses the App Engine datastore."""
github google / appengine_xblock_runtime / appengine_xblock_runtime / runtime.py View on Github external
"""Create a new definition id, bound to the given block type.

        Args:
            block_type: str. The type of the XBlock for this definition.

        Returns:
            str. The id of the new definition.
        """
        definition_id = generate_id()
        definition = store.DefinitionEntity(id=definition_id)
        definition.block_type = block_type
        definition.put()
        return definition_id


class Runtime(xblock.runtime.Runtime):
    """An XBlock runtime which uses the App Engine datastore."""

    def __init__(
            self, id_reader=None, field_data=None, student_id=None, **kwargs):
        super(Runtime, self).__init__(
            id_reader or IdReader(),
            field_data or xblock.runtime.KvsFieldData(store.KeyValueStore()),
            **kwargs)
        self.user_id = student_id
github google / coursebuilder_xblock_module / src / modules / xblock_module / xblock_module.py View on Github external
def __init__(
            self, handler, id_reader=None, field_data=None, student_id=None,
            is_admin=False):

        field_data = field_data or xblock.runtime.KvsFieldData(
            store.KeyValueStore())

        if is_admin:
            pass
        elif student_id:
            field_data = StudentFieldData(field_data)
        else:
            field_data = xblock.field_data.ReadOnlyFieldData(field_data)

        def get_jinja_template(template_name, dirs):
            locale = handler.app_context.get_environ()['course']['locale']
            return jinja_utils.get_template(template_name, dirs, locale=locale)
        services = {'jinja': get_jinja_template}

        super(Runtime, self).__init__(
            id_reader=id_reader, field_data=field_data, student_id=student_id,
github google / coursebuilder_xblock_module / cb-xblocks-core / cb_xblocks_core / problem.py View on Github external
def __init__(self, runtime, field_data, scope_ids):
        extras = RuntimeExtras(self, runtime)
        runtime = xblock.runtime.ObjectAggregator(extras, runtime)

        super(ProblemBlock, self).__init__(runtime, field_data, scope_ids)

        self.close_date = None
        self.close_date = None

        if self.seed is None:
            self.choose_new_seed()

        self.lcp = self.new_lcp(self.get_state_for_lcp())
github google / coursebuilder_xblock_module / src / modules / xblock_module / xblock_module.py View on Github external
the datastore, if it exists.
            dry_run: bool. If set True, then parse the XML but do not do any
                datastore writes.
            log: file-like. A buffer to write back the XML representation of the
                XBlock tree which has been assembled.

        Returns:
            str. The usage id of the root block of the XML tree.
        """
        if orig_xml_str is None:
            orig_xml_str = ''
        if log is None:
            log = StringIO()

        id_manager = MemoryIdManager()
        dict_key_value_store = xblock.runtime.DictKeyValueStore()

        old_id_reader = self.id_reader
        self.id_reader = id_manager

        old_field_data = self.field_data
        self.field_data = xblock.runtime.KvsFieldData(dict_key_value_store)

        try:
            root_usage_id = super(Runtime, self).parse_xml_string(
                xml_str, id_manager)

            block = self.get_block(root_usage_id)
            self.export_to_xml(block, log)
        finally:
            self.id_reader = old_id_reader
            self.field_data = old_field_data
github google / appengine_xblock_runtime / appengine_xblock_runtime / runtime.py View on Github external
def __init__(
            self, id_reader=None, field_data=None, student_id=None, **kwargs):
        super(Runtime, self).__init__(
            id_reader or IdReader(),
            field_data or xblock.runtime.KvsFieldData(store.KeyValueStore()),
            **kwargs)
        self.user_id = student_id
github google / appengine_xblock_runtime / appengine_xblock_runtime / runtime.py View on Github external
def get_definition_id(self, usage_id):
        """Retrieve the definition id to which this usage id is bound."""
        usage = ndb.Key(store.UsageEntity, str(usage_id)).get()
        if usage is None:
            raise xblock.exceptions.NoSuchUsage(str(usage_id))
        return str(usage.definition_id)

    def get_block_type(self, def_id):
        """Retrieve the block type to which this definition is bound."""
        definition = ndb.Key(store.DefinitionEntity, str(def_id)).get()
        if definition is None:
            raise xblock.exceptions.NoSuchDefinition(str(def_id))
        return definition.block_type


class IdGenerator(xblock.runtime.IdGenerator):
    """Implementation of XBlock IdGenerator using App Engine datastore.

    This manages the graph of many-to-one relationships between
    usages, definitions, and blocks. The schema is:
        usage (n) -- (1) definition (n) -- (1) block_type
    """

    def create_usage(self, def_id):
        """Create a new usage id bound to the given definition id."""
        definition_key = ndb.Key(store.DefinitionEntity, str(def_id))
        assert definition_key.get() is not None
        usage_id = generate_id()
        usage = store.UsageEntity(id=usage_id)
        usage.definition_id = def_id
        usage.put()
        return usage_id