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