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 StringField |
---|
6 | from Products.ATContentTypes.content.base import registerATCT |
---|
7 | |
---|
8 | from Products.PloneFormGen.content.fields import FGStringField |
---|
9 | from Products.PloneFormGen.content.fieldsBase import BaseFormField |
---|
10 | from Products.PloneFormGen.content.fieldsBase \ |
---|
11 | 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 | |
---|
28 | def finalizeCaptchaFieldSchema(schema): |
---|
29 | schema['title'].default = 'key' |
---|
30 | for field in HIDDEN_FIELDS: |
---|
31 | schema[field].widget.visible = {'view': 'invisible', |
---|
32 | 'edit': 'invisible'} |
---|
33 | |
---|
34 | CaptchaFieldSchema = BaseFieldSchemaStringDefault.copy() |
---|
35 | finalizeCaptchaFieldSchema(CaptchaFieldSchema) |
---|
36 | |
---|
37 | |
---|
38 | def addCaptchaField(self, id, **kwargs): |
---|
39 | obj = CaptchaField(id) |
---|
40 | notify(ObjectCreatedEvent(obj)) |
---|
41 | self._setObject(id, obj) |
---|
42 | obj = self._getOb(id) |
---|
43 | obj.initializeArchetype(**kwargs) |
---|
44 | notify(ObjectModifiedEvent(obj)) |
---|
45 | return obj.getId() |
---|
46 | |
---|
47 | |
---|
48 | class CaptchaField(FGStringField): |
---|
49 | |
---|
50 | _at_rename_after_creation = True |
---|
51 | schema = CaptchaFieldSchema |
---|
52 | |
---|
53 | def __init__(self, oid, **kwargs): |
---|
54 | """ initialize class """ |
---|
55 | BaseFormField.__init__(self, oid, **kwargs) |
---|
56 | |
---|
57 | # set a preconfigured field as an instance attribute |
---|
58 | self.fgField = StringField('fg_string_field', |
---|
59 | searchable=0, |
---|
60 | required=1, |
---|
61 | write_permission=View, |
---|
62 | validators=('isCaptchaCorrect',), |
---|
63 | widget=CaptchaWidget(), |
---|
64 | ) |
---|
65 | |
---|
66 | registerATCT(CaptchaField, PROJECTNAME) |
---|