Created
November 21, 2017 10:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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