view sat_website/templatetags/utils.py @ 29:b45621706d83

use Bootstrap carousels to display images and videos galeries: - one carousel for a 3x3 thumbnails grid - a second one in a modal window to view bigger pictures when a thumbnail is clicked
author souliane <souliane@mailoo.org>
date Wed, 21 Jan 2015 20:13:19 +0100
parents eda4deefecd1
children 9d553570cc61
line wrap: on
line source

from django import template
register = template.Library()

@register.filter
def is_tuple(value):
    """Tell if this value is a tuple or not.

    @param value (obj): any object
    @return: True if value is a tuple
    """
    # XXX: we would like to have is_type with an argument instead, but it would
    # rely on a strange comparison - isinstance(value, arg) doesn't work here
    return type(value) == tuple

@register.filter
def buffer(value, n):
    """Split values in sub-lists of n elements.

    @param value (list): a list object
    @return: a list containing sub-lists

    For example:

    >>> buffer(range(10), 2)
    [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

    >>> buffer(range(10), 3)
    [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

    >>> buffer(range(10), 4)
    [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
    """
    result = []
    index = 0
    while index < len(value):
        result.append(value[index:index + n])
        index += n
    return result