Skip to content

Instantly share code, notes, and snippets.

@mammuth
Created November 21, 2017 10:21
Show Gist options
  • Save mammuth/71f8be53b0952a27bb8d3b9a568f334d to your computer and use it in GitHub Desktop.
Save mammuth/71f8be53b0952a27bb8d3b9a568f334d to your computer and use it in GitHub Desktop.
Methods for obfuscating E-Mail addresses via HTML comments and JavaScript to prevent some spam in Django
"""
Requires django (mark_safe and escape)
Add the following two properties to your model (and update the naming of the email field)
In your templates: <a href="{{ person.email_obfuscated_href }}">{{ person.email_obfuscated_name }}</a>
Result: <a href="javascript:window.location.href = 'mailto:' + ['max', 'mustermann.com'].join('@')">max<!---->@<!---->mustermann.com</a>
Inspired by https://github.com/Blueshoe/djangocms-link2
"""
@property
def email_obfuscated_href(self):
name, domain = self.email.split('@')
return 'javascript:window.location.href = \'mailto:\' + [\'{}\', \'{}\'].join(\'@\')'.format(name, domain)
@property
def email_obfuscated_name(self):
name, domain = self.email.split('@')
return mark_safe('{}<!---->@<!---->{}'.format(escape(name), escape(domain)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment