1 # -*- coding: utf-8 -*-
2 from os
.path
import dirname
, join
3 from urllib
import urlopen
5 from werkzeug
import escape
, url_encode
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
20 from recaptcha
.client
import captcha
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
50 #import pdb ; pdb.set_trace()
52 resp
= captcha
.submit(
53 recaptcha_challenge_field
=req
.values
['recaptcha_challenge_field'],
54 recaptcha_response_field
=req
.values
['recaptcha_response_field'],
55 private_key
=req
.app
.cfg
['recaptcha_spam_filter/privkey'],
56 remoteip
=comment
.submitter_ip
63 comment
.status
= COMMENT_BLOCKED_SPAM
64 comment
.blocked_msg
= "Blocked for failing the captcha"
67 def add_recaptcha_link(req
, navigation_bar
):
68 """Add a button for recaptcha to the comments page."""
69 if req
.user
.has_privilege(BLOG_ADMIN
):
70 for link_id
, url
, title
, children
in navigation_bar
:
71 if link_id
== 'options':
72 children
.insert(-3, ('recaptcha_spam_filter',
73 url_for('recaptcha_spam_filter/config'),
77 @require_privilege(BLOG_ADMIN
)
78 def show_recaptcha_config(req
):
79 """Show the recaptcha control panel."""
80 form
= ConfigurationForm(initial
=dict(
81 pubkey
=req
.app
.cfg
['recaptcha_spam_filter/pubkey'],
82 privkey
=req
.app
.cfg
['recaptcha_spam_filter/privkey']
85 if req
.method
== 'POST' and form
.validate(req
.form
):
87 req
.app
.cfg
.change_single('recaptcha_spam_filter/pubkey',
89 req
.app
.cfg
.change_single('recaptcha_spam_filter/privkey',
92 flash(_('reCAPTCHA has been successfully enabled.'), 'ok')
94 flash(_('reCAPTCHA disabled.'), 'ok')
95 return redirect_to('recaptcha_spam_filter/config')
96 return render_admin_response('admin/recaptcha_spam_filter.html',
97 'options.recaptcha_spam_filter',
98 form
=form
.as_widget())
101 def add_captcha(req
, post
):
102 return captcha
.displayhtml(req
.app
.cfg
['recaptcha_spam_filter/pubkey'])
104 def setup(app
, plugin
):
105 if not have_recaptcha
:
106 raise SetupError("The reCAPTCHA plugin requires the recaptcha python library")
107 app
.add_config_var('recaptcha_spam_filter/pubkey',
108 forms
.TextField(default
=u
''))
109 app
.add_config_var('recaptcha_spam_filter/privkey',
110 forms
.TextField(default
=u
''))
111 app
.add_url_rule('/options/recaptcha', prefix
='admin',
112 endpoint
='recaptcha_spam_filter/config',
113 view
=show_recaptcha_config
)
114 app
.connect_event('before-comment-editor-buttons-rendered', add_captcha
)
115 app
.connect_event('before-comment-saved', do_spamcheck
)
116 app
.connect_event('modify-admin-navigation-bar', add_recaptcha_link
)
117 app
.add_template_searchpath(TEMPLATES
)