1. Benoit Chesneau
  2. couchit

Commits

benoitc  committed 2aeb4b7

first bits of akismet antispam management. For now we flag pages

  • Participants
  • Parent commits 15e8362
  • Branches default

Comments (0)

Files changed (6)

File couchit/documents.py

View file
     claimed = BooleanProperty(default=False)
     created = DateTimeProperty()
     updated = DateTimeProperty()
+    akismet_key = StringProperty(default='')
     allow_javascript = BooleanProperty(default=False)
     default_theme = BooleanProperty(default=True)
     theme = SchemaProperty(Theme)
         if cls.get_alias(siteid, pagename) is not None:
             return True
         return False
-        
-                      
+                          
 class Page(Document):
     site = StringProperty()
     title = StringProperty()
     updated = DateTimeProperty()
     changes = ListProperty()
     nb_revision = IntegerProperty(default=0)
+    is_spam = BooleanProperty(default=False)
     
     itemType = StringProperty(default='page')
     

File couchit/settings.py

View file
 import platform
 import os
 
-DEBUG = False
+DEBUG = True
 
 SERVER_NAME = 'couch.it'
 

File couchit/urls.py

View file
     'site_design': views.site_design,
     'site_settings': views.site_settings,
     'site_claim': views.site_claim,
+    'site_check_akismet': views.site_check_akismet,
     'proxy': views.proxy,
     'site_login': views.site_login,
     'site_logout': views.site_logout,
     Rule('/site/change-password', endpoint='change_password'),
     Rule('/site/change-site-address', endpoint='site_address'),
     Rule('/site/settings', endpoint='site_settings'),
+    Rule('/site/check-akismet/<key>', endpoint='site_check_akismet'),
     Rule('/site/sitemap.xml', endpoint='sitemap'),
     Rule('/sitemap.xml', endpoint='sitemap'),
     Rule('/site/changes', endpoint='site_changes'),

File couchit/views.py

View file
 from couchit.http import BCResponse
 from couchit.template import render_response, url_for, render_template, send_json, convert_markdown
 from couchit.utils import db, local, make_hash, datetime_tojson, to_str, smart_str, force_unicode
+from couchit.utils.akismet import Akismet
 from couchit.utils.mail import send_mail
 from couchit.utils.sioc import SiocWiki, send_sioc
 
             else:
                 error = "An unexpected error happened, please contact administrator."
         else:
-            page.content = request.form.get('content', '')
+            content = request.form.get('content', '')
+            
+            # check spam with akismet
+            if request.site.akismet_key:
+                ak = Akismet()
+                if request.site.alias:
+                    site_url = "http://%s.%s" % (request.site.alias, settings.SERVER_NAME)
+                else:
+                    site_url = "http://%s/%s" % (settings.SERVER_NAME, request.site.cname)
+                is_spam = ak.comment_check(site_url, request.environ['REMOTE_ADDR'], 
+                    request.environ['HTTP_USER_AGENT'], content)
+            else:
+                is_spam = False
+                
+            page.content = content
+            page.is_spam = is_spam # flag page
             page.save()
             redirect_url = url_for('show_page', pagename=pagename)
             return redirect(redirect_url)
     
-    return redirect(url_for('show_page', pagename=pagename))
+    return redirect(url_for('show_page', pagename=pagename, error=error))
 
 @can_edit
 @login_required
         site.subtitle = data.get('subtitle', site.subtitle)
         site.email = data.get('email', site.email)
         site.privacy = data.get('privacy', site.privacy)
+        site.akismet_key = data.get('akismet_key', "")
         site.allow_javascript = allow_javascript
         site.save()
         request.site = site
     pages = all_pages(request.site._id)       
     return render_response('site/design.html', pages=pages)
     
-
+def site_check_akismet(request, key):
+    if request.site.alias:
+        site_url = "http://%s.%s" % (request.site.alias, settings.SERVER_NAME)
+    else:
+        site_url = "http://%s/%s" % (settings.SERVER_NAME, request.site.cname)
+        
+    res = Akismet()
+    resp = res.verify_key(site_url, key)
+    return send_json({"valid": resp})
     
 def sitemap(request):
     pages = all_pages(request.site._id)
     response.content_type = content_type
     return response 
 
-
 def couchit_about(request):
     return render_response('about.html')
     

File static/js/src/application.js

View file
                'subtitle': $('site_subtitle').getValue(),
                'email': $('email').getValue(),
                'privacy': $$('input:checked[type="radio"][name="privacy"]').pluck('value')[0],
+               'akismet_key': $('site_akismet_key').getValue(),
                'allow_javascript': $('allow_javascript').getValue()
+               
            }
        } catch(e) {
            data = {

File templates/site/settings.html

View file
                 </td>
             </tr>
             <tr>
+                <th>Akismet API key</th>
+                <td><input type="text" name="site_akismet_key" id="site_akismet_key" value="{{ site.akismet_key }}"></td>
+                <td>Fight spam on you site with <a href="http://akismet.com/">akismet</a></td>
+            </tr>
+            <tr>
                 <th>Allow javascript:</th>
                 <td><input type="checkbox" name="allow_javascript" id="allow_javascript" value="1"{% if site.allow_javascript %} checked="checked"{% endif %} /></td>
                 <td>Allow users to include javascript in pages</td>
+            </tr>
+            
             {% endif %}
         </table>
     </form>