memopol_tags.py 4.55 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.core.urlresolvers import reverse
Nicolas Joyard's avatar
Nicolas Joyard committed
8
from django.utils.safestring import mark_safe
9
from django.utils.html import escape
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
20
21
22
23
def fix_url(url):
    # Ensure we have a usable URL
    return re.sub('^(https?://)?', 'https://', url.strip())


Nicolas Joyard's avatar
Nicolas Joyard committed
24
25
@register.filter
def twitter_link(url):
26
27
28
    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
29
30
31
32


@register.filter
def facebook_link(url):
33
34
35
36
37
    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
38

Nicolas Joyard's avatar
Nicolas Joyard committed
39
40
41

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


@register.filter
def email_link(address):
    return mark_safe(link.format(network='email', url='mailto:%s' % address,
51
        label=address))
52
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


@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
    }))
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103


@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(
        '<span class="flag-icon flag-icon-{code}"></span> {name}'.format(
            name=country.name,
            code=code.lower()))


@register.filter
def chamber_icon(chamber):
    return mark_safe(
        u'<span class="chamber-icon ' +
        u'chamber-icon-{abbr}"></span> {name}'.format(
            name=chamber.name,
            abbr=cssify(chamber.abbreviation)))


104
105
106
107
108
109
110
111
112
@register.filter
def chamber_small_icon(chamber):
    return mark_safe(
        u'<span class="chamber-icon ' +
        u'chamber-icon-{abbr}" title="{name}"></span>'.format(
            name=chamber.name,
            abbr=cssify(chamber.abbreviation)))


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
@register.filter
def mandate_icon(main_mandate):
    return mark_safe(
        u'<span class="group-icon ' +
        u'group-icon-{abbr}"></span> {role} of {name}'.format(
            role=main_mandate.role,
            name=main_mandate.group.name,
            abbr=cssify(main_mandate.group.abbreviation)))


@register.filter
def group_icon(group):
    return mark_safe(
        u'<span class="group-icon ' +
        u'group-icon-{abbr}"></span> {name}'.format(
            abbr=cssify(group.abbreviation),
            name=group.abbreviation))


@register.filter
def group_long_icon(group):
    return mark_safe(
        u'<span class="group-icon ' +
        u'group-icon-{abbr}"></span> {name}'.format(
            abbr=cssify(group.abbreviation),
            name=group.name))


@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
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>')