hide_email

 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
40
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 '<a href="mailto:noreply@spammer.net" onmouseover="var a=String.fromCharCode(%s);var b=String.fromCharCode(%s);this.href=[\'mail\',\'to:\',a,b].join(\'\');">%s</a>' %\
			   (",".join(decoded_email_name), ",".join(decoded_email_domain), name)

def do_hide_email(parser, token):
	"""Return mailto link with hidden href like this:
	<a href="mailto:noreply@spammer.net" onmouseover="var a='@';a+='dot.';a+='com';var b='user';this.href=['mail','to:',b,a].join('');">name</a>
	
	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)

More like this

  1. encode_mailto by santuri 4 years, 9 months ago
  2. packjs templatetag by kyprizel 4 years, 1 month ago
  3. Hide Emails by epicserve 2 years, 11 months ago
  4. Integer list to pattern function by marinho 4 years, 2 months ago
  5. Pattern to integer list function by marinho 4 years, 2 months ago

Comments

(Forgotten your password?)