summaryrefslogtreecommitdiffstats
path: root/mirrors/templatetags/jinja2.py
blob: 5d47fe9bd54a3c15d1bb0e6a2b903f24bf7c128e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from django_jinja import library
from markupsafe import Markup


@library.global_function
def country_flag(country):
    if not country:
        return ''
    html = '<span class="fam-flag fam-flag-%s" title="%s"></span> ' % (
            unicode(country.code).lower(), unicode(country.name))
    return Markup(html)


@library.filter
def duration(value):
    if not value and type(value) != timedelta:
        return u''
    # does not take microseconds into account
    total_secs = value.seconds + value.days * 24 * 3600
    mins = total_secs // 60
    hrs, mins = divmod(mins, 60)
    return '%d:%02d' % (hrs, mins)


@library.filter
def floatvalue(value, arg=2):
    if value is None:
        return u''
    return '%.*f' % (arg, value)

# vim: set ts=4 sw=4 et: