source: trunk/FieldsWidgets.py @ 446

Revision 446, 9.9 KB checked in by jukka, 13 years ago (diff)

Closed #442, spent 20min. Now by adding 'no_label':True to widget definition in archetypes object, field can be displayed without label. This gets rid of stupid or obvious labels breaking page flow like 'BodyText:' or 'Copyright:'. Also if field is empty, the label is never shown. (This doesn't yet work for field 'video' in activities, but it should have its own widget soon that fixes this.)

  • Property svn:eol-style set to native
Line 
1# Copyright 2006 by the LeMill Team (see AUTHORS)
2#
3# This file is part of LeMill.
4#
5# LeMill is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# LeMill is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with LeMill; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19from AccessControl import ClassSecurityInfo
20from Products.Archetypes.Registry import registerField, registerWidget
21from Products.Archetypes.Field import StringField, LinesField
22from Products.Archetypes.Widget import TextAreaWidget, StringWidget, SelectionWidget, TypesWidget
23from config import to_unicode
24from Products.Archetypes import config
25from string import letters, punctuation
26from Products.CMFCore.utils import getToolByName
27
28import re
29
30class WYSIWYMWidget(TextAreaWidget):
31    _properties = TextAreaWidget._properties.copy()
32    _properties.update({
33        'macro' : 'wysiwym',
34        'helper_js' : ('js_helpers.js',),
35        'cols' : 40,
36        'rows' : 10,
37        'WYSIWYMLines' : False,
38        'no_label' : True,
39    })
40
41registerWidget(WYSIWYMWidget,
42    title='WYSIWYM Widget',
43    description='WYSIWYM Widget',
44    used_for=('Products.LeMill.FieldsWidgets.WYSIWYMField',)
45)
46
47class WYSIWYMLinesWidget(WYSIWYMWidget):
48    _properties = WYSIWYMWidget._properties.copy()
49    _properties.update({
50        'rows' : 5,
51        'WYSIWYMLines' : True,
52    })
53
54registerWidget(WYSIWYMLinesWidget,
55    title='WYSIWYM Lines Widget',
56    description='WYSIWYM Lines Widget',
57    used_for=('Products.LeMill.FieldsWidgets.WYSIWYMLinesField',)
58)
59
60
61class TagsWidget(StringWidget):
62    _properties = StringWidget._properties.copy()
63    _properties.update({
64        'macro' : 'tagswidget',
65    })
66   
67registerWidget(TagsWidget,
68    title='Tags Widget',
69    description='Tags Widget',
70    used_for=('Products.LeMill.TagsField.TagsField',)
71)
72
73class MessengerWidget(TypesWidget):
74    _properties = TypesWidget._properties.copy()
75    _properties.update({
76        'macro' : 'messengerwidget',
77    },)
78
79    security = ClassSecurityInfo()
80
81    security.declarePublic('process_form')
82    def process_form(self, instance, field, form, empty_marker=None,
83                     emptyReturnsMarker=False):
84        """Concatenates address + messenger type, but if address already contains something like prefix (word:) then removes it.
85        """
86        name = field.getName()
87        otherName = "%s_other" % name
88        value = form.get(name, empty_marker)
89        othervalue = form.get(otherName, empty_marker)
90        if value is empty_marker:
91            value = form.get(name, empty_marker)
92        if (not value or value == empty_marker):
93            return empty_marker
94        if not othervalue == empty_marker:
95            if len(value.split(':'))>1:
96                value=value.split(':')
97                value=value[1]
98            value = "%s:%s" % (othervalue,value)
99        return value, {}
100
101
102registerWidget(MessengerWidget,
103    title='Messenger Widget',
104    description='Messenger Widget',
105    used_for=('Products.Archetypes.Field.StringField',)
106)
107
108class Age_rangeWidget(StringWidget):
109    _properties = StringWidget._properties.copy()
110    _properties.update({
111        'macro' : 'age_rangewidget',
112    })
113
114    security = ClassSecurityInfo()
115
116    security.declarePublic('process_form')
117    def process_form(self, instance, field, form, empty_marker=None,
118                     emptyReturnsMarker=False):
119        name = field.getName()
120        otherName = "%s_other" % name
121        value = form.get(name, empty_marker)
122        othervalue = form.get(otherName, empty_marker)
123        if value == othervalue == empty_marker and emptyReturnsMarker:
124            return empty_marker
125        # ugly validation -- can't use default validators here
126        if value != empty_marker:
127            if value.isdigit():
128                value=int(value)
129            else:
130                value=value.strip(letters+punctuation)
131                if value.isdigit():
132                    value=int(value)
133                else:
134                    value=0
135        if othervalue != empty_marker:
136            if othervalue.isdigit():
137                othervalue=int(othervalue)
138            else:
139                othervalue=othervalue.strip(letters+punctuation)
140                if othervalue.isdigit():
141                    othervalue=int(othervalue)
142                else:
143                    othervalue=0
144        if value>othervalue>0:
145            x=value
146            value=othervalue
147            othervalue=x
148        value=min(100,max(0,value))
149        othervalue=min(100,max(0,othervalue))
150        if value==othervalue==0:
151            return None, {}
152        else:
153            if value==0: value=''           
154            if othervalue==0: othervalue=''           
155            value = "%s-%s" % (value, othervalue)
156            return value, {}
157
158   
159registerWidget(Age_rangeWidget,
160    title='Age range Widget',
161    description='Age range Widget',
162    used_for=('Products.Archetypes.Field.StringField',)
163)
164
165class MobileWidget(StringWidget):
166    _properties = StringWidget._properties.copy()
167    _properties.update({
168        'macro' : 'mobilewidget',
169    })
170
171    security = ClassSecurityInfo()
172
173    security.declarePublic('process_form')
174    def process_form(self, instance, field, form, empty_marker=None,
175                     emptyReturnsMarker=False):
176        name = field.getName()
177        otherName = "%s_checkbox" % name
178        value = form.get(name, empty_marker)
179        othervalue = form.get(otherName, empty_marker)
180        if value == empty_marker and emptyReturnsMarker:
181            return empty_marker
182        if othervalue=="1":
183            value = "*SMS*%s" % value
184        return value, {}
185
186   
187registerWidget(MobileWidget,
188    title='Mobile Widget',
189    description='Mobile Widget',
190    used_for=('Products.Archetypes.Field.StringField',)
191)
192
193
194class CopyrightWidget(SelectionWidget):
195    _properties = SelectionWidget._properties.copy()
196    _properties.update({
197        'macro' : 'copyrightwidget',
198        'no_label' : True,
199    })
200   
201registerWidget(CopyrightWidget,
202    title='Copyright Widget',
203    description='Copyright selection Widget',
204    used_for=('Products.LeMill.StringField.StringField',)
205)
206
207
208
209class refImagesWidget(SelectionWidget):
210    _properties = SelectionWidget._properties.copy()
211    _properties.update({
212        'macro' : 'imageselection',
213        'helper_js' : ('js_helpers.js',),
214    })     
215
216class ImageSelectorWidget(SelectionWidget):
217    _properties = SelectionWidget._properties.copy()
218    _properties.update({
219        'macro' : 'widget_imageselector',
220        'helper_js' : ('js_helpers.js',),
221    })
222
223class TagsField(LinesField):
224    """ A field that stores tags """
225    __implements__ = LinesField.__implements__
226    _properties = LinesField._properties.copy()
227    _properties.update({
228        'widget' : TagsWidget,
229    })
230
231    security = ClassSecurityInfo()
232
233    def set(self, instance, value, **kwargs):
234        tags = []
235        tags_dirty = []
236    if isinstance(value,str):
237            value = value.lower()
238            tags_dirty = value.split(',')
239            value = tags_dirty
240            tags = []
241        if isinstance(value, tuple) or isinstance(value, list):
242            tags_dirty = value
243            [ tags.append(t.strip().lower()) for t in tags_dirty if t.strip().lower() not in tags ]
244            value = ','.join(tags)
245    if isinstance(value,str):
246        value = value.replace(',','\n')
247        LinesField.set(self, instance, value, **kwargs)
248                       
249    def getRaw(self, instance, **kwargs):
250        value = self.get(instance, **kwargs)
251        return ', '.join(value)
252
253    def get(self, instance, **kwargs):
254        return LinesField.get(self, instance, **kwargs)
255
256registerField(TagsField,
257               title='Tags Field',
258               description=('Tags Field'),
259               )
260
261
262
263
264
265class WYSIWYMField(StringField):
266    """A field that stores WYSIWYM strings"""
267    _properties = StringField._properties.copy()
268    _properties.update({
269        'widget' : WYSIWYMWidget,
270        'edit_accessor' :  "getRaw"
271    })   
272   
273    security = ClassSecurityInfo()
274   
275   
276    def getRaw(self, instance, **kwargs):
277        value = StringField.get(self, instance, **kwargs)
278        return value
279   
280    def get(self, instance, **kwargs):
281        ltool = getToolByName(instance, 'lemill_tool')
282        value = StringField.get(self, instance, **kwargs)
283        return ltool.render(value)
284
285    # render -method has moved to LeMillTools, because it is needed also outside the wysiwym field.
286
287registerField(WYSIWYMField,
288               title='WYSIWYM Field',
289               description=('WYSIWYM Field'),
290               )
291
292class WYSIWYMLinesField(LinesField):
293    """A field that stores a list of strings where every string is handled through WYSIWYM renderer """
294    _properties = LinesField._properties.copy()
295    _properties.update({
296        'widget' : WYSIWYMLinesWidget,
297        'edit_accessor' : "getRaw",
298    })
299
300    security = ClassSecurityInfo()
301
302
303    def getRaw(self, instance, **kwargs):
304        values = LinesField.get(self, instance, **kwargs)
305        return values
306   
307    def get(self, instance, **kwargs):
308        ltool = getToolByName(instance, 'lemill_tool')
309        values = LinesField.get(self, instance, **kwargs)
310        data=[ltool.listrender(value) for value in values]
311        if config.ZOPE_LINES_IS_TUPLE_TYPE:
312            return tuple(data)
313        else:
314            return data
315
316
317
318registerField(WYSIWYMLinesField,
319               title='WYSIWYM Lines Field',
320               description=('WYSIWYM Lines Field'),
321               )
Note: See TracBrowser for help on using the repository browser.