<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>What's Chris Doing? &#187; python plone captcha recaptcha</title>
	<atom:link href="http://whatschrisdoing.com/blog/tag/python-plone-captcha-recaptcha/feed/" rel="self" type="application/rss+xml" />
	<link>http://whatschrisdoing.com/blog</link>
	<description>Programming, photography, music and life.</description>
	<lastBuildDate>Sun, 30 Oct 2011 22:07:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Protecting Plone Registration Forms with ReCAPTCHA</title>
		<link>http://whatschrisdoing.com/blog/2009/02/24/protecting-plone-registration-forms-with-recaptcha/</link>
		<comments>http://whatschrisdoing.com/blog/2009/02/24/protecting-plone-registration-forms-with-recaptcha/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 15:56:36 +0000</pubDate>
		<dc:creator>Christopher Lambacher</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python plone captcha recaptcha]]></category>

		<guid isPermaLink="false">http://whatschrisdoing.com/blog/?p=42</guid>
		<description><![CDATA[We recently launched the CIOC Community Site running on Plone. While we have not actually had any SPAM on the site yet, we continually get automated SPAM registrations that we then need to go and clear out. Last night Kate got fed up, so I decided to see if I could put a CAPTCHA on [...]]]></description>
			<content:encoded><![CDATA[<p>We recently launched the <a href="http://community.cioc.ca/">CIOC Community Site</a> running on Plone. While we have not actually had any SPAM on the site yet, we continually get automated SPAM registrations that we then need to go and clear out. Last night Kate got fed up, so I decided to see if I could put a CAPTCHA on the form.</p>
<p>Looks like there is a <a href="http://plone.org/products/plone/roadmap/222/">proposal to include CAPTCHA support</a>. The <em>de facto standard</em> CAPTCHA tool for Plone seems to be <a href="http://plone.org/products/collective.captcha">collective.captcha</a>, but I wanted to be able to use the <a href="http://recaptcha.net/">ReCAPTCHA</a> service.</p>
<p>Luckily there is <a href="http://plone.org/products/collective.recaptcha/">collective.recaptcha</a> which is in beta and, while not listing any releases on the Plone site, is <a href="http://pypi.python.org/pypi/collective.recaptcha/">available from the Cheese Shop</a>. But, alas, there is no documentation. It is supposed to be a drop in replacement for collective.captcha so with a little sleuthing and I got it to work.  Here&#8217;s how.</p>
<h3>Installation</h3>
<p>I am using the unified Unix installer so everything is done via buildout. Following the instructions on the <a href="http://pypi.python.org/pypi/collective.captcha/">collective.captcha</a> page we need to add <code>collective.recaptcha</code> to your eggs list and <code>collective.recaptcha</code> to your zcml list. Then run <code>./bin/buildout</code> and restart your Plone instance.</p>
<h3>Configuration</h3>
<p>We need to enter our private and public ReCAPTCHA keys in the collective.recaptcha setup form which is located at http://urltoyourplonesite.com/recaptcha-settings</p>
<h3>Registration Form Modifications</h3>
<p>Using <a href="http://www.codesyntax.com/cs-workshop/blog/using-collective.captcha-in-custom-forms">this</a> site as a helpful reference I was able to figure out the additions I needed to make to the registration form and validator.</p>
<p>You need to go in through your Plone site setup into the <em>Zope Management Interface</em>. Navigate to portal_skins/plone_login. Click join_form and then the customize button. You should now be able to customize the form contents. Down near the bottom of the form there is a line:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;formControls&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Above that line you need to add:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;field&quot;</span></span>
<span style="color: #009900;">     tal:define<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;error errors/captcha|nothing;&quot;</span></span>
<span style="color: #009900;">     tal:attributes<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;class python:test(error, 'field error', 'field')&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;captcha&quot;</span> i18n:translate<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;label_captcha&quot;</span>&gt;</span>Captcha<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fieldRequired&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Required&quot;</span></span>
<span style="color: #009900;">        i18n:attributes<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span></span>
<span style="color: #009900;">        i18n:domain<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;plone&quot;</span></span>
<span style="color: #009900;">        i18n:translate<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;label_required&quot;</span>&gt;</span>(Required)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;formHelp&quot;</span> i18n:translate<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;help_captcha&quot;</span>&gt;</span>
    Provide the text in the image. Just to avoid spambots
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> tal:replace<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;structure here/@@captcha/image_tag&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Note that the difference from the code provided by Mikel Larreategi is that ReCAPTACH provides the input element itself, so you need to omit the <code>div</code> that includes the <code>&lt;input type="text"&gt;</code> tag.</p>
<p>Once you have that saved, you need to go back to portal_skins/plone_login and click join_form_validate and once again click customize. At the bottom of the validation code, just before</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">return</span> state</pre></div></div>

<p> add:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">captcha = context.<span style="color: black;">REQUEST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'recaptcha_response_field'</span><span style="color: black;">&#41;</span>
view = context.<span style="color: black;">restrictedTraverse</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'@@captcha'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> view.<span style="color: black;">verify</span><span style="color: black;">&#40;</span>captcha<span style="color: black;">&#41;</span>:
    state.<span style="color: black;">setError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'captcha'</span>, _<span style="color: black;">&#40;</span>u<span style="color: #483d8b;">'Are you a bot? Try again...'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    state.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span>status=<span style="color: #483d8b;">'failure'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Note that the difference in the validation code from Mikel Larreategi&#8217;s is that the ReCAPTCHA inserted input tag is called <code>recaptcha_response_field</code> and not <code>captcha</code>.</p>
<p>Hopefully that is helpful to someone other than me :)</p>
]]></content:encoded>
			<wfw:commentRss>http://whatschrisdoing.com/blog/2009/02/24/protecting-plone-registration-forms-with-recaptcha/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

