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