]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/akismet/startup.py
cb449cc5877f6242b37c4327d591f5846d84a8d4
[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, REP_TO_FOR_NO_SPAM_CHECK
9 from forum.models.user import User
10
11 import settings
12
13
14 def check_spam(param, comment_type):
15     def wrapper(origin, request, *args, **kwargs):
16         if request.POST and request.POST.get(param, None) and settings.WORDPRESS_API_KEY:
17             comment = request.POST[param]
18             user = request.user
19
20             data = {
21                 "user_ip":request.META["REMOTE_ADDR"],
22                 "user_agent":request.environ['HTTP_USER_AGENT'],
23                 "comment_type": comment_type,
24                 "comment":comment
25             }
26
27             if user.is_authenticated():
28                 data.update({
29                     "comment_author":request.user.username,
30                     "comment_author_email":request.user.email,
31                     "comment_author_url":request.user.website,    
32                 })
33
34             api = Akismet(settings.WORDPRESS_API_KEY, APP_URL, "OSQA/%s" % OSQA_VERSION)
35             if not user.is_authenticated() or (user.reputation < settings.REP_TO_FOR_NO_SPAM_CHECK and not user.is_staff and not user.is_superuser):
36                 if api.comment_check(comment, data):
37                     if request.is_ajax():
38                         response = {
39                             'success': False,
40                             'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
41                         }
42                         return HttpResponse(simplejson.dumps(response), mimetype="application/json")
43                     else:
44                         return render_to_response('modules/akismet/foundspam.html', {
45                             'action_name': comment_type
46                         })
47                     
48         return origin(request, *args, **kwargs)
49     return wrapper
50             
51
52 decorate(views.writers.ask)(check_spam('text', _('question')))
53 decorate(views.writers.answer)(check_spam('text', _('answer')))
54 decorate(views.commands.comment)(check_spam('comment', _('comment')))
55
56