]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/akismet/startup.py
added the "Minimum reputation to not have your posts checked for spam." field to...
[osqa.git] / forum_modules / akismet / startup.py
1 from django.utils.translation import ugettext as _
2 from django.http import HttpResponse, HttpResponseRedirect
3 from django.utils import simplejson
4 from django.shortcuts import render_to_response
5 from forum.modules.decorators import decorate
6 from forum import views
7 from lib.akismet import Akismet
8 from forum.settings import APP_URL, OSQA_VERSION
9 import settings
10
11
12 def check_spam(param, comment_type):
13     def wrapper(origin, request, *args, **kwargs):
14         if request.POST and request.POST.get(param, None) and settings.WORDPRESS_API_KEY:
15             comment = request.POST[param]
16             data = {
17                 "user_ip":request.META["REMOTE_ADDR"],
18                 "user_agent":request.environ['HTTP_USER_AGENT'],
19                 "comment_type": comment_type,
20                 "comment":comment
21             }
22
23             if request.user.is_authenticated():
24                 data.update({
25                     "comment_author":request.user.username,
26                     "comment_author_email":request.user.email,
27                     "comment_author_url":request.user.website,    
28                 })
29
30             api = Akismet(settings.WORDPRESS_API_KEY, APP_URL, "OSQA/%s" % OSQA_VERSION)
31             if api.comment_check(comment, data):
32                 if request.is_ajax():
33                     response = {
34                         'success': False,
35                         'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
36                     }
37                     return HttpResponse(simplejson.dumps(response), mimetype="application/json")
38                 else:
39                     return render_to_response('modules/akismet/foundspam.html', {
40                         'action_name': comment_type
41                     })
42                     
43         return origin(request, *args, **kwargs)
44     return wrapper
45             
46
47 decorate(views.writers.ask)(check_spam('text', _('question')))
48 decorate(views.writers.answer)(check_spam('text', _('answer')))
49 decorate(views.commands.comment)(check_spam('comment', _('comment')))
50
51