Example #1
0
    def test_missing_config(self):
        """
        Instantiating without any configuration fails.

        """
        with self.assertRaises(akismet.ConfigurationError):
            akismet.Akismet(key=None, blog_url=None)
        with self.assertRaises(akismet.ConfigurationError):
            akismet.Akismet()
Example #2
0
    def test_bad_config_args(self):
        """
        Configuring with bad arguments fails.

        """
        with self.assertRaises(akismet.APIKeyError):
            akismet.Akismet(key='invalid', blog_url='http://invalid')
Example #3
0
    def test_config_from_args(self):
        """
        Configuring via explicit arguments succeeds.

        """
        api = akismet.Akismet(key=self.api_key, blog_url=self.blog_url)
        self.assertEqual(self.api_key, api.api_key)
        self.assertEqual(self.blog_url, api.blog_url)
Example #4
0
    def test_user_agent(self):
        """
        The Akismet class creates the correct user-agent string.

        """
        api = akismet.Akismet(key=self.api_key, blog_url=self.blog_url)
        expected_agent = "Python/{} | akismet.py/{}".format(
            '{}.{}'.format(*sys.version_info[:2]), akismet.__version__)
        self.assertEqual(expected_agent, api.user_agent_header['User-Agent'])
Example #5
0
    def test_config_from_env(self):
        """
        Configuring via environment variables succeeds.

        """
        try:
            os.environ[self.api_key_env_var] = self.api_key
            os.environ[self.blog_url_env_var] = self.blog_url

            api = akismet.Akismet(key=None, blog_url=None)
            self.assertEqual(self.api_key, api.api_key)
            self.assertEqual(self.blog_url, api.blog_url)

            api = akismet.Akismet()
            self.assertEqual(self.api_key, api.api_key)
            self.assertEqual(self.blog_url, api.blog_url)
        finally:
            os.environ[self.api_key_env_var] = ''
            os.environ[self.blog_url_env_var] = ''
Example #6
0
    def test_bad_config_env(self):
        """
        Configuring with bad environment variables fails.

        """
        try:
            os.environ[self.api_key_env_var] = 'invalid'
            os.environ[self.blog_url_env_var] = 'http://invalid'
            with self.assertRaises(akismet.APIKeyError):
                akismet.Akismet()
        finally:
            os.environ[self.api_key_env_var] = ''
            os.environ[self.blog_url_env_var] = ''
Example #7
0
    def test_bad_url(self):
        """
        Configuring with a bad URL fails.

        """
        bad_urls = (
            'example.com',
            'ftp://example.com',
            'www.example.com',
            'http//example.com',
            'https//example.com',
        )
        for url in bad_urls:
            with self.assertRaises(akismet.ConfigurationError):
                akismet.Akismet(key=self.api_key, blog_url=url)
Example #8
0
def moderate_comment(sender, comment, request, **kwargs):
    ak = akismet.Akismet(key=settings.AKISMET_API_KEY,
                         blog_url='http://%s/' %
                         Site.objects.get_current().domain)
    data = {
        'user_ip': request.META.get('REMOTE_ADDR', '127.0.0.1'),
        'user_agent': request.META.get('HTTP_USER_AGENT', ''),
        'referrer': request.META.get('HTTP_REFERRER', ''),
        'comment_type': 'comment',
        'comment_author': smart_str(comment.user_name),
    }
    if ak.comment_check(smart_str(comment.comment), data=data,
                        build_data=True):
        comment.is_public = False
        comment.save()
Example #9
0
def isspam(comment, author, ipaddress, agent=defaultagent, apikey=defaultkey):
    try:
        #传入key和url,另外一种方法是将key和url存到当前目录下的apikey.txt中
        api = akismet.Akismet(key=apikey, blog_url=pageurl, agent=agent)
        valid = api.verify_key()

        if valid:
            print "OK. The key is valid"
            return api.comment_check(comment, data)
        else:
            print 'Invalid key'
            return False
    except akismet.AkismetError, e:
        print "AkismetError"
        return False
Example #10
0
def spam_check(comment):
    # Pass comment object into configured spam control engines and return True or False
    config = Blog.objects.first()
    spam_status = False

    if config.akismet_key:
        akismet_api = akismet.Akismet(key=config.akismet_key,
                                      blog_url=config.site_url)
        spam_status = akismet_api.comment_check(
            comment.ip_address,
            comment.user_agent,
            comment_author=comment.name,
            comment_author_email=comment.email,
            comment_author_url=comment.website,
            comment_content=comment.body)

        return spam_status

    return spam_status
Example #11
0
def akismet_spam_ham(comment):
    """Submit comment to Akismet spam/ham API, using current spam status"""

    config = Blog.objects.first()
    if config.akismet_key:

        akismet_api = akismet.Akismet(key=config.akismet_key,
                                      blog_url=config.site_url)

        kwargs = {
            'comment_author': comment.name,
            'comment_author_email': comment.email,
            'comment_author_url': comment.website,
            'comment_content': comment.body
        }

        if comment.spam is True:
            submit = akismet_api.submit_spam(comment.ip_address,
                                             comment.user_agent, **kwargs)
        else:
            submit = akismet_api.submit_ham(comment.ip_address,
                                            comment.user_agent, **kwargs)

        return submit
Example #12
0
 def setUp(self):
     self.api = akismet.Akismet(key=self.api_key, blog_url=self.blog_url)
Example #13
0
import akismet
from django.conf import settings
from recaptcha.client import captcha

spam_api = akismet.Akismet(agent="mymissedopportunities")


def is_spam(request, message, author):
    data = {
        'user_ip': request.META.get('REMOTE_ADDR', '127.0.0.1'),
        'user_agent': request.META.get('HTTP_USER_AGENT', ''),
        'referrer': request.META.get('HTTP_REFERER', ''),
        'comment_type': 'comment',
        'comment_author': author,
    }
    return spam_api.comment_check(message, data)


def verify_captcha(request):
    return captcha.submit(request.POST['recaptcha_challenge_field'],
                          request.POST['recaptcha_response_field'],
                          settings.RECAPTCHA_PRIVATE_KEY,
                          request.META['REMOTE_ADDR']).is_valid
Example #14
0
 def __init__(self, config):
     if not AKISMET_AVAILABLE:
         raise ImportError('akismet not available')
     self.service = akismet.Akismet(config.get('spam.key'),
                                    config.get('base_url'))
     self.service.verify_key()