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

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


@register.filter
def twitter_link(url):
    return mark_safe(link.format(network='twitter', url=url,
17
        label=re.sub(r'.*/@?([^/]+)', '@\\1', re.sub(r'/$', '', url.strip()))))
Nicolas Joyard's avatar
Nicolas Joyard committed
18
19
20
21
22


@register.filter
def facebook_link(url):
    return mark_safe(link.format(network='facebook', url=url,
23
        label=re.sub(r'.*/([^/]+)', '\\1', re.sub(r'/$', '', url.strip()))))
Nicolas Joyard's avatar
Nicolas Joyard committed
24

Nicolas Joyard's avatar
Nicolas Joyard committed
25
26
27
28
29

@register.filter
def website_link(url):
    short_url = re.sub(r'^https?://([^/]+).*', '\\1', url)
    return mark_safe(link.format(network='website', url=url,
30
        label=short_url))
Nicolas Joyard's avatar
Nicolas Joyard committed
31
32
33
34
35


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


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