memopol_tags.py 4.28 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
8
from django.core.urlresolvers import reverse
Nicolas Joyard's avatar
Nicolas Joyard committed
9
from django.utils.safestring import mark_safe
10
from django.utils.html import escape
Nicolas Joyard's avatar
Nicolas Joyard committed
11
12

register = template.Library()
13
14
15
16
17
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
18
19


20
21
22
23
24
def fix_url(url):
    # Ensure we have a usable URL
    return re.sub('^(https?://)?', 'https://', url.strip())


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
@register.simple_tag
def group_url(group):
    if group.kind == 'chamber' or group.chamber is None:
        return escape(reverse('representative-list', kwargs={
            'group_kind': group.kind,
            'group': group.name
        }))
    else:
        return escape(reverse('representative-list', kwargs={
            'group_kind': group.kind,
            'chamber': group.chamber.name,
            'group': group.name
        }))


@register.simple_tag
def chamber_url(chamber):
    return escape(reverse('representative-list', kwargs={
        'group_kind': 'chamber',
        'group': chamber.name
    }))


@register.simple_tag
def country_url(country):
    return escape(reverse('representative-list', kwargs={
        'group_kind': 'country',
        'group': country.name
    }))
54
55
56


@register.filter
57
def country_flag(country, tplace='bottom'):
58
59
60
    # Enable using groups instead of countries
    code = country.code if hasattr(country, 'code') else country.abbreviation
    return mark_safe(
61
62
63
        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))
64
65
66


@register.filter
Nicolas Joyard's avatar
Nicolas Joyard committed
67
def chamber_icon(chamber, tplace='bottom'):
68
    url = static('images/chamber-%s.png' % cssify(chamber.abbreviation))
69
    return mark_safe(
Nicolas Joyard's avatar
Nicolas Joyard committed
70
71
72
73
74
75
76
77
        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
        )
    )
78
79


80
@register.filter
81
def group_icon(group, tplace='bottom'):
82
83
    url = static('images/group-%s.png' % cssify('%s-%s' % (
        group.chamber.abbreviation, group.abbreviation)))
84
    return mark_safe(
85
86
87
        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))
88
89
90
91
92
93
94
95
96
97
98


@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
99
100
101
102
103
104
105
106
def position_icon(position, recommendation=None):
    color = 'default'
    if recommendation:
        if position == recommendation:
            color = 'success'
        else:
            color = 'danger'

107
    if position == 'for':
108
        icon = "thumbs-up"
109
    elif position == 'against':
110
        icon = "thumbs-down"
111
    else:
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        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
133
134


Nicolas Joyard's avatar
Nicolas Joyard committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
@register.filter
def score_badge(score, tooltip=None):
    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
153
154
155
@register.filter
def cast_str(val):
    return str(val)