source: trunk/FieldsWidgets.py @ 486

Revision 486, 10.0 KB checked in by vahur, 13 years ago (diff)

closes #530 spent 7h
this is actually the worst editor I have seen. But it's here now because I realized it too late...

  • 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', 'wym_browser.js', 'wym_config.js', 'wym_util.js', 'wym_wym.js'),
35        'helper_css' : ('wym_css.css',),
36        'cols' : 40,
37        'rows' : 10,
38        'WYSIWYMLines' : False,
39        'no_label' : True,
40    })
41
42registerWidget(WYSIWYMWidget,
43    title='WYSIWYM Widget',
44    description='WYSIWYM Widget',
45    used_for=('Products.LeMill.FieldsWidgets.WYSIWYMField',)
46)
47
48class WYSIWYMLinesWidget(WYSIWYMWidget):
49    _properties = WYSIWYMWidget._properties.copy()
50    _properties.update({
51        'rows' : 5,
52        'WYSIWYMLines' : True,
53    })
54
55registerWidget(WYSIWYMLinesWidget,
56    title='WYSIWYM Lines Widget',
57    description='WYSIWYM Lines Widget',
58    used_for=('Products.LeMill.FieldsWidgets.WYSIWYMLinesField',)
59)
60
61
62class TagsWidget(StringWidget):
63    _properties = StringWidget._properties.copy()
64    _properties.update({
65        'macro' : 'tagswidget',
66    })
67   
68registerWidget(TagsWidget,
69    title='Tags Widget',
70    description='Tags Widget',
71    used_for=('Products.LeMill.TagsField.TagsField',)
72)
73
74class MessengerWidget(TypesWidget):
75    _properties = TypesWidget._properties.copy()
76    _properties.update({
77        'macro' : 'messengerwidget',
78    },)
79
80    security = ClassSecurityInfo()
81
82    security.declarePublic('process_form')
83    def process_form(self, instance, field, form, empty_marker=None,
84                     emptyReturnsMarker=False):
85        """Concatenates address + messenger type, but if address already contains something like prefix (word:) then removes it.
86        """
87        name = field.getName()
88        otherName = "%s_other" % name
89        value = form.get(name, empty_marker)
90        othervalue = form.get(otherName, empty_marker)
91        if value is empty_marker:
92            value = form.get(name, empty_marker)
93        if (not value or value == empty_marker):
94            return empty_marker
95        if not othervalue == empty_marker:
96            if len(value.split(':'))>1:
97                value=value.split(':')
98                value=value[1]
99            value = "%s:%s" % (othervalue,value)
100        return value, {}
101
102
103registerWidget(MessengerWidget,
104    title='Messenger Widget',
105    description='Messenger Widget',
106    used_for=('Products.Archetypes.Field.StringField',)
107)
108
109class Age_rangeWidget(StringWidget):
110    _properties = StringWidget._properties.copy()
111    _properties.update({
112        'macro' : 'age_rangewidget',
113    })
114
115    security = ClassSecurityInfo()
116
117    security.declarePublic('process_form')
118    def process_form(self, instance, field, form, empty_marker=None,
119                     emptyReturnsMarker=False):
120        name = field.getName()
121        otherName = "%s_other" % name
122        value = form.get(name, empty_marker)
123        othervalue = form.get(otherName, empty_marker)
124        if value == othervalue == empty_marker and emptyReturnsMarker:
125            return empty_marker
126        # ugly validation -- can't use default validators here
127        if value != empty_marker:
128            if value.isdigit():
129                value=int(value)
130            else:
131                value=value.strip(letters+punctuation)
132                if value.isdigit():
133                    value=int(value)
134                else:
135                    value=0
136        if othervalue != empty_marker:
137            if othervalue.isdigit():
138                othervalue=int(othervalue)
139            else:
140                othervalue=othervalue.strip(letters+punctuation)
141                if othervalue.isdigit():
142                    othervalue=int(othervalue)
143                else:
144                    othervalue=0
145        if value>othervalue>0:
146            x=value
147            value=othervalue
148            othervalue=x
149        value=min(100,max(0,value))
150        othervalue=min(100,max(0,othervalue))
151        if value==othervalue==0:
152            return None, {}
153        else:
154            if value==0: value=''           
155            if othervalue==0: othervalue=''           
156            value = "%s-%s" % (value, othervalue)
157            return value, {}
158
159   
160registerWidget(Age_rangeWidget,
161    title='Age range Widget',
162    description='Age range Widget',
163    used_for=('Products.Archetypes.Field.StringField',)
164)
165
166class MobileWidget(StringWidget):
167    _properties = StringWidget._properties.copy()
168    _properties.update({
169        'macro' : 'mobilewidget',
170    })
171
172    security = ClassSecurityInfo()
173
174    security.declarePublic('process_form')
175    def process_form(self, instance, field, form, empty_marker=None,
176                     emptyReturnsMarker=False):
177        name = field.getName()
178        otherName = "%s_checkbox" % name
179        value = form.get(name, empty_marker)
180        othervalue = form.get(otherName, empty_marker)
181        if value == empty_marker and emptyReturnsMarker:
182            return empty_marker
183        if othervalue=="1":
184            value = "*SMS*%s" % value
185        return value, {}
186
187   
188registerWidget(MobileWidget,
189    title='Mobile Widget',
190    description='Mobile Widget',
191    used_for=('Products.Archetypes.Field.StringField',)
192)
193
194
195class CopyrightWidget(SelectionWidget):
196    _properties = SelectionWidget._properties.copy()
197    _properties.update({
198        'macro' : 'copyrightwidget',
199        'no_label' : True,
200    })
201   
202registerWidget(CopyrightWidget,
203    title='Copyright Widget',
204    description='Copyright selection Widget',
205    used_for=('Products.LeMill.StringField.StringField',)
206)
207
208
209
210class refImagesWidget(SelectionWidget):
211    _properties = SelectionWidget._properties.copy()
212    _properties.update({
213        'macro' : 'imageselection',
214        'helper_js' : ('js_helpers.js',),
215    })     
216
217class ImageSelectorWidget(SelectionWidget):
218    _properties = SelectionWidget._properties.copy()
219    _properties.update({
220        'macro' : 'widget_imageselector',
221        'helper_js' : ('js_helpers.js',),
222    })
223
224class TagsField(LinesField):
225    """ A field that stores tags """
226    __implements__ = LinesField.__implements__
227    _properties = LinesField._properties.copy()
228    _properties.update({
229        'widget' : TagsWidget,
230    })
231
232    security = ClassSecurityInfo()
233
234    def set(self, instance, value, **kwargs):
235        tags = []
236        tags_dirty = []
237    if isinstance(value,str):
238            value = value.lower()
239            tags_dirty = value.split(',')
240            value = tags_dirty
241            tags = []
242        if isinstance(value, tuple) or isinstance(value, list):
243            tags_dirty = value
244            [ tags.append(t.strip().lower()) for t in tags_dirty if t.strip().lower() not in tags ]
245            value = ','.join(tags)
246    if isinstance(value,str):
247        value = value.replace(',','\n')
248        LinesField.set(self, instance, value, **kwargs)
249                       
250    def getRaw(self, instance, **kwargs):
251        value = self.get(instance, **kwargs)
252        return ', '.join(value)
253
254    def get(self, instance, **kwargs):
255        return LinesField.get(self, instance, **kwargs)
256
257registerField(TagsField,
258               title='Tags Field',
259               description=('Tags Field'),
260               )
261
262
263
264
265
266class WYSIWYMField(StringField):
267    """A field that stores WYSIWYM strings"""
268    _properties = StringField._properties.copy()
269    _properties.update({
270        'widget' : WYSIWYMWidget,
271        'edit_accessor' :  "getRaw"
272    })   
273   
274    security = ClassSecurityInfo()
275   
276   
277    def getRaw(self, instance, **kwargs):
278        value = StringField.get(self, instance, **kwargs)
279        return value
280   
281    def get(self, instance, **kwargs):
282        ltool = getToolByName(instance, 'lemill_tool')
283        value = StringField.get(self, instance, **kwargs)
284        return ltool.render(value)
285
286    # render -method has moved to LeMillTools, because it is needed also outside the wysiwym field.
287
288registerField(WYSIWYMField,
289               title='WYSIWYM Field',
290               description=('WYSIWYM Field'),
291               )
292
293class WYSIWYMLinesField(LinesField):
294    """A field that stores a list of strings where every string is handled through WYSIWYM renderer """
295    _properties = LinesField._properties.copy()
296    _properties.update({
297        'widget' : WYSIWYMLinesWidget,
298        'edit_accessor' : "getRaw",
299    })
300
301    security = ClassSecurityInfo()
302
303
304    def getRaw(self, instance, **kwargs):
305        values = LinesField.get(self, instance, **kwargs)
306        return values
307   
308    def get(self, instance, **kwargs):
309        ltool = getToolByName(instance, 'lemill_tool')
310        values = LinesField.get(self, instance, **kwargs)
311        data=[ltool.listrender(value) for value in values]
312        if config.ZOPE_LINES_IS_TUPLE_TYPE:
313            return tuple(data)
314        else:
315            return data
316
317
318
319registerField(WYSIWYMLinesField,
320               title='WYSIWYM Lines Field',
321               description=('WYSIWYM Lines Field'),
322               )
Note: See TracBrowser for help on using the repository browser.