source: trunk/WYSIWYMWidget.py @ 42

Revision 42, 3.3 KB checked in by laszlo, 14 years ago (diff)

Partial implementation of WYSIWYM Editor. references #89 spent 1h remaining 1h

Line 
1from copy import deepcopy
2from types import DictType, FileType, ListType, StringTypes
3
4from Products.CMFCore.utils import getToolByName
5from Products.CMFCore.Expression import Expression
6from Products.CMFCore.Expression import createExprContext
7
8from Products.Archetypes.utils import className
9from Products.Archetypes.utils import unique
10from Products.Archetypes.utils import capitalize
11from Products.generator.widget import macrowidget
12from Products.Archetypes.debug import log
13from Products.Archetypes.Registry import registerPropertyType
14from Products.Archetypes.Registry import registerWidget
15
16from ExtensionClass import Base
17from AccessControl import ClassSecurityInfo
18from Globals import InitializeClass
19from Acquisition import aq_base
20from Acquisition import Implicit
21
22from Products.Archetypes.Widget import TypesWidget
23
24import re
25
26class WYSIWYMTranslator:
27   
28    def getBody(self):
29        body=str(self.body)
30        #raise 'FOO', str(body)
31        body=re.sub(r'\*(.*?)\*', r'<b>\1</b>',body)
32        body=re.sub(r'\_(.*?)\_', r'<i>\1</i>',body)
33        return body
34
35class WYSIWYMWidget(TypesWidget):
36    _properties = TypesWidget._properties.copy()
37    _properties.update({
38        'macro' : "wysiwym",
39        'rows'  : 5,
40        'cols'  : 40,
41        'format': 1,
42        'allow_file_upload': False,
43        })
44
45    security = ClassSecurityInfo()
46
47    # XXX
48    security.declarePublic('process_form')
49    def process_form(self, instance, field, form, empty_marker=None,
50                     emptyReturnsMarker=False):
51        """complex form processing, includes handling for text
52        formatting and file objects"""
53        # TODO: This is not right
54        # This is basically the old processing chain from base object
55        text_format = None
56        isFile = False
57        value = None
58
59        # text field with formatting
60        if hasattr(field, 'allowable_content_types') and \
61           field.allowable_content_types:
62            # was a mimetype specified
63            format_field = "%s_text_format" % field.getName()
64            text_format = form.get(format_field, empty_marker)
65
66        # or a file?
67        fileobj = form.get('%s_file' % field.getName(), empty_marker)
68
69        if fileobj is not empty_marker:
70
71            filename = getattr(fileobj, 'filename', '') or \
72                       (isinstance(fileobj, FileType) and \
73                        getattr(fileobj, 'name', ''))
74
75            if filename:
76                value = fileobj
77                isFile = True
78
79        kwargs = {}
80        if not value:
81            value = form.get(field.getName(), empty_marker)
82            if text_format is not empty_marker and text_format:
83                kwargs['mimetype'] = text_format
84
85        if value is empty_marker: return empty_marker
86
87        if value and not isFile:
88            # Value filled, no file uploaded
89            if kwargs.get('mimetype') == str(field.getContentType(instance)) \
90                   and instance.isBinary(field.getName()):
91                # Was an uploaded file, same content type
92                del kwargs['mimetype']
93
94        return value, kwargs
95
96registerWidget(WYSIWYMWidget,
97               title='WYSIWYM Widget',
98               description=('WYSIWYM Widget'),
99               used_for=('Products.Archetypes.Field.TextField',)
100               )
Note: See TracBrowser for help on using the repository browser.