Ejemplo n.º 1
0
    def get_template(self, uri):
        """
        Override of the base class for us to look into the
        database tables for a template definition, if we can't find
        one we'll return None which means "use default means" (aka filesystem)
        """
        cache_key = "template_cache." + fasthash(
            microsite_get_value('site_domain') + '.' + uri)
        template_text = cache.get(cache_key)  # pylint: disable=maybe-no-member

        if not template_text:
            # cache is empty so pull template from DB and fill cache.
            template_obj = MicrositeTemplate.get_template_for_microsite(
                microsite_get_value('site_domain'), uri)

            if not template_obj:
                # We need to set something in the cache to improve performance
                # of the templates stored in the filesystem as well
                cache.set(  # pylint: disable=maybe-no-member
                    cache_key, '##none',
                    settings.MICROSITE_DATABASE_TEMPLATE_CACHE_TTL)
                return None

            template_text = template_obj.template
            cache.set(  # pylint: disable=maybe-no-member
                cache_key, template_text,
                settings.MICROSITE_DATABASE_TEMPLATE_CACHE_TTL)

        if template_text == '##none':
            return None

        return Template(text=template_text)
Ejemplo n.º 2
0
    def get_template(self, uri):
        """
        Override of the base class for us to look into the
        database tables for a template definition, if we can't find
        one we'll return None which means "use default means" (aka filesystem)
        """
        cache_key = "template_cache." + fasthash(microsite_get_value('site_domain') + '.' + uri)
        template_text = cache.get(cache_key)  # pylint: disable=maybe-no-member

        if not template_text:
            # cache is empty so pull template from DB and fill cache.
            template_obj = MicrositeTemplate.get_template_for_microsite(
                microsite_get_value('site_domain'),
                uri
            )

            if not template_obj:
                # We need to set something in the cache to improve performance
                # of the templates stored in the filesystem as well
                cache.set(  # pylint: disable=maybe-no-member
                    cache_key, '##none', settings.MICROSITE_DATABASE_TEMPLATE_CACHE_TTL
                )
                return None

            template_text = template_obj.template
            cache.set(  # pylint: disable=maybe-no-member
                cache_key, template_text, settings.MICROSITE_DATABASE_TEMPLATE_CACHE_TTL
            )

        if template_text == '##none':
            return None

        return Template(
            text=template_text
        )
Ejemplo n.º 3
0
def cached_has_permission(user, permission, course_id=None):
    """
    Call has_permission if it's not cached. A change in a user's role or
    a role's permissions will only become effective after CACHE_LIFESPAN seconds.
    """
    CACHE_LIFESPAN = 60
    key = "permission_%d_%s_%s" % (user.id, str(course_id), permission)
    val = cache.get(key, None)
    if val not in [True, False]:
        val = has_permission(user, permission, course_id=course_id)
        cache.set(key, val, CACHE_LIFESPAN)
    return val
Ejemplo n.º 4
0
def cached_has_permission(user, permission, course_id=None):
    """
    Call has_permission if it's not cached. A change in a user's role or
    a role's permissions will only become effective after CACHE_LIFESPAN seconds.
    """
    CACHE_LIFESPAN = 60
    key = "permission_%d_%s_%s" % (user.id, str(course_id), permission)
    val = cache.get(key, None)
    if val not in [True, False]:
        val = has_permission(user, permission, course_id=course_id)
        cache.set(key, val, CACHE_LIFESPAN)
    return val
Ejemplo n.º 5
0
def user_groups(user):
    """
    TODO (vshnayder): This is not used. When we have a new plan for groups, adjust appropriately.
    """
    if not user.is_authenticated():
        return []

    # TODO: Rewrite in Django
    key = 'user_group_names_{user.id}'.format(user=user)
    cache_expiration = 60 * 60  # one hour

    # Kill caching on dev machines -- we switch groups a lot
    group_names = cache.get(key)
    if settings.DEBUG:
        group_names = None

    if group_names is None:
        group_names = [u.name for u in UserTestGroup.objects.filter(users=user)]
        cache.set(key, group_names, cache_expiration)

    return group_names
Ejemplo n.º 6
0
def user_groups(user):
    """
    TODO (vshnayder): This is not used. When we have a new plan for groups, adjust appropriately.
    """
    if not user.is_authenticated():
        return []

    # TODO: Rewrite in Django
    key = 'user_group_names_{user.id}'.format(user=user)
    cache_expiration = 60 * 60  # one hour

    # Kill caching on dev machines -- we switch groups a lot
    group_names = cache.get(key)
    if settings.DEBUG:
        group_names = None

    if group_names is None:
        group_names = [u.name for u in UserTestGroup.objects.filter(users=user)]
        cache.set(key, group_names, cache_expiration)

    return group_names
def _set_value_in_cache(key_name, value):
    cache.set(key_name, json.dumps(value), NOTIFICATION_CACHE_TIME)
def _set_value_in_cache(key_name, value):
    cache.set(key_name, json.dumps(value), NOTIFICATION_CACHE_TIME)
Ejemplo n.º 9
0
def course_structure(course_key, block_types=None):
    """
    Retrieves the entire course structure, including information about all the blocks used in the
    course if `block_types` is None else information about `block_types` will be returned only.
    Final serialized information will be cached.

    Args:
        course_key: the CourseKey of the course we'd like to retrieve.
        block_types: list of required block types. Possible values include sequential,
                     vertical, html, problem, video, and discussion. The type can also be
                     the name of a custom type of block used for the course.
    Returns:
        The serialized output of the course structure:
            * root: The ID of the root node of the course structure.

            * blocks: A dictionary that maps block IDs to a collection of
            information about each block. Each block contains the following
            fields.

                * id: The ID of the block.

                * type: The type of block. Possible values include sequential,
                    vertical, html, problem, video, and discussion. The type can also be
                    the name of a custom type of block used for the course.

                * display_name: The display name configured for the block.

                * graded: Whether or not the sequential or problem is graded. The
                    value is true or false.

                * format: The assignment type.

                * children: If the block has child blocks, a list of IDs of the child
                blocks.
    Raises:
        CourseStructureNotAvailableError, CourseNotFoundError

    """
    course = _retrieve_course(course_key)

    modified_timestamp = models.CourseStructure.objects.filter(course_id=course_key).values('modified')
    if modified_timestamp.exists():
        cache_key = 'openedx.content.course_structures.api.v0.api.course_structure.{}.{}.{}'.format(
            course_key, modified_timestamp[0]['modified'], '_'.join(block_types or [])
        )
        data = cache.get(cache_key)  # pylint: disable=maybe-no-member
        if data is not None:
            return data

        try:
            requested_course_structure = models.CourseStructure.objects.get(course_id=course.id)
        except models.CourseStructure.DoesNotExist:
            pass
        else:
            structure = requested_course_structure.structure
            if block_types is not None:
                blocks = requested_course_structure.ordered_blocks
                required_blocks = OrderedDict()
                for usage_id, block_data in blocks.iteritems():
                    if block_data['block_type'] in block_types:
                        required_blocks[usage_id] = block_data

                structure['blocks'] = required_blocks

            data = CourseStructureSerializer(structure).data
            cache.set(cache_key, data, None)  # pylint: disable=maybe-no-member
            return data

    # If we don't have data stored, generate it and return an error.
    tasks.update_course_structure.delay(unicode(course_key))
    raise CourseStructureNotAvailableError
Ejemplo n.º 10
0
def course_structure(course_key, block_types=None):
    """
    Retrieves the entire course structure, including information about all the blocks used in the
    course if `block_types` is None else information about `block_types` will be returned only.
    Final serialized information will be cached.

    Args:
        course_key: the CourseKey of the course we'd like to retrieve.
        block_types: list of required block types. Possible values include sequential,
                     vertical, html, problem, video, and discussion. The type can also be
                     the name of a custom type of block used for the course.
    Returns:
        The serialized output of the course structure:
            * root: The ID of the root node of the course structure.

            * blocks: A dictionary that maps block IDs to a collection of
            information about each block. Each block contains the following
            fields.

                * id: The ID of the block.

                * type: The type of block. Possible values include sequential,
                    vertical, html, problem, video, and discussion. The type can also be
                    the name of a custom type of block used for the course.

                * display_name: The display name configured for the block.

                * graded: Whether or not the sequential or problem is graded. The
                    value is true or false.

                * format: The assignment type.

                * children: If the block has child blocks, a list of IDs of the child
                blocks.
    Raises:
        CourseStructureNotAvailableError, CourseNotFoundError

    """
    course = _retrieve_course(course_key)

    modified_timestamp = models.CourseStructure.objects.filter(course_id=course_key).values('modified')
    if modified_timestamp.exists():
        cache_key = 'openedx.content.course_structures.api.v0.api.course_structure.{}.{}.{}'.format(
            course_key, modified_timestamp[0]['modified'], '_'.join(block_types or [])
        )
        data = cache.get(cache_key)  # pylint: disable=maybe-no-member
        if data is not None:
            return data

        try:
            requested_course_structure = models.CourseStructure.objects.get(course_id=course.id)
        except models.CourseStructure.DoesNotExist:
            pass
        else:
            structure = requested_course_structure.structure
            if block_types is not None:
                blocks = requested_course_structure.ordered_blocks
                required_blocks = OrderedDict()
                for usage_id, block_data in blocks.iteritems():
                    if block_data['block_type'] in block_types:
                        required_blocks[usage_id] = block_data

                structure['blocks'] = required_blocks

            data = CourseStructureSerializer(structure).data
            cache.set(cache_key, data, None)  # pylint: disable=maybe-no-member
            return data

    # If we don't have data stored, generate it and return an error.
    tasks.update_course_structure.delay(unicode(course_key))
    raise CourseStructureNotAvailableError