]> git.openstreetmap.org Git - osqa.git/commitdiff
A fix in akismet module, which was crashing when the user has 0 reputation.
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Fri, 11 Jun 2010 00:19:07 +0000 (00:19 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Fri, 11 Jun 2010 00:19:07 +0000 (00:19 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@404 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum_modules/akismet/startup.py

index 1e9e731e76eeee83bbc54074b6f7673d05df67fa..c1d548cfa538a292cc6f46d1c9ab378912e274ad 100644 (file)
@@ -12,43 +12,48 @@ from forum.models.user import User
 
 import settings
 
+def can_bypass_spam_check(user):
+    return user.is_authenticated() and (user.is_superuser or user.is_staff or cmp(user.reputation, REP_FOR_NO_SPAM_CHECK
+                                                                                  ) > 0)
+
 
 def check_spam(param, comment_type):
     def wrapper(origin, request, *args, **kwargs):
-        if (request.POST and request.POST.get(param, None) and WORDPRESS_API_KEY) and (not request.user.is_authenticated()
-             or not (request.user.is_staff or request.user.is_superuser or request.user.reputation >= REP_FOR_NO_SPAM_CHECK)):
+        if request.POST and request.POST.get(param, None) and WORDPRESS_API_KEY and (not can_bypass_spam_check(
+                request.user)):
             comment = smart_str(request.POST[param])
 
             data = {
-                "user_ip":request.META["REMOTE_ADDR"],
-                "user_agent":request.environ['HTTP_USER_AGENT'],
-                "comment_type": comment_type,
-                "comment":comment
+            "user_ip":request.META["REMOTE_ADDR"],
+            "user_agent":request.environ['HTTP_USER_AGENT'],
+            "comment_type": comment_type,
+            "comment":comment
             }
 
             if request.user.is_authenticated():
                 data.update({
-                    "comment_author":smart_str(request.user.username),
-                    "comment_author_email":request.user.email,
-                    "comment_author_url":request.user.website,    
+                "comment_author":smart_str(request.user.username),
+                "comment_author_email":request.user.email,
+                "comment_author_url":request.user.website,
                 })
 
             api = Akismet(settings.WORDPRESS_API_KEY, APP_URL, "OSQA/%s" % OSQA_VERSION)
             if api.comment_check(comment, data):
                 if request.is_ajax():
                     response = {
-                        'success': False,
-                        'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
+                    'success': False,
+                    'error_message': _("Sorry, but akismet thinks your %s is spam.") % comment_type
                     }
                     return HttpResponse(simplejson.dumps(response), mimetype="application/json")
                 else:
                     return render_to_response('modules/akismet/foundspam.html', {
-                        'action_name': comment_type
+                    'action_name': comment_type
                     })
-                    
+
         return origin(request, *args, **kwargs)
+
     return wrapper
-            
+
 
 decorate(views.writers.ask)(check_spam('text', _('question')))
 decorate(views.writers.answer)(check_spam('text', _('answer')))