memopol_tags.py 3.34 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1
# coding: utf-8
2
3
4

import re

Nicolas Joyard's avatar
Nicolas Joyard committed
5
from django import template
6
from django.contrib.humanize.templatetags.humanize import naturalday
7
from django.contrib.staticfiles.templatetags.staticfiles import static
Nicolas Joyard's avatar
Nicolas Joyard committed
8
from django.utils.safestring import mark_safe
9

Nicolas Joyard's avatar
Nicolas Joyard committed
10
11

register = template.Library()
12
13
14
15
16
link = u'<a class="{network}-link" href="{url}" target="_blank">{label}</a>'


def cssify(string):
    return re.sub('[^a-z_-]', '', string.lower())
Nicolas Joyard's avatar
Nicolas Joyard committed
17
18


19
@register.filter
20
def country_flag(country, tplace='bottom'):
21
22
23
    # Enable using groups instead of countries
    code = country.code if hasattr(country, 'code') else country.abbreviation
    return mark_safe(
24
25
26
        u'<span class="flag-icon flag-icon-{code}" title="{name}"'
        u' data-toggle="tooltip" data-placement="{place}"></span>'.format(
            code=code.lower(), place=tplace, name=country.name))
27
28
29


@register.filter
Nicolas Joyard's avatar
Nicolas Joyard committed
30
def chamber_icon(chamber, tplace='bottom'):
31
    url = static('images/chamber-%s.png' % cssify(chamber.abbreviation))
32
    return mark_safe(
Nicolas Joyard's avatar
Nicolas Joyard committed
33
34
35
36
37
38
39
40
        u'<span class="chamber-icon" style="background-image: url({url})"'
        u' data-toggle="tooltip" data-placement="{place}"'
        u' title="{name}"></span>'.format(
            name=chamber.name,
            url=url,
            place=tplace
        )
    )
41
42


43
@register.filter
44
def group_icon(group, tplace='bottom'):
45
46
    url = static('images/group-%s.png' % cssify('%s-%s' % (
        group.chamber.abbreviation, group.abbreviation)))
47
    return mark_safe(
48
49
50
        u'<span class="group-icon" style="background-image: url({url})"'
        u' data-toggle="tooltip" data-placement="{place}" title="{name}">'
        u'</span>'.format(url=url, name=group.name, place=tplace))
51
52
53
54
55
56
57
58
59
60
61


@register.filter
def mandate_date(date, arg=None):
    if date is None or date.year == 9999:
        return 'present'
    else:
        return naturalday(date, arg)


@register.filter
62
63
64
65
66
67
68
69
def position_icon(position, recommendation=None):
    color = 'default'
    if recommendation:
        if position == recommendation:
            color = 'success'
        else:
            color = 'danger'

70
    if position == 'for':
71
        icon = "thumbs-up"
72
    elif position == 'against':
73
        icon = "thumbs-down"
74
    else:
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        icon = "circle-o"

    pattern = '<i class="fa fa-%s text-%s" title="%s"></i>'
    return mark_safe(pattern % (icon, color, position))


@register.filter
def proposal_status_label(status, recommendation=None):
    color = 'default'
    if recommendation:
        reco = recommendation.recommendation

        if (reco == 'for' and status == 'adopted' or
           reco == 'against' and status == 'rejected'):
            color = 'success'
        elif (reco == 'for' and status == 'rejected' or
              reco == 'against' and status == 'adopted'):
            color = 'danger'

    pattern = '<span class="label label-%s">%s</span>'
    return mark_safe(pattern % (color, status))
Robin's avatar
Robin committed
96
97


Nicolas Joyard's avatar
Nicolas Joyard committed
98
99
@register.filter
def score_badge(score, tooltip=None):
100
101
    score = int(score or 0)

Nicolas Joyard's avatar
Nicolas Joyard committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    if score > 0:
        color = 'success'
    elif score < 0:
        color = 'danger'
    else:
        color = 'primary'

    attrs = ''
    if tooltip:
        attrs = 'data-toggle="tooltip" data-placement="%s" title="%s"'
        attrs = attrs % ('left', tooltip)

    pattern = '<span class="badge badge-%s" %s>%s</span>'
    return mark_safe(pattern % (color, attrs, score))


Robin's avatar
Robin committed
118
119
120
@register.filter
def cast_str(val):
    return str(val)