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