1 | from AccessControl import ClassSecurityInfo |
---|
2 | |
---|
3 | from Products.CMFCore.permissions import View |
---|
4 | from Products.CMFCore.Expression import getExprContext |
---|
5 | from Products.Archetypes.atapi import Schema |
---|
6 | from Products.Archetypes.Field import StringField |
---|
7 | from Products.Archetypes.Widget import StringWidget |
---|
8 | from Products.ATContentTypes.content.base import registerATCT |
---|
9 | |
---|
10 | from Products.TALESField import TALESString |
---|
11 | from Products.PloneFormGen.content.fields import FGStringField |
---|
12 | from Products.PloneFormGen.content.fieldsBase import finalizeFieldSchema |
---|
13 | |
---|
14 | from quintagroup.ploneformgen.readonlystringfield.widget import ReadonlyStringWidget |
---|
15 | from quintagroup.ploneformgen.readonlystringfield.config import PROJECTNAME |
---|
16 | |
---|
17 | class FGReadonlyStringField(FGStringField): |
---|
18 | """ A string entry field """ |
---|
19 | |
---|
20 | security = ClassSecurityInfo() |
---|
21 | |
---|
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 | |
---|
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 | |
---|
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') |
---|
74 | |
---|
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 | |
---|
85 | registerATCT(FGReadonlyStringField, PROJECTNAME) |
---|