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