4 from django.utils.translation import ugettext as _
5 from django.http import HttpResponse, HttpResponseRedirect
6 from django.template import RequestContext
7 from django.utils.encoding import smart_str
8 from django.shortcuts import render_to_response
9 from forum.modules import decorate
10 from forum import views
11 from lib.akismet import Akismet
12 from forum.settings import APP_URL, OSQA_VERSION
14 from settings import WORDPRESS_API_KEY, REP_FOR_NO_SPAM_CHECK
16 from forum.models.user import User
17 from forum.forms.general import SimpleCaptchaForm
21 def can_bypass_spam_check(user):
22 return user.is_authenticated and (user.is_superuser or user.is_staff or cmp(int(user.reputation), REP_FOR_NO_SPAM_CHECK) > 0)
25 def check_spam(param, comment_type):
26 def wrapper(origin, request, *args, **kwargs):
27 if request.POST and request.POST.get(param, None) and WORDPRESS_API_KEY and (not can_bypass_spam_check(request.user)):
29 comment = smart_str(request.POST[param])
32 "user_ip":request.META["REMOTE_ADDR"],
33 "user_agent":request.environ['HTTP_USER_AGENT'],
34 "comment_type": comment_type,
38 if request.user.is_authenticated():
40 "comment_author":smart_str(request.user.username),
41 "comment_author_email":request.user.email,
42 "comment_author_url":request.user.website,
45 api = Akismet(settings.WORDPRESS_API_KEY, APP_URL, "OSQA/%s" % OSQA_VERSION)
46 if api.comment_check(comment, data):
47 post_data = request.POST
48 captcha_form = SimpleCaptchaForm(request.POST)
53 'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
55 return HttpResponse(json.dumps(response), mimetype="application/json")
57 captcha_checked = False
59 if captcha_form.is_valid() and 'recaptcha' in captcha_form.fields.keys():
60 captcha_checked = True
64 if not captcha_checked:
65 return render_to_response('modules/akismet/foundspam.html', {
66 'action_name': comment_type,
67 'post_data' : post_data,
68 'captcha_form' : captcha_form,
69 }, RequestContext(request))
71 return origin(request, *args, **kwargs)
76 decorate(views.writers.ask)(check_spam('text', _('question')))
77 decorate(views.writers.answer)(check_spam('text', _('answer')))
78 decorate(views.commands.comment)(check_spam('comment', _('comment')))