Login

mask_email filter

Author:
jkocherhans
Posted:
March 3, 2007
Language:
Python
Version:
Pre .96
Score:
12 (after 12 ratings)

Mask an email address by removing most of the first portion and replacing it with "..."

For example. If you have a variable in your template context named email_address, and its value is "[email protected]"

{{ email_address|mask_email }}

will render as:

[email protected]

If the part preceding @domain.com is shorter than 5 characters, only the first letter will be used, followed by "...". So if we have "[email protected]"

{{ email_address|mask_email }}

will render as:

[email protected]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django import template

register = template.Library()

def mask_email(email):
    """Mask an email address."""
    name, domain = email.split('@')
    if len(name) > 5:
        # show the first 3 characters
        masked_name = name[:3]
    else:
        # just use the 1st character
        masked_name = name[0]
    return "%s[email protected]%s" % (masked_name, domain)

register.filter('mask_email', mask_email)

More like this

  1. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 2 months, 3 weeks ago
  2. Python Django CRUD Example Tutorial by tuts_station 3 months, 1 week ago
  3. Browser-native date input field by kytta 4 months, 3 weeks ago
  4. Generate and render HTML Table by LLyaudet 5 months ago
  5. My firs Snippets by GutemaG 5 months ago

Comments

jcroft (on March 3, 2007):

Best. Snippet. EVAR!

#

poelzi (on March 6, 2007):

very nice. i added the filter to djazz

#

Please login first before commenting.