source: products/quintagroup.pfg.captcha/trunk/quintagroup/pfg/captcha/field.py @ 2098

Last change on this file since 2098 was 2098, checked in by mylan, 10 years ago

#174: added tests for CaptchaField?

  • Property svn:eol-style set to native
File size: 1.9 KB
Line 
1from zope.event import notify
2from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent
3from Products.CMFCore.permissions import View
4
5from Products.Archetypes.atapi import *
6from Products.ATContentTypes.content.base import registerATCT
7
8from Products.PloneFormGen.content.fields import FGStringField
9from Products.PloneFormGen.content.fieldsBase import BareFieldSchema
10from Products.PloneFormGen.content.fieldsBase import BaseFormField
11from Products.PloneFormGen.content.fieldsBase import BaseFieldSchemaStringDefault
12
13from quintagroup.pfg.captcha.config import PROJECTNAME
14from quintagroup.pfg.captcha.widget import CaptchaWidget
15
16CAPTCHA_ID = 'key'
17HIDDEN_FIELDS = [
18    'title', 
19    'description',
20    'required', 
21    'hidden', 
22    'fgTDefault', 
23    'fgTEnabled', 
24    'fgDefault',
25    'fgTValidator']
26
27def finalizeCaptchaFieldSchema( schema ):
28    schema['title'].default= 'key'
29    for field in HIDDEN_FIELDS:
30        schema[field].widget.visible = {'view':'invisible','edit':'invisible'}
31
32CaptchaFieldSchema = BaseFieldSchemaStringDefault.copy()
33finalizeCaptchaFieldSchema( CaptchaFieldSchema )
34
35def 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
45class 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
63registerATCT(CaptchaField, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.