source: products/quintagroup.plonecaptchas/trunk/quintagroup/plonecaptchas/captcha.py @ 3457

Last change on this file since 3457 was 3457, checked in by vmaksymiv, 12 years ago

merged with captcha_provider branch

File size: 1.7 KB
RevLine 
[3428]1from plone.app.discussion.browser.captcha import CaptchaExtender
2from quintagroup.z3cform.captcha.widget import CaptchaWidgetFactory
3from plone.app.discussion import vocabularies
4from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
5from quintagroup.plonecaptchas.interfaces import IQGDiscussionCaptchas
[3457]6from quintagroup.plonecaptchas.interfaces import ICaptchaProvider
[3428]7from zope.interface import Interface
[3457]8from zope.component import adapts, getUtilitiesFor, queryUtility
[3428]9from plone.app.discussion.browser.comments import CommentForm
10
11
[3457]12class CaptchaProvider(object):
13
14    def __init__(self):
15        self.widget_factory = CaptchaWidgetFactory
16
17
[3428]18class CaptchaExtender(CaptchaExtender):
19    adapts(Interface, IQGDiscussionCaptchas, CommentForm)
20
21    def update(self):
22        super(CaptchaExtender, self).update()
[3457]23        if self.isAnon:
24            captcha_provider = queryUtility(ICaptchaProvider,
25                                            name=self.captcha)
26            if captcha_provider:
27                self.form.fields['captcha'].widgetFactory = \
28                                                captcha_provider.widget_factory
[3428]29                self.form.fields['captcha'].mode = None
30
31
32def captcha_vocabulary(context):
33    """ Extend captcha vocabulary with quintagroup.plonecaptchas"""
34    terms = vocabularies.captcha_vocabulary(context)._terms
[3457]35    captchas = set((t.value for t in terms))
36
37    providers = getUtilitiesFor(ICaptchaProvider)
38    for name, util in providers:
39        if name and name not in captchas:
40            terms.append(SimpleTerm(value=name,
41                                    token=name,
42                                    title=name.capitalize()))
[3428]43    return SimpleVocabulary(terms)
Note: See TracBrowser for help on using the repository browser.