-
-
Save brianarn/1711795 to your computer and use it in GitHub Desktop.
Preventing comment spam
This file contains 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
COMMENTS_SECRET = 'any_old_string_you_like' |
This file contains 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
<input type="hidden" name="comment-secret" id="id_comment-secret" /> | |
<textarea id="id_comment-body" rows="10" cols="40" name="comment-body"></textarea> |
This file contains 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
<script type="text/javascript"> | |
(function($){ | |
// The elements we'll be working with | |
var $secretinput = $('#id_comment-secret'), | |
$commentbody = $('#id_comment-body'); | |
// Create a function to act as the event handler, | |
// which we can reference by its own name internally | |
function setSecret(){ | |
// Kick off the timeout | |
setTimeout(function(){ | |
$secretinput.val('{% setting 'COMMENTS_SECRET' %}'); | |
}, | |
5000); | |
// Unbind the input so that it's not happening repeatedly | |
$commentbody.unbind('keypress', setSecret); | |
} | |
// Set up the keypress event | |
// in this case, using bind/unbind. If using jQuery 1.7, | |
// use on/off instead. | |
$commentbody.bind('keypress', setSecret); | |
})(jQuery); | |
</script> |
This file contains 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
def clean(self): | |
cleaned = self.cleaned_data | |
if not self.request.POST.get('comment-secret', '') == settings.COMMENTS_SECRET: | |
raise forms.ValidationError("This site thinks you're a spammer. If you're not, wait a few seconds after typing your comment before you submit it.") | |
return self.cleaned_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment