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...@%s" % (masked_name, domain)

register.filter('mask_email', mask_email)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 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.