]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/akismet/startup.py
Remove ADMIN_MEDIA_PREFIX since it has been deprecated in Django 1.4
[osqa.git] / forum_modules / akismet / startup.py
1 import json
2 import loggin
3
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
13
14 from settings import WORDPRESS_API_KEY, REP_FOR_NO_SPAM_CHECK
15
16 from forum.models.user import User
17 from forum.forms.general import SimpleCaptchaForm
18
19 import settings
20
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)
23
24
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)):
28         
29             comment = smart_str(request.POST[param])
30
31             data = {
32             "user_ip":request.META["REMOTE_ADDR"],
33             "user_agent":request.environ['HTTP_USER_AGENT'],
34             "comment_type": comment_type,
35             "comment":comment
36             }
37
38             if request.user.is_authenticated():
39                 data.update({
40                 "comment_author":smart_str(request.user.username),
41                 "comment_author_email":request.user.email,
42                 "comment_author_url":request.user.website,
43                 })
44
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)
49                 
50                 if request.is_ajax():
51                     response = {
52                     'success': False,
53                     'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
54                     }
55                     return HttpResponse(json.dumps(response), mimetype="application/json")
56                 else:
57                     captcha_checked = False
58                     try:
59                         if captcha_form.is_valid() and 'recaptcha' in captcha_form.fields.keys():
60                             captcha_checked = True
61                     except:
62                         pass
63
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))
70
71         return origin(request, *args, **kwargs)
72
73     return wrapper
74
75
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')))