def moderate(self, comment, content_object, request): """ Return True or False, True indicating that the Akismet spam-checking service thinks the comment is spam. As the Akismet library doesn't handle Unicode all user-input is converted to ASCII before it's passed to the library. """ api = Akismet(key=settings.AKISMET_API_KEY) for k in comment.userinfo: comment.userinfo[k] = unicodedata.normalize( 'NFKD', comment.userinfo[k]).encode('ascii', 'ignore') comment_data = { 'user_ip': comment.ip_address, 'user_agent': '', 'comment_author': comment.userinfo['name'], 'comment_author_url': comment.userinfo['url'], 'permalink': comment.get_absolute_url(), } try: return api.comment_check( unicodedata.normalize('NFKD', comment.comment).encode( 'ascii', 'ignore'), comment_data) except (urllib2.HTTPError, urllib2.URLError): # If Akismet is down the safest option is to assume the # comment needs moderating. return True
def is_spam(self): try: api = Akismet(key=settings.AKISMET_API_KEY) data = { "user_ip": os.environ.get('REMOTE_ADDR', '127.0.0.1'), "user_agent": os.environ.get('HTTP_USER_AGENT', 'Unknown'), } return api.comment_check( unicodedata.normalize('NFKD', self.cleaned_data["body"]).encode( 'ascii', 'ignore'), data) except (urllib2.HTTPError, urllib2.URLError): # TODO: Do better than simply assume the message is OK if Akismet # is down. return False