view sat_website/templatetags/utils.py @ 93:9ae3d9c8b28a

actually display the subtitles (with Firefox it is used only when it's the first <track /> of the list, and when the "default" option is present)
author souliane <souliane@mailoo.org>
date Tue, 16 Jun 2015 21:26:58 +0200
parents 5de2a3dd4e67
children c56b6ad62153
line wrap: on
line source

from django import template
from django.forms import RadioSelect, CheckboxInput
from django.utils.translation import get_language, ugettext_lazy as _
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 is_radio(value):
    """Tell if this value is a RadioSelect or not.

    @param value (obj): any object
    @return: True if value is a RadioSelect
    """
    return type(value) == RadioSelect

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

    @param value (obj): any object
    @return: True if value is a CheckboxInput
    """
    return type(value) == CheckboxInput

@register.filter
def selected_label(select):
    """Return the label of the single selected option.
    
    @param select: a BoundField object bound to a Select with single choice.
    @return: unicode
    """
    for value, label in select.field.choices:
        if value == select.value():
            return label
    return None

@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

@register.filter
def select_video_path(paths):
    """Return the video path matching the current language.

    @param paths (dict): dict of video paths.
    @return: unicode
    """
    lang = get_language()
    if lang in paths:
        return paths[lang]
    return paths.values()[0]

@register.filter
def metadata(entry):
    """Return a string representation of the video metadata.

    @param entry (VideoDesc): video entry
    @return: unicode
    """
    sep = _(': ')
    lang = get_language()

    def get_item(key, value):
        return "%s%s%s" % (key, sep, value)
    
    items = []
    for key, value in entry.data.iteritems():
        if key == "subtitles":  # is used as another dict key in media.py
            key = _(u"subtitles")
        items.append(get_item(key.translate(lang), value))
    try:
        lang_key = _(u"language").translate(lang)
        lang_value = lang if lang in entry.paths else entry.paths.keys()[0]
        items.append(get_item(lang_key, lang_value))
    except AttributeError:
        pass
    return "(%s)" % ', '.join(items)