Code

Ticket #2250: munge_email.diff

File munge_email.diff, 1.4 KB (added by anonymous, 8 years ago)

Patch implementing a template filter munge_email

Line 
1Index: django/template/defaultfilters.py
2===================================================================
3--- django/template/defaultfilters.py   (revision 3217)
4+++ django/template/defaultfilters.py   (working copy)
5@@ -63,6 +63,16 @@
6     """
7     return list(str(value))
8 
9+def munge_email(value):
10+    """Tries to hide email address from spambots.
11+
12+    Not handling unicode strings.
13+    """
14+    if isinstance(value, str):
15+        return ''.join('&#%d;' % ord(char) for char in value)
16+    else:
17+        return value
18+
19 def slugify(value):
20     "Converts to lowercase, removes non-alpha chars and converts spaces to hyphens"
21     value = re.sub('[^\w\s-]', '', value).strip().lower()
22@@ -485,6 +495,7 @@
23 register.filter(ljust)
24 register.filter(lower)
25 register.filter(make_list)
26+register.filter(munge_email)
27 register.filter(phone2numeric)
28 register.filter(pluralize)
29 register.filter(pprint)
30Index: docs/templates.txt
31===================================================================
32--- docs/templates.txt  (revision 3217)
33+++ docs/templates.txt  (working copy)
34@@ -938,6 +938,13 @@
35 Returns the value turned into a list. For an integer, it's a list of
36 digits. For a string, it's a list of characters.
37 
38+munge_email
39+~~~~~~~~~~
40+
41+Returns an encoded version of a string. Can be used to make it harder for
42+spambots to harvest e-mail addresses. For example, ``'a@b.com'`` gets
43+converted to ``'a@b.com'``.
44+
45 phone2numeric
46 ~~~~~~~~~~~~~
47