representatives_extras.py 1.79 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
from django import template
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe

from representatives.models import Mandate, Group

register = template.Library()

@register.filter
def position_icon(position):
    if position == 'for':
        return mark_safe(
            '<i \
            aria-label="for" \
            class="fa fa-thumbs-up vote_positive" \
            title="for" \
            ></i>')
    elif position == 'against':
        return mark_safe(
            '<i \
            aria-label="against" \
            class="fa fa-thumbs-down vote_negative" \
            title="against" \
            ></i>')
    else:
        return mark_safe(
            '<i \
            aria-label="abstain" \
            class="fa fa-circle-o vote_abstain" \
            title="abstain" \
            ></i>')


@register.filter
def score_label(score):
    if score > 0:
        return mark_safe('<span class="label label-success">{}</span>'.format(score))
    elif score < 0:
        return mark_safe('<span class="label label-danger">{}</span>'.format(score))
    else:
        return mark_safe('<span class="label label-default">{}</span>'.format(score))


@register.filter
def country_flag(country):
    return mark_safe('{} <span class="flag-icon flag-icon-{}"></span>'.format(
        country.name,
        country.code.lower()
    ))


@register.filter
def by_group_url(group):
    if isinstance(group, Mandate):
        group = group.group

    if not isinstance(group, Group):
        return ''

    kwargs = {'group_kind': group.kind}

    if group.abbreviation:
        kwargs['group'] = group.abbreviation
    else:
        kwargs['group'] = group.name

    # kwargs['group_id'] = group.id

    return reverse(
        'legislature:representative-index',
        kwargs=kwargs
    )