]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/akismet/startup.py
Accepting incoming merge (branches/carpenter->trunk). Incorporates fixes to the...
[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.utils.encoding import smart_str
5 from django.shortcuts import render_to_response
6 from forum.modules import decorate
7 from forum import views
8 from lib.akismet import Akismet
9 from forum.settings import APP_URL, OSQA_VERSION
10 from settings import WORDPRESS_API_KEY, REP_FOR_NO_SPAM_CHECK, RECAPTCHA_PUB_KEY, RECAPTCHA_PRIV_KEY
11 from forum.models.user import User
12 from forum.forms.general import SimpleCaptchaForm
13
14 import settings
15
16 def can_bypass_spam_check(user):
17     return user.is_authenticated and (user.is_superuser or user.is_staff or cmp(int(user.reputation), REP_FOR_NO_SPAM_CHECK) > 0)
18
19
20 def check_spam(param, comment_type):
21     def wrapper(origin, request, *args, **kwargs):
22         if request.POST and request.POST.get(param, None) and WORDPRESS_API_KEY and (not can_bypass_spam_check(request.user)):
23         
24             comment = smart_str(request.POST[param])
25
26             data = {
27             "user_ip":request.META["REMOTE_ADDR"],
28             "user_agent":request.environ['HTTP_USER_AGENT'],
29             "comment_type": comment_type,
30             "comment":comment
31             }
32
33             if request.user.is_authenticated():
34                 data.update({
35                 "comment_author":smart_str(request.user.username),
36                 "comment_author_email":request.user.email,
37                 "comment_author_url":request.user.website,
38                 })
39
40             api = Akismet(settings.WORDPRESS_API_KEY, APP_URL, "OSQA/%s" % OSQA_VERSION)
41             if api.comment_check(comment, data):
42                 post_data = request.POST
43                 captcha_form = SimpleCaptchaForm(request.POST)
44                 
45                 if request.is_ajax():
46                     response = {
47                     'success': False,
48                     'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
49                     }
50                     return HttpResponse(simplejson.dumps(response), mimetype="application/json")
51                 else:
52                     captcha_checked = False
53                     
54                     if RECAPTCHA_PUB_KEY and RECAPTCHA_PRIV_KEY:
55                         if captcha_form.is_valid():
56                             captcha_checked = True
57                     
58                     if not captcha_checked:
59                         return render_to_response('modules/akismet/foundspam.html', {
60                         'action_name': comment_type,
61                         'post_data' : post_data,
62                         'captcha_form' : captcha_form,
63                         })
64
65         return origin(request, *args, **kwargs)
66
67     return wrapper
68
69
70 decorate(views.writers.ask)(check_spam('text', _('question')))
71 decorate(views.writers.answer)(check_spam('text', _('answer')))
72 decorate(views.commands.comment)(check_spam('comment', _('comment')))