This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
plone.formwidget.captcha/plone/formwidget/captcha
Thomas Desvenain afac46f0b7 avoid if/else structures for hashlib/sha compatibility
svn path=/plone.formwidget.captcha/trunk/; revision=48798
2011-04-13 22:25:10 +00:00
..
browser avoid if/else structures for hashlib/sha compatibility 2011-04-13 22:25:10 +00:00
demo comments added. 2009-08-12 11:45:50 +00:00
README.txt remove not working doctest part. 2009-08-12 11:13:05 +00:00
__init__.py allow "from plone.formwidget.captcha import CaptchaValidator". 2009-08-13 10:10:32 +00:00
captcha_input.pt make captcha field required. 2009-08-11 20:48:47 +00:00
configure.zcml Move to plone.app.discussion-captcha feature declaration to meta.zcml. 2010-06-10 14:52:07 +00:00
interfaces.py fix interfaces import for ICaptchaWidget; make demo captcha_form work. 2009-08-10 14:01:33 +00:00
meta.zcml Move to plone.app.discussion-captcha feature declaration to meta.zcml. 2010-06-10 14:52:07 +00:00
testing.zcml add further doctests (failing). 2009-08-12 07:04:47 +00:00
tests.py add further doctests (failing). 2009-08-12 07:04:47 +00:00
validator.py whitespace 2010-07-13 09:13:16 +00:00
widget.pt Initial import. 2009-08-06 22:09:24 +00:00
widget.py whitespace 2010-07-13 09:13:16 +00:00

README.txt

Captcha widget
==============

plone.formwidget.captcha provides a captcha widget based on the
collective.captcha widget.

    >>> from plone.formwidget.captcha import CaptchaFieldWidget

First, set up a simple test form and context.

    >>> from zope.interface import alsoProvides
    >>> from zope.publisher.browser import TestRequest
    >>> from zope.annotation.interfaces import IAttributeAnnotatable
    >>> from z3c.form.interfaces import IFormLayer

    >>> def make_request(path, form={}):
    ...     request = TestRequest()
    ...     request.form.update(form)
    ...     alsoProvides(request, IFormLayer)
    ...     alsoProvides(request, IAttributeAnnotatable)
    ...     request._traversed_names = path.split('/')
    ...     return request

    >>> from zope.interface import Interface
    >>> from zope import schema
    >>> from z3c.form import form, field, button
    >>> from plone.z3cform.layout import wrap_form

    >>> class ICaptchaForm(Interface):
    ...     subject = schema.TextLine(title=u"Subject",
    ...                               description=u"",
    ...                               required=True)
    ...
    ...     captcha = schema.TextLine(title=u"Captcha",
    ...                               description=u"",
    ...                               required=False)

    >>> from z3c.form.interfaces import IFieldsForm
    >>> from zope.interface import implements
    >>> class CaptchaForm(form.Form):
    ...     implements(ICaptchaForm)
    ...     fields = field.Fields(ICaptchaForm)
    ...     fields['captcha'].widgetFactory = CaptchaFieldWidget
    ...
    ...     @button.buttonAndHandler(u'Apply')
    ...     def handleApply(self, action):
    ...         data, errors = self.extractData()
    ...         if data.has_key('captcha'):
    ...             # Verify the user input against the captcha
    ...             captcha = CaptchaValidator(self.context, self.request, None, ICaptchaForm['captcha'], None)
    ...             if data.has_key('subject') and captcha.validate(data['captcha']):
    ...                 # if captcha validation passes, print the subject
    ...                 print data['subject']
    ...         return


    >>> form_view = wrap_form(CaptchaForm)

    >>> from zope.component import provideAdapter
    >>> from zope.publisher.interfaces.browser import IBrowserRequest
    >>> from zope.interface import Interface

    >>> provideAdapter(adapts=(ICaptchaForm, IBrowserRequest),
    ...                provides=Interface,
    ...                factory=form_view,
    ...                name=u"captcha-form")

    >>> from OFS.SimpleItem import SimpleItem
    >>> class Bar(SimpleItem):
    ...     __allow_access_to_unprotected_subobjects__ = 1
    ...     implements(ICaptchaForm)
    ...
    ...     def __init__(self, id):
    ...         self.id = id
    ...         self.subject = u""
    ...         self.captcha = u""

Let us now look up the form and attempt to render the widget.

    >>> from zope.component import getMultiAdapter
    >>> from OFS.Application import Application
    >>> app = Application()
    >>> app.REQUEST = make_request('/') # eeeevil
    >>> context = Bar('bar').__of__(app)

Simulates traversal:

    >>> request = make_request('bar/@@captcha-form')
    >>> form_view = getMultiAdapter((context, request), name=u"captcha-form").__of__(context)
    >>> form_view.__name__ = 'captcha-form'

Todo