[2805] | 1 | ============================ |
---|
| 2 | quintagroup.formlib.captcha |
---|
| 3 | ============================ |
---|
| 4 | |
---|
| 5 | This package allows to add captcha to zope.formlib form. |
---|
| 6 | As a result such forms are prevented from automatic submit. |
---|
| 7 | |
---|
| 8 | *example.py* module provides example of usage the *Captcha* |
---|
| 9 | field from the package in formlib forms. |
---|
| 10 | |
---|
| 11 | What you have to do |
---|
| 12 | ------------------- |
---|
| 13 | |
---|
| 14 | 1. You should add a *Captcha* field to your schema: |
---|
| 15 | |
---|
| 16 | >>> from zope.interface import Interface |
---|
| 17 | >>> from quintagroup.formlib.captcha import Captcha |
---|
| 18 | >>> class ICaptchaFormlibFormSchema(Interface): |
---|
| 19 | ... # ... your schema definition |
---|
| 20 | ... captcha = Captcha(title=u'Type the code') |
---|
| 21 | |
---|
| 22 | 2. If your form define Schema adapter (ICaptchaFormlibFormSchema |
---|
| 23 | adapter in our case), just add *captcha* property. |
---|
| 24 | |
---|
| 25 | >>> from zope.component import adapts |
---|
| 26 | >>> from zope.interface import implements |
---|
| 27 | >>> class CaptchaFormlibFormAdapter(object): |
---|
| 28 | ... adapts(Interface) |
---|
| 29 | ... implements(ICaptchaFormlibFormSchema) |
---|
| 30 | ... # ... your adapter code |
---|
| 31 | ... captcha = None |
---|
| 32 | |
---|
| 33 | And away we go. |
---|
| 34 | |
---|
| 35 | In tests/tests.zcml we have registered a adapter and page view for test form, |
---|
| 36 | named @@formlib-captcha-form. |
---|
| 37 | |
---|
| 38 | Get browser object . |
---|
| 39 | |
---|
| 40 | >>> from Products.Five.testbrowser import Browser |
---|
| 41 | >>> from Products.PloneTestCase import PloneTestCase as ptc |
---|
| 42 | >>> user, pwd = ptc.default_user, ptc.default_password |
---|
| 43 | >>> browser = Browser() |
---|
| 44 | |
---|
| 45 | Now check if captcha presented on the mentioned form. |
---|
| 46 | |
---|
| 47 | >>> browser.open(self.portal.absolute_url() + '/@@formlib-captcha-form') |
---|
| 48 | >>> open("/tmp/test.html", 'w').write(browser.contents) |
---|
| 49 | >>> "Type the code" in browser.contents |
---|
| 50 | True |
---|
| 51 | |
---|
[2806] | 52 | |
---|
| 53 | Now try to submit form with wrong captcha key. Error status message will |
---|
| 54 | be shown. |
---|
| 55 | |
---|
| 56 | >>> browser.open(self.portal.absolute_url() + '/@@formlib-captcha-form') |
---|
| 57 | >>> browser.getControl(name='form.captcha').value = "wrong captcha" |
---|
| 58 | >>> browser.getControl(name='form..hashkey').value = self.hashkey |
---|
| 59 | >>> browser.getControl(name="form.actions.save").click() |
---|
| 60 | >>> "Type the code" in browser.contents |
---|
| 61 | True |
---|
| 62 | >>> "Please re-enter validation code." in browser.contents |
---|
| 63 | True |
---|
| 64 | |
---|
| 65 | And now submit form with correct captcha key. |
---|
| 66 | |
---|
| 67 | >>> browser.open(self.portal.absolute_url() + '/@@formlib-captcha-form') |
---|
| 68 | >>> browser.getControl(name='form.captcha').value = self.captcha_word |
---|
| 69 | >>> browser.getControl(name='form..hashkey').value = self.hashkey |
---|
| 70 | >>> browser.getControl(name="form.actions.save").click() |
---|
| 71 | >>> "Please re-enter validation code." in browser.contents |
---|
| 72 | False |
---|
| 73 | |
---|
| 74 | No error shown. |
---|