Exemple #1
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)

    descriptor = modulestore().get_item(usage_key)
    descriptor.xmodule_runtime = StudioEditModuleRuntime(request.user)
    # Let the module handle the AJAX
    req = django_to_webob_request(request)

    try:
        resp = descriptor.handle(handler, req, suffix)

    except NoSuchHandlerError:
        log.info("XBlock %s attempted to access missing handler %r", 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)

    return webob_to_django_response(resp)
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)