[322] | 1 | from AccessControl import ClassSecurityInfo |
---|
| 2 | |
---|
| 3 | from Products.CMFCore.permissions import View |
---|
[826] | 4 | from Products.CMFCore.Expression import getExprContext |
---|
| 5 | from Products.Archetypes.atapi import Schema |
---|
[322] | 6 | from Products.Archetypes.Field import StringField |
---|
[826] | 7 | from Products.Archetypes.Widget import StringWidget |
---|
[322] | 8 | from Products.ATContentTypes.content.base import registerATCT |
---|
| 9 | |
---|
[826] | 10 | from Products.TALESField import TALESString |
---|
[322] | 11 | from Products.PloneFormGen.content.fields import FGStringField |
---|
[826] | 12 | from Products.PloneFormGen.content.fieldsBase import finalizeFieldSchema |
---|
[322] | 13 | |
---|
[826] | 14 | from quintagroup.ploneformgen.readonlystringfield.widget import ReadonlyStringWidget |
---|
| 15 | from quintagroup.ploneformgen.readonlystringfield.config import PROJECTNAME |
---|
[322] | 16 | |
---|
| 17 | class FGReadonlyStringField(FGStringField): |
---|
| 18 | """ A string entry field """ |
---|
| 19 | |
---|
| 20 | security = ClassSecurityInfo() |
---|
| 21 | |
---|
[826] | 22 | schema = FGStringField.schema.copy() + Schema(( |
---|
| 23 | TALESString('editMode', |
---|
| 24 | searchable=0, |
---|
| 25 | required=0, |
---|
| 26 | validators=('talesvalidator',), |
---|
| 27 | default='', |
---|
| 28 | widget=StringWidget(label="Edit Mode Expression", |
---|
| 29 | description=""" |
---|
| 30 | A TALES expression that will be evaluated when the form is displayed |
---|
| 31 | to know whether or not to make field editable. |
---|
| 32 | Leave empty if you need read only field in any case. Your expression |
---|
| 33 | should evaluate as a string. |
---|
| 34 | PLEASE NOTE: errors in the evaluation of this expression will cause |
---|
| 35 | an error on form display. |
---|
| 36 | """, |
---|
| 37 | size=70, |
---|
| 38 | i18n_domain = "quintagroup.ploneformgen.readonlystringfield", |
---|
| 39 | label_msgid = "label_editmode_text", |
---|
| 40 | description_msgid = "help_editmode_text", |
---|
| 41 | ), |
---|
| 42 | ), |
---|
| 43 | )) |
---|
| 44 | |
---|
| 45 | # hide references & discussion |
---|
| 46 | finalizeFieldSchema(schema, folderish=True, moveDiscussion=False) |
---|
| 47 | |
---|
[322] | 48 | # Standard content type setup |
---|
| 49 | portal_type = meta_type = 'FormReadonlyStringField' |
---|
| 50 | archetype_name = 'Readonly String Field' |
---|
| 51 | content_icon = 'StringField.gif' |
---|
| 52 | typeDescription= 'A readonly string field' |
---|
| 53 | |
---|
| 54 | def __init__(self, oid, **kwargs): |
---|
| 55 | """ initialize class """ |
---|
| 56 | |
---|
| 57 | super(FGReadonlyStringField, self).__init__(oid, **kwargs) |
---|
| 58 | |
---|
| 59 | # set a preconfigured field as an instance attribute |
---|
| 60 | self.fgField = StringField('fg_string_field', |
---|
| 61 | searchable=0, |
---|
| 62 | required=0, |
---|
| 63 | write_permission = View, |
---|
| 64 | validators=('isNotTooLong',), |
---|
| 65 | widget=ReadonlyStringWidget(), |
---|
| 66 | ) |
---|
| 67 | |
---|
[826] | 68 | def fgPrimeDefaults(self, request, contextObject=None): |
---|
| 69 | super(FGReadonlyStringField, self).fgPrimeDefaults(request, |
---|
| 70 | contextObject=contextObject) |
---|
| 71 | editable = self.isEditable(contextObject) |
---|
| 72 | if editable: |
---|
| 73 | request.set('%s_editable' % self.fgField.__name__, '1') |
---|
[322] | 74 | |
---|
[826] | 75 | def isEditable(self, context=None): |
---|
| 76 | if self.getRawEditMode(): |
---|
| 77 | if context: |
---|
| 78 | value = self.getEditMode( |
---|
| 79 | expression_context=getExprContext(self, context)) |
---|
| 80 | else: |
---|
| 81 | value = self.getEditMode() |
---|
| 82 | return value |
---|
| 83 | return False |
---|
| 84 | |
---|
[322] | 85 | registerATCT(FGReadonlyStringField, PROJECTNAME) |
---|