api.py 2.61 KB
Newer Older
1 2
"""
API entry point to the course_blocks app with top-level
3
get_course_blocks function.
4
"""
5
from openedx.core.djangoapps.content.block_structure.api import get_block_structure_manager
6
from openedx.core.lib.block_structure.transformers import BlockStructureTransformers
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

from .transformers import (
    library_content,
    start_date,
    user_partitions,
    visibility,
)
from .usage_info import CourseUsageInfo


# Default list of transformers for manipulating course block structures
# based on the user's access to the course blocks.
COURSE_BLOCK_ACCESS_TRANSFORMERS = [
    library_content.ContentLibraryTransformer(),
    start_date.StartDateTransformer(),
    user_partitions.UserPartitionTransformer(),
    visibility.VisibilityTransformer(),
]


def get_course_blocks(
        user,
29
        starting_block_usage_key,
30
        transformers=None,
31
        collected_block_structure=None,
32 33 34
):
    """
    A higher order function implemented on top of the
35
    block_structure.get_blocks function returning a transformed block
36
    structure for the given user starting at starting_block_usage_key.
37 38 39 40 41

    Arguments:
        user (django.contrib.auth.models.User) - User object for
            which the block structure is to be transformed.

42 43
        starting_block_usage_key (UsageKey) - Specifies the starting block
            of the block structure that is to be transformed.
44

45
        transformers (BlockStructureTransformers) - A collection of
46 47 48
            transformers whose transform methods are to be called.
            If None, COURSE_BLOCK_ACCESS_TRANSFORMERS is used.

49 50 51 52 53
        collected_block_structure (BlockStructureBlockData) - A
            block structure retrieved from a prior call to
            BlockStructureManager.get_collected.  Can be optionally
            provided if already available, for optimization.

54 55
    Returns:
        BlockStructureBlockData - A transformed block structure,
56
            starting at starting_block_usage_key, that has undergone the
57 58 59 60 61 62
            transform methods for the given user and the course
            associated with the block structure.  If using the default
            transformers, the transformed block structure will be
            exactly equivalent to the blocks that the given user has
            access.
    """
63 64
    if not transformers:
        transformers = BlockStructureTransformers(COURSE_BLOCK_ACCESS_TRANSFORMERS)
65
    transformers.usage_info = CourseUsageInfo(starting_block_usage_key.course_key, user)
66

67
    return get_block_structure_manager(starting_block_usage_key.course_key).get_transformed(
68 69
        transformers,
        starting_block_usage_key,
70
        collected_block_structure,
71
    )