Mercurial > sat_legacy_website
view sat_website/templatetags/utils.py @ 35:10ce34ab389a
dummy patch (just add some new lines to make space between the blocks)
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 27 Jan 2015 08:16:27 +0100 |
parents | 9d553570cc61 |
children | a18800261cf6 |
line wrap: on
line source
from django import template from django.forms import RadioSelect, CheckboxInput 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