import random
from django import template
class HideEmailNode(template.Node):
def __init__(self, name, email):
self.name = template.Variable(name)
self.email = template.Variable(email)
def render(self, context):
name = self.name.resolve(context)
email = self.email.resolve(context)
email_name, email_domain = str(email).split('@')
email_domain = '@'+email_domain
decoded_email_name = []
decoded_email_domain = []
for c in email_name:
d = ord(c)
x = random.randint(0, d)
decoded_email_name.append("%d+%d" % (x, d-x))
for c in email_domain:
d = ord(c)
x = random.randint(0, d)
decoded_email_domain.append("%d+%d" % (x, d-x))
return '%s' %\
(",".join(decoded_email_name), ",".join(decoded_email_domain), name)
def do_hide_email(parser, token):
"""Return mailto link with hidden href like this:
name
Usage example:
{% hide_email user user@dot.com %}
"""
bits = token.contents.split()
if len(bits) != 3:
raise template.TemplateSyntaxError, "'%s' tag takes three arguments" % bits[0]
# todo: check bit[2] is valid email
return HideEmailNode(bits[1], bits[2])
register.tag('hide_email', do_hide_email)