示例#1
0
def _get_overrides_for_user(user, block):
    """
    Gets all of the individual student overrides for given user and block.
    Returns a dictionary of field override values keyed by field name.
    """
    if (
        hasattr(block, "scope_ids") and
        hasattr(block.scope_ids, "usage_id") and
        is_xblock_aside(block.scope_ids.usage_id)
    ):
        location = block.scope_ids.usage_id.usage_key
    else:
        location = block.location

    query = StudentFieldOverride.objects.filter(
        course_id=block.runtime.course_id,
        location=location,
        student_id=user.id,
    )
    overrides = {}
    for override in query:
        field = block.fields[override.field]
        value = field.from_json(json.loads(override.value))
        overrides[override.field] = value
    return overrides
def xblock_handler(request,
                   course_key_string,
                   usage_key_string,
                   handler,
                   suffix=''):
    """
    Dispatch an AJAX action to an xblock

    Args:
        usage_id: The usage-id of the block to dispatch to
        handler (str): The handler to execute
        suffix (str): The remainder of the url to be passed to the handler

    Returns:
        :class:`django.http.HttpResponse`: The response from the handler, converted to a
            django response

    Example:
    POST ${STUDIO_URL}/sn-api/courses/{course_key}/xblocks/{usage_key}/handler/{handler}/

    See https://github.com/edx/edx-platform/blob/open-release/juniper.master/cms/djangoapps/contentstore/views/component.py#L449
    """
    usage_key = UsageKey.from_string(usage_key_string)

    # Let the module handle the AJAX
    req = django_to_webob_request(request)

    try:
        if is_xblock_aside(usage_key):
            # Get the descriptor for the block being wrapped by the aside (not the aside itself)
            descriptor = modulestore().get_item(usage_key.usage_key)
            handler_descriptor = get_aside_from_xblock(descriptor,
                                                       usage_key.aside_type)
            asides = [handler_descriptor]
        else:
            descriptor = modulestore().get_item(usage_key)
            handler_descriptor = descriptor
            asides = []
        handler_descriptor.xmodule_runtime = StudioEditModuleRuntime(
            request.user)
        resp = handler_descriptor.handle(handler, req, suffix)
    except NoSuchHandlerError:
        log.info(u"XBlock %s attempted to access missing handler %r",
                 handler_descriptor,
                 handler,
                 exc_info=True)
        raise Http404

    # unintentional update to handle any side effects of handle call
    # could potentially be updating actual course data or simply caching its values
    modulestore().update_item(descriptor, request.user.id, asides=asides)
    log.info('xblock content is updated (course_id: {}, xblock_id: {})'.format(
        course_key_string, usage_key_string))
    return webob_to_django_response(resp)
示例#3
0
def component_handler(request, usage_key_string, handler, suffix=''):
    """
    Dispatch an AJAX action to an xblock

    Args:
        usage_id: The usage-id of the block to dispatch to
        handler (str): The handler to execute
        suffix (str): The remainder of the url to be passed to the handler

    Returns:
        :class:`django.http.HttpResponse`: The response from the handler, converted to a
            django response
    """
    usage_key = UsageKey.from_string(usage_key_string)

    # Let the module handle the AJAX
    req = django_to_webob_request(request)

    try:
        if is_xblock_aside(usage_key):
            # Get the descriptor for the block being wrapped by the aside (not the aside itself)
            descriptor = modulestore().get_item(usage_key.usage_key)
            handler_descriptor = get_aside_from_xblock(descriptor,
                                                       usage_key.aside_type)
            asides = [handler_descriptor]
        else:
            descriptor = modulestore().get_item(usage_key)
            handler_descriptor = descriptor
            asides = []
        handler_descriptor.xmodule_runtime = StudioEditModuleRuntime(
            request.user)
        resp = handler_descriptor.handle(handler, req, suffix)
    except NoSuchHandlerError:
        log.info(u"XBlock %s attempted to access missing handler %r",
                 handler_descriptor,
                 handler,
                 exc_info=True)
        raise Http404

    # unintentional update to handle any side effects of handle call
    # could potentially be updating actual course data or simply caching its values
    modulestore().update_item(descriptor, request.user.id, asides=asides)

    return webob_to_django_response(resp)
示例#4
0
def component_handler(request, usage_key_string, handler, suffix=''):
    """
    Dispatch an AJAX action to an xblock

    Args:
        usage_id: The usage-id of the block to dispatch to
        handler (str): The handler to execute
        suffix (str): The remainder of the url to be passed to the handler

    Returns:
        :class:`django.http.HttpResponse`: The response from the handler, converted to a
            django response
    """
    usage_key = UsageKey.from_string(usage_key_string)

    # Let the module handle the AJAX
    req = django_to_webob_request(request)

    try:
        if is_xblock_aside(usage_key):
            # Get the descriptor for the block being wrapped by the aside (not the aside itself)
            descriptor = modulestore().get_item(usage_key.usage_key)
            handler_descriptor = get_aside_from_xblock(descriptor, usage_key.aside_type)
            asides = [handler_descriptor]
        else:
            descriptor = modulestore().get_item(usage_key)
            handler_descriptor = descriptor
            asides = []
        handler_descriptor.xmodule_runtime = StudioEditModuleRuntime(request.user)
        resp = handler_descriptor.handle(handler, req, suffix)
    except NoSuchHandlerError:
        log.info(u"XBlock %s attempted to access missing handler %r", handler_descriptor, handler, exc_info=True)
        raise Http404

    # unintentional update to handle any side effects of handle call
    # could potentially be updating actual course data or simply caching its values
    modulestore().update_item(descriptor, request.user.id, asides=asides)

    return webob_to_django_response(resp)
示例#5
0
 def test_is_not_xblock_aside(self):
     """test if xblock is not aside"""
     assert is_xblock_aside(self.block.scope_ids.usage_id) is False
示例#6
0
 def test_is_xblock_aside(self):
     """test if xblock is aside"""
     assert is_xblock_aside(self.aside_v2) is True
     assert is_xblock_aside(self.aside_v1) is True
示例#7
0
 def test_is_not_xblock_aside(self):
     """test if xblock is not aside"""
     assert is_xblock_aside(self.block.scope_ids.usage_id) is False
示例#8
0
 def test_is_xblock_aside(self):
     """test if xblock is aside"""
     assert is_xblock_aside(self.aside_v2) is True
     assert is_xblock_aside(self.aside_v1) is True