Remove pdb from do_spamcheck function
[recaptcha_spam_plugin.git] / __init__.py
blobdd27f445900422a055520417ccfad17e92c59329
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 USER_AGENT = 'Zine /%s | reCatpcha/1.11' % zine.__version__
26 #AKISMET_URL_BASE = 'rest.akismet.com'
27 #AKISMET_VERSION = '1.1'
28 TEMPLATES = join(dirname(__file__), 'templates')
29 #BLOCKED_MSG = 'blocked by akismet'
32 #: because we need the information about verified keys quite often
33 #: we store verified keys here.
34 _verified_keys = set()
37 class ConfigurationForm(forms.Form):
38 """The configuration form."""
39 pubkey = forms.TextField()
40 privkey = forms.TextField()
43 def do_spamcheck(req, comment):
44 """Do spamchecking for all new comments."""
45 # something blocked the comment already. no need to check for
46 # spam then.
47 if comment.blocked:
48 return
50 resp = captcha.submit(
51 recaptcha_challenge_field=req.values['recaptcha_challenge_field'],
52 recaptcha_response_field=req.values['recaptcha_response_field'],
53 private_key=req.app.cfg['recaptcha_spam_filter/privkey'],
54 remoteip=comment.submitter_ip
57 if resp.is_valid:
58 # Captcha passed
59 return
60 else:
61 comment.status = COMMENT_BLOCKED_SPAM
62 comment.blocked_msg = "Blocked for failing the captcha"
65 def add_recaptcha_link(req, navigation_bar):
66 """Add a button for recaptcha to the comments page."""
67 if req.user.has_privilege(BLOG_ADMIN):
68 for link_id, url, title, children in navigation_bar:
69 if link_id == 'options':
70 children.insert(-3, ('recaptcha_spam_filter',
71 url_for('recaptcha_spam_filter/config'),
72 _('reCAPTCHA')))
75 @require_privilege(BLOG_ADMIN)
76 def show_recaptcha_config(req):
77 """Show the recaptcha control panel."""
78 form = ConfigurationForm(initial=dict(
79 pubkey=req.app.cfg['recaptcha_spam_filter/pubkey'],
80 privkey=req.app.cfg['recaptcha_spam_filter/privkey']
83 if req.method == 'POST' and form.validate(req.form):
84 if form.has_changed:
85 req.app.cfg.change_single('recaptcha_spam_filter/pubkey',
86 form['pubkey'])
87 req.app.cfg.change_single('recaptcha_spam_filter/privkey',
88 form['privkey'])
89 if form['pubkey']:
90 flash(_('reCAPTCHA has been successfully enabled.'), 'ok')
91 else:
92 flash(_('reCAPTCHA disabled.'), 'ok')
93 return redirect_to('recaptcha_spam_filter/config')
94 return render_admin_response('admin/recaptcha_spam_filter.html',
95 'options.recaptcha_spam_filter',
96 form=form.as_widget())
99 def add_captcha(req, post):
100 return captcha.displayhtml(req.app.cfg['recaptcha_spam_filter/pubkey'])
102 def setup(app, plugin):
103 if not have_recaptcha:
104 raise SetupError("The reCAPTCHA plugin requires the recaptcha python library")
105 app.add_config_var('recaptcha_spam_filter/pubkey',
106 forms.TextField(default=u''))
107 app.add_config_var('recaptcha_spam_filter/privkey',
108 forms.TextField(default=u''))
109 app.add_url_rule('/options/recaptcha', prefix='admin',
110 endpoint='recaptcha_spam_filter/config',
111 view=show_recaptcha_config)
112 app.connect_event('before-comment-editor-buttons-rendered', add_captcha)
113 app.connect_event('before-comment-saved', do_spamcheck)
114 app.connect_event('modify-admin-navigation-bar', add_recaptcha_link)
115 app.add_template_searchpath(TEMPLATES)