1 | from zope.event import notify |
---|
2 | from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent |
---|
3 | from Products.CMFCore.permissions import View |
---|
4 | |
---|
5 | from Products.Archetypes.atapi import * |
---|
6 | from Products.ATContentTypes.content.base import registerATCT |
---|
7 | |
---|
8 | from Products.PloneFormGen.content.fields import FGStringField |
---|
9 | from Products.PloneFormGen.content.fieldsBase import BareFieldSchema |
---|
10 | from Products.PloneFormGen.content.fieldsBase import BaseFormField |
---|
11 | from Products.PloneFormGen.content.fieldsBase import BaseFieldSchemaStringDefault |
---|
12 | |
---|
13 | from quintagroup.pfg.captcha.config import PROJECTNAME |
---|
14 | from quintagroup.pfg.captcha.widget import CaptchaWidget |
---|
15 | |
---|
16 | CAPTCHA_ID = 'key' |
---|
17 | HIDDEN_FIELDS = [ |
---|
18 | 'title', |
---|
19 | 'description', |
---|
20 | 'required', |
---|
21 | 'hidden', |
---|
22 | 'fgTDefault', |
---|
23 | 'fgTEnabled', |
---|
24 | 'fgDefault', |
---|
25 | 'fgTValidator'] |
---|
26 | |
---|
27 | def finalizeCaptchaFieldSchema( schema ): |
---|
28 | schema['title'].default= 'key' |
---|
29 | for field in HIDDEN_FIELDS: |
---|
30 | schema[field].widget.visible = {'view':'invisible','edit':'invisible'} |
---|
31 | |
---|
32 | CaptchaFieldSchema = BaseFieldSchemaStringDefault.copy() |
---|
33 | finalizeCaptchaFieldSchema( CaptchaFieldSchema ) |
---|
34 | |
---|
35 | def addCaptchaField(self, id, **kwargs): |
---|
36 | id = CAPTCHA_ID |
---|
37 | obj = CaptchaField(id) |
---|
38 | notify(ObjectCreatedEvent(obj)) |
---|
39 | self._setObject(id, obj) |
---|
40 | obj = self._getOb(id) |
---|
41 | obj.initializeArchetype(**kwargs) |
---|
42 | notify(ObjectModifiedEvent(obj)) |
---|
43 | return obj.getId() |
---|
44 | |
---|
45 | class CaptchaField(FGStringField): |
---|
46 | |
---|
47 | _at_rename_after_creation = False |
---|
48 | schema = CaptchaFieldSchema |
---|
49 | |
---|
50 | def __init__(self, oid, **kwargs): |
---|
51 | """ initialize class """ |
---|
52 | BaseFormField.__init__(self, oid, **kwargs) |
---|
53 | |
---|
54 | # set a preconfigured field as an instance attribute |
---|
55 | self.fgField = StringField('fg_string_field', |
---|
56 | searchable=0, |
---|
57 | required=1, |
---|
58 | write_permission = View, |
---|
59 | validators=('isCaptchaCorrect',), |
---|
60 | widget=CaptchaWidget(), |
---|
61 | ) |
---|
62 | |
---|
63 | registerATCT(CaptchaField, PROJECTNAME) |
---|