Standardised on reCAPTCHA as a spelling and removed akismet file
[recaptcha_spam_plugin.git] / __init__.py
blobc981c63bc897298dd91a504cf76c898f4039396b
1 # -*- coding: utf-8 -*-
2 from os.path import dirname, join
3 from urllib import urlopen
5 from werkzeug import escape, url_encode
7 import zine
8 from zine.api import *
9 from zine.widgets import Widget
10 from zine.views.admin import flash, render_admin_response
11 from zine.models import COMMENT_BLOCKED_SPAM, Comment
12 from zine.privileges import BLOG_ADMIN, require_privilege
13 from zine.utils.validators import ValidationError, check
14 from zine.utils.http import redirect_to
15 from zine.utils import forms
17 # Check do we have the recaptcha library installed
19 try:
20 from recaptcha.client import captcha
21 have_recaptcha = True
22 except ImportError:
23 have_recaptcha = False
25 TEMPLATES = join(dirname(__file__), 'templates')
27 class ConfigurationForm(forms.Form):
28 """The configuration form."""
29 pubkey = forms.TextField()
30 privkey = forms.TextField()
33 def do_spamcheck(req, comment):
34 """Do spamchecking for all new comments."""
35 # something blocked the comment already. no need to check for
36 # spam then.
37 if comment.blocked:
38 return
40 resp = captcha.submit(
41 recaptcha_challenge_field=req.values['recaptcha_challenge_field'],
42 recaptcha_response_field=req.values['recaptcha_response_field'],
43 private_key=req.app.cfg['recaptcha_spam_filter/privkey'],
44 remoteip=comment.submitter_ip
47 if resp.is_valid:
48 # Captcha passed
49 return
50 else:
51 comment.status = COMMENT_BLOCKED_SPAM
52 comment.blocked_msg = "Blocked for failing the captcha"
55 def add_recaptcha_link(req, navigation_bar):
56 """Add a button for recaptcha to the comments page."""
57 if req.user.has_privilege(BLOG_ADMIN):
58 for link_id, url, title, children in navigation_bar:
59 if link_id == 'options':
60 children.insert(-3, ('recaptcha_spam_filter',
61 url_for('recaptcha_spam_filter/config'),
62 _('reCAPTCHA')))
65 @require_privilege(BLOG_ADMIN)
66 def show_recaptcha_config(req):
67 """Show the recaptcha control panel."""
68 form = ConfigurationForm(initial=dict(
69 pubkey=req.app.cfg['recaptcha_spam_filter/pubkey'],
70 privkey=req.app.cfg['recaptcha_spam_filter/privkey']
73 if req.method == 'POST' and form.validate(req.form):
74 if form.has_changed:
75 req.app.cfg.change_single('recaptcha_spam_filter/pubkey',
76 form['pubkey'])
77 req.app.cfg.change_single('recaptcha_spam_filter/privkey',
78 form['privkey'])
79 if form['pubkey']:
80 flash(_('reCAPTCHA has been successfully enabled.'), 'ok')
81 else:
82 flash(_('reCAPTCHA disabled.'), 'ok')
83 return redirect_to('recaptcha_spam_filter/config')
84 return render_admin_response('admin/recaptcha_spam_filter.html',
85 'options.recaptcha_spam_filter',
86 form=form.as_widget())
89 def add_captcha(req, post):
90 return captcha.displayhtml(req.app.cfg['recaptcha_spam_filter/pubkey'])
92 def setup(app, plugin):
93 if not have_recaptcha:
94 raise SetupError("The reCAPTCHA plugin requires the recaptcha python library")
95 app.add_config_var('recaptcha_spam_filter/pubkey',
96 forms.TextField(default=u''))
97 app.add_config_var('recaptcha_spam_filter/privkey',
98 forms.TextField(default=u''))
99 app.add_url_rule('/options/recaptcha', prefix='admin',
100 endpoint='recaptcha_spam_filter/config',
101 view=show_recaptcha_config)
102 app.connect_event('before-comment-editor-buttons-rendered', add_captcha)
103 app.connect_event('before-comment-saved', do_spamcheck)
104 app.connect_event('modify-admin-navigation-bar', add_recaptcha_link)
105 app.add_template_searchpath(TEMPLATES)