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 from settings import WORDPRESS_API_KEY, REP_FOR_NO_SPAM_CHECK
10 from forum.models.user import User
15 def check_spam(param, comment_type):
16 def wrapper(origin, request, *args, **kwargs):
17 if (request.POST and request.POST.get(param, None) and WORDPRESS_API_KEY) and (not request.user.is_authenticated()
18 or not (request.user.is_staff and request.user.is_superuser and request.user.reputation >= REP_FOR_NO_SPAM_CHECK)):
19 comment = request.POST[param]
22 "user_ip":request.META["REMOTE_ADDR"],
23 "user_agent":request.environ['HTTP_USER_AGENT'],
24 "comment_type": comment_type,
28 if request.user.is_authenticated():
30 "comment_author":request.user.username,
31 "comment_author_email":request.user.email,
32 "comment_author_url":request.user.website,
35 api = Akismet(settings.WORDPRESS_API_KEY, APP_URL, "OSQA/%s" % OSQA_VERSION)
36 if api.comment_check(comment, data):
40 'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
42 return HttpResponse(simplejson.dumps(response), mimetype="application/json")
44 return render_to_response('modules/akismet/foundspam.html', {
45 'action_name': comment_type
48 return origin(request, *args, **kwargs)
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')))