def __init__(self, **kwargs): course_id = kwargs['course_id'] id_manager = CourseLocationManager(course_id) kwargs.setdefault('id_reader', id_manager) kwargs.setdefault('id_generator', id_manager) services = kwargs.get('services', {}) services.setdefault('cache', CacheService(DoNothingCache())) services.setdefault('field-data', DictFieldData({})) services.setdefault('sandbox', SandboxService(contentstore, course_id)) kwargs['services'] = services super().__init__(**kwargs)
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
def setUpClass(cls): """ Upload the python lib file to the test course. """ super().setUpClass() course_key = CourseLocator('test', 'sandbox_test', '2021_01') cls.sandbox_service = SandboxService(course_id=course_key, contentstore=contentstore) cls.zipfile = upload_file_to_course( course_key=course_key, contentstore=cls.sandbox_service.contentstore(), source_file=cls.PYTHON_LIB_SOURCE_FILE, target_filename=cls.PYTHON_LIB_FILENAME, )
def _preview_module_system(request, descriptor, field_data): """ Returns a ModuleSystem for the specified descriptor that is specialized for rendering module previews. request: The active django request descriptor: An XModuleDescriptor """ course_id = descriptor.location.course_key display_name_only = (descriptor.category == 'static_tab') replace_url_service = ReplaceURLService(course_id=course_id) wrappers = [ # This wrapper wraps the module in the template specified above partial(wrap_xblock, 'PreviewRuntime', display_name_only=display_name_only, usage_id_serializer=str, request_token=request_token(request)), # This wrapper replaces urls in the output that start with /static # with the correct course-specific url for the static content partial(replace_urls_wrapper, replace_url_service=replace_url_service, static_replace_only=True), _studio_wrap_xblock, ] wrappers_asides = [ partial(wrap_xblock_aside, 'PreviewRuntime', usage_id_serializer=str, request_token=request_token(request)) ] mako_service = MakoService(namespace_prefix='lms.') if settings.FEATURES.get("LICENSING", False): # stick the license wrapper in front wrappers.insert(0, partial(wrap_with_license, mako_service=mako_service)) return PreviewModuleSystem( static_url=settings.STATIC_URL, # TODO (cpennington): Do we want to track how instructors are using the preview problems? track_function=lambda event_type, event: None, get_module=partial(_load_preview_module, request), debug=True, mixins=settings.XBLOCK_MIXINS, course_id=course_id, # Set up functions to modify the fragment produced by student_view wrappers=wrappers, wrappers_asides=wrappers_asides, error_descriptor_class=ErrorBlock, # Get the raw DescriptorSystem, not the CombinedSystem descriptor_runtime=descriptor._runtime, # pylint: disable=protected-access services={ "field-data": field_data, "i18n": ModuleI18nService, 'mako': mako_service, "settings": SettingsService(), "user": DjangoXBlockUserService( request.user, anonymous_user_id='student', user_role=get_user_role(request.user, course_id), ), "partitions": StudioPartitionService(course_id=course_id), "teams_configuration": TeamsConfigurationService(), "sandbox": SandboxService(contentstore=contentstore, course_id=course_id), "cache": CacheService(cache), 'replace_urls': replace_url_service }, )
def validate_can_execute_unsafe_code(context_key, expected_result): sandbox_service = SandboxService(course_id=context_key, contentstore=None) assert expected_result == sandbox_service.can_execute_unsafe_code()