- Author:
- nitinhayaran
- Posted:
- February 2, 2010
- Language:
- Python
- Version:
- 1.1
- Score:
- 3 (after 3 ratings)
This is a Django Template Tag to protect the E-mail address you publish on your website against bots or spiders that index or harvest E-mail. It uses a substitution cipher with a different key for every page load.
It basically produce a equivalent Javascript code for an email address. This code when executed by browser make it mailto link.
Usage
{% encrypt_email user.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 41 42 43 44 45 | import re
from django import template
register = template.Library()
class EncryptEmail(template.Node):
def __init__(self, context_var):
self.context_var = template.Variable(context_var)# context_var
def render(self, context):
import random
email_address = self.context_var.resolve(context)
character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
char_list = list(character_set)
random.shuffle(char_list)
key = ''.join(char_list)
cipher_text = ''
id = 'e' + str(random.randrange(1,999999999))
for a in email_address:
cipher_text += key[ character_set.find(a) ]
script = 'var a="'+key+'";var b=a.split("").sort().join("");var c="'+cipher_text+'";var d="";'
script += 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));'
script += 'document.getElementById("'+id+'").innerHTML="<a href=\\"mailto:"+d+"\\">"+d+"</a>"'
script = "eval(\""+ script.replace("\\","\\\\").replace('"','\\"') + "\")"
script = '<script type="text/javascript">/*<![CDATA[*/'+script+'/*]]>*/</script>'
return '<span id="'+ id + '">[javascript protected email address]</span>'+ script
def encrypt_email(parser, token):
"""
{% encrypt_email user.email %}
"""
tokens = token.contents.split()
if len(tokens)!=2:
raise template.TemplateSyntaxError("%r tag accept two argument" % tokens[0])
return EncryptEmail(tokens[1])
register.tag('encrypt_email', encrypt_email)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
https://djangosnippets.org/snippets/10482/
#
Please login first before commenting.