forked from hacktoolkit/django-htk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlizer.py
39 lines (33 loc) · 989 Bytes
/
urlizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import base64
from django import template
from django.urls import reverse
from django.utils.safestring import mark_safe
from htk.utils import htk_setting
register = template.Library()
@register.simple_tag
def redir_url(url):
redir_view = htk_setting('HTK_REDIRECT_URL_NAME')
url = '%(path)s?url=%(url)s' % {
'path' : reverse(redir_view),
'url' : base64.urlsafe_b64encode(url),
}
return url
@register.simple_tag
def redir(url, text=None, target='_blank'):
"""Links to a redirect page
"""
text = text or url
html = '<a href="%(url)s" target="%(target)s">%(text)s</a>' % {
'url' : redir_url(url),
'text' : text,
'target' : target,
}
html = mark_safe(html)
return html
@register.simple_tag
def redir_trunc(url, length, target='_blank'):
"""Links to a redirect page
"""
text = url[:length] + '...' if len(url) > length else url
html = redir(url, text=text, target=target)
return html