Ejemplo n.º 1
0
    def service(self, block, service_name):
        """Return a service, or None.

        Services are objects implementing arbitrary other interfaces.  They are
        requested by agreed-upon names, see [XXX TODO] for a list of possible
        services.  The object returned depends on the service requested.

        XBlocks must announce their intention to request services with the
        `XBlock.needs` or `XBlock.wants` decorators.  Use `needs` if you assume
        that the service is available, or `wants` if your code is flexible and
        can accept a None from this method.

        Runtimes can override this method if they have different techniques for
        finding and delivering services.

        Arguments:
            block (XBlock): this block's class will be examined for service
                decorators.
            service_name (str): the name of the service requested.

        Returns:
            An object implementing the requested service, or None.

        """
        declaration = block.service_declaration(service_name)
        if declaration is None:
            raise NoSuchServiceError(
                "Service {!r} was not requested.".format(service_name))
        service = self._services.get(service_name)
        if service is None and declaration == "need":
            raise NoSuchServiceError(
                "Service {!r} is not available.".format(service_name))
        return service
Ejemplo n.º 2
0
 def service(self, block, service_name):
     """
     Return a service, or None.
     Services are objects implementing arbitrary other interfaces.
     """
     # TODO: Do these declarations actually help with anything? Maybe this check should
     # be removed from here and from XBlock.runtime
     declaration = block.service_declaration(service_name)
     if declaration is None:
         raise NoSuchServiceError("Service {!r} was not requested.".format(service_name))
     # Most common service is field-data so check that first:
     if service_name == "field-data":
         if block.scope_ids not in self.block_field_datas:
             try:
                 self.block_field_datas[block.scope_ids] = self._init_field_data_for_block(block)
             except:
                 # Don't try again pointlessly every time another field is accessed
                 self.block_field_datas[block.scope_ids] = None
                 raise
         return self.block_field_datas[block.scope_ids]
     elif service_name == "completion":
         context_key = block.scope_ids.usage_id.context_key
         return CompletionService(user=self.user, context_key=context_key)
     # Check if the XBlockRuntimeSystem wants to handle this:
     service = self.system.get_service(block, service_name)
     # Otherwise, fall back to the base implementation which loads services
     # defined in the constructor:
     if service is None:
         service = super(XBlockRuntime, self).service(block, service_name)
     return service
Ejemplo n.º 3
0
 def service(self, _, service_name):
     """
     Mocked version of `runtime.service(self, service_name)`
     """
     if service_name == 'teams':
         if self.has_teams_service:
             return self.teams_service
     elif service_name == 'teams_configuration':
         if self.has_teams_configuration_service:
             return self.teams_configuration_service
     raise NoSuchServiceError()
Ejemplo n.º 4
0
    def service(self, block, service_name):
        """
        Return a service, or None.
        Services are objects implementing arbitrary other interfaces.
        """
        # TODO: Do these declarations actually help with anything? Maybe this check should
        # be removed from here and from XBlock.runtime
        declaration = block.service_declaration(service_name)
        if declaration is None:
            raise NoSuchServiceError(
                f"Service {service_name!r} was not requested.")
        # Most common service is field-data so check that first:
        if service_name == "field-data":
            if block.scope_ids not in self.block_field_datas:
                try:
                    self.block_field_datas[
                        block.scope_ids] = self._init_field_data_for_block(
                            block)
                except:
                    # Don't try again pointlessly every time another field is accessed
                    self.block_field_datas[block.scope_ids] = None
                    raise
            return self.block_field_datas[block.scope_ids]
        elif service_name == "completion":
            context_key = block.scope_ids.usage_id.context_key
            return CompletionService(user=self.user, context_key=context_key)
        elif service_name == "user":
            return DjangoXBlockUserService(
                self.user,
                # The value should be updated to whether the user is staff in the context when Blockstore runtime adds
                # support for courses.
                user_is_staff=self.user.is_staff,
                anonymous_user_id=self.anonymous_student_id,
            )
        elif service_name == "mako":
            return MakoService()
        elif service_name == "i18n":
            return ModuleI18nService(block=block)
        elif service_name == 'sandbox':
            context_key = block.scope_ids.usage_id.context_key
            return SandboxService(contentstore=contentstore,
                                  course_id=context_key)
        elif service_name == 'cache':
            return CacheService(cache)

        # Check if the XBlockRuntimeSystem wants to handle this:
        service = self.system.get_service(block, service_name)
        # Otherwise, fall back to the base implementation which loads services
        # defined in the constructor:
        if service is None:
            service = super().service(block, service_name)
        return service
Ejemplo n.º 5
0
 def service(self, block, service_name):
     """
     Return a service, or None.
     Services are objects implementing arbitrary other interfaces.
     """
     # TODO: Do these declarations actually help with anything? Maybe this check should
     # be removed from here and from XBlock.runtime
     declaration = block.service_declaration(service_name)
     if declaration is None:
         raise NoSuchServiceError(
             "Service {!r} was not requested.".format(service_name))
     # Special case handling for some services:
     service = self.system.get_service(block.scope_ids, service_name)
     if service is None:
         service = super(XBlockRuntime, self).service(block, service_name)
     return service