Template Tag to protect the E-mail address

 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

  1. Simple e-mail template tag by dchandek 5 years, 11 months ago
  2. email_links by sansmojo 6 years, 10 months ago
  3. Validation for full e-mails (e.g. "Joe Hacker <joe@hacker.com>") by akaihola 2 years, 3 months ago
  4. Protect anti robots template tag by marinho 5 years, 11 months ago
  5. Hide Emails by epicserve 5 years, 1 month ago

Comments

(Forgotten your password?)