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