Login

mask_email filter

Author:
jkocherhans
Posted:
March 3, 2007
Language:
Python
Version:
Pre .96
Tags:
filters email
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 "someguy@example.com"

{{ email_address|mask_email }}

will render as:

som...@example.com

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

{{ email_address|mask_email }}

will render as:

e...@example.com
 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. format output as table by bendavis78 5 years, 7 months ago
  2. duplicate model object merging script by nstrite 7 years, 6 months ago
  3. Update only selected model fields by jcrocholl 7 years, 3 months ago
  4. Hide Emails by epicserve 6 years ago
  5. Render to email by bl4th3rsk1t3 5 years, 10 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.