memopol_tags.py 6.18 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())


Nicolas Joyard's avatar
Nicolas Joyard committed
25
26
@register.filter
def twitter_link(url):
27
28
29
    furl = fix_url(url)
    return mark_safe(link.format(network='twitter', url=furl,
        label=re.sub(r'.*/@?([^/]+)', '@\\1', re.sub(r'/$', '', furl))))
Nicolas Joyard's avatar
Nicolas Joyard committed
30
31
32
33


@register.filter
def facebook_link(url):
34
35
36
37
38
    furl = fix_url(url)
    clean_url = re.sub(r'/$', '', re.sub(r'\?.*', '', furl))
    m = re.search(r'/pages/([^/]+)', clean_url, re.I)
    return mark_safe(link.format(network='facebook', url=furl,
        label=m.group(1) if m else re.sub(r'.*/([^/]+)', '\\1', clean_url)))
Nicolas Joyard's avatar
Nicolas Joyard committed
39

Nicolas Joyard's avatar
Nicolas Joyard committed
40
41
42

@register.filter
def website_link(url):
43
44
45
    furl = fix_url(url)
    short_url = re.sub(r'^https?://([^/]+).*', '\\1', furl)
    return mark_safe(link.format(network='website', url=furl,
46
        label=short_url))
Nicolas Joyard's avatar
Nicolas Joyard committed
47
48
49
50
51


@register.filter
def email_link(address):
    return mark_safe(link.format(network='email', url='mailto:%s' % address,
52
        label=address))
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83


@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
    }))
84
85
86
87
88
89
90


@register.filter
def country_flag(country):
    # Enable using groups instead of countries
    code = country.code if hasattr(country, 'code') else country.abbreviation
    return mark_safe(
91
        '<span class="flag-icon flag-icon-{code}"></span>'.format(
92
93
94
95
96
            code=code.lower()))


@register.filter
def chamber_icon(chamber):
97
    url = static('images/chamber-%s.png' % cssify(chamber.abbreviation))
98
    return mark_safe(
Nicolas Joyard's avatar
Nicolas Joyard committed
99
        u'<span class="chamber-icon" style="background-image: url({url})">'
100
        u'</span>'.format(name=chamber.name, url=url))
101
102


103
104
@register.filter
def chamber_small_icon(chamber):
105
    url = static('images/chamber-%s.png' % cssify(chamber.abbreviation))
106
    return mark_safe(
107
108
        u'<span class="chamber-icon" style="background-image: url({url})" '
        u'title="{name}"></span>'.format(name=chamber.name, url=url))
109
110


111
112
113
114
115
116
117
118
119
120
@register.simple_tag
def chamber_page_link(site, name):
    icon_url = static('images/chamber-%s.png' % cssify(site.kind))
    site_url = fix_url(site.url)
    return mark_safe(
        u'<span class="chamber-icon" style="background-image: url({icon})" '
        u'></span> <a href="{site}" target="_blank">{name}</a>'
        .format(name=name, site=site_url, icon=icon_url))


121
122
@register.filter
def mandate_icon(main_mandate):
123
124
125
    group = main_mandate.group
    url = static('images/group-%s.png' % cssify('%s-%s' % (
        group.chamber.abbreviation, group.abbreviation)))
126
    return mark_safe(
127
128
        u'<span class="group-icon" style="background-image: url({url})">'
        u'</span> {role} of {name}'.format(
129
130
            role=main_mandate.role,
            name=main_mandate.group.name,
131
            url=url))
132
133
134
135


@register.filter
def group_icon(group):
136
137
    url = static('images/group-%s.png' % cssify('%s-%s' % (
        group.chamber.abbreviation, group.abbreviation)))
138
    return mark_safe(
139
        u'<span class="group-icon" style="background-image: url({url})">'
140
        u'</span>'.format(url=url, name=group.abbreviation))
141
142
143
144


@register.filter
def group_long_icon(group):
145
146
    url = static('images/group-%s.png' % cssify('%s-%s' % (
        group.chamber.abbreviation, group.abbreviation)))
147
    return mark_safe(
148
149
        u'<span class="group-icon" style="background-image: url({url})">'
        u'</span> {name}'.format(url=url, name=group.name))
150
151
152
153
154
155
156
157
158
159
160


@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
161
162
163
164
165
166
167
168
def position_icon(position, recommendation=None):
    color = 'default'
    if recommendation:
        if position == recommendation:
            color = 'success'
        else:
            color = 'danger'

169
    if position == 'for':
170
        icon = "thumbs-up"
171
    elif position == 'against':
172
        icon = "thumbs-down"
173
    else:
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        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
195
196


Nicolas Joyard's avatar
Nicolas Joyard committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
@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
215
216
217
@register.filter
def cast_str(val):
    return str(val)