| [263] | 1 | from zope.event import notify |
|---|
| 2 | from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent |
|---|
| [1] | 3 | from Products.CMFCore.permissions import View |
|---|
| 4 | |
|---|
| [3168] | 5 | from Products.Archetypes.atapi import StringField |
|---|
| [1] | 6 | from Products.ATContentTypes.content.base import registerATCT |
|---|
| 7 | |
|---|
| 8 | from Products.PloneFormGen.content.fields import FGStringField |
|---|
| [2091] | 9 | from Products.PloneFormGen.content.fieldsBase import BaseFormField |
|---|
| [3168] | 10 | from Products.PloneFormGen.content.fieldsBase \ |
|---|
| 11 | import BaseFieldSchemaStringDefault |
|---|
| [1] | 12 | |
|---|
| [1973] | 13 | from quintagroup.pfg.captcha.config import PROJECTNAME |
|---|
| [2085] | 14 | from quintagroup.pfg.captcha.widget import CaptchaWidget |
|---|
| [1] | 15 | |
|---|
| [263] | 16 | CAPTCHA_ID = 'key' |
|---|
| [2098] | 17 | HIDDEN_FIELDS = [ |
|---|
| [3168] | 18 | 'title', |
|---|
| [2098] | 19 | 'description', |
|---|
| [3168] | 20 | 'required', |
|---|
| 21 | 'hidden', |
|---|
| 22 | 'fgTDefault', |
|---|
| 23 | 'fgTEnabled', |
|---|
| [2098] | 24 | 'fgDefault', |
|---|
| 25 | 'fgTValidator'] |
|---|
| [263] | 26 | |
|---|
| [3168] | 27 | |
|---|
| 28 | def finalizeCaptchaFieldSchema(schema): |
|---|
| 29 | schema['title'].default = 'key' |
|---|
| [2098] | 30 | for field in HIDDEN_FIELDS: |
|---|
| [3168] | 31 | schema[field].widget.visible = {'view': 'invisible', |
|---|
| 32 | 'edit': 'invisible'} |
|---|
| [263] | 33 | |
|---|
| 34 | CaptchaFieldSchema = BaseFieldSchemaStringDefault.copy() |
|---|
| [3168] | 35 | finalizeCaptchaFieldSchema(CaptchaFieldSchema) |
|---|
| [263] | 36 | |
|---|
| [3168] | 37 | |
|---|
| [263] | 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 | |
|---|
| [3168] | 47 | |
|---|
| [1] | 48 | class CaptchaField(FGStringField): |
|---|
| 49 | |
|---|
| [3430] | 50 | _at_rename_after_creation = True |
|---|
| [263] | 51 | schema = CaptchaFieldSchema |
|---|
| [1] | 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, |
|---|
| [3168] | 61 | write_permission=View, |
|---|
| [1] | 62 | validators=('isCaptchaCorrect',), |
|---|
| 63 | widget=CaptchaWidget(), |
|---|
| 64 | ) |
|---|
| 65 | |
|---|
| [263] | 66 | registerATCT(CaptchaField, PROJECTNAME) |
|---|