source: trunk/WYSIWYMWidget.py @ 44

Revision 44, 2.8 KB checked in by tarmo, 14 years ago (diff)

Cleaned up unnecessary import statements.

Line 
1from AccessControl import ClassSecurityInfo
2from Products.Archetypes.Registry import registerWidget
3from Products.Archetypes.Widget import TypesWidget
4
5import re
6
7class WYSIWYMTranslator:
8   
9    def getBody(self):
10        body=str(self.body)
11        #raise 'FOO', str(body)
12        body=re.sub(r'\*(.*?)\*', r'<b>\1</b>',body)
13        body=re.sub(r'\_(.*?)\_', r'<i>\1</i>',body)
14        return body
15
16class WYSIWYMWidget(TypesWidget):
17    _properties = TypesWidget._properties.copy()
18    _properties.update({
19        # Macro is located by trying all registered skin folders to find a template with this path
20        'macro' : "wysiwym",
21        'rows'  : 5,
22        'cols'  : 40,
23        'format': 1,
24        'allow_file_upload': False,
25        })
26
27    security = ClassSecurityInfo()
28
29    # XXX
30    security.declarePublic('process_form')
31    def process_form(self, instance, field, form, empty_marker=None,
32                     emptyReturnsMarker=False):
33        """complex form processing, includes handling for text
34        formatting and file objects"""
35        # TODO: This is not right
36        # This is basically the old processing chain from base object
37        text_format = None
38        isFile = False
39        value = None
40
41        # text field with formatting
42        if hasattr(field, 'allowable_content_types') and \
43           field.allowable_content_types:
44            # was a mimetype specified
45            format_field = "%s_text_format" % field.getName()
46            text_format = form.get(format_field, empty_marker)
47
48        # or a file?
49        fileobj = form.get('%s_file' % field.getName(), empty_marker)
50
51        if fileobj is not empty_marker:
52
53            filename = getattr(fileobj, 'filename', '') or \
54                       (isinstance(fileobj, FileType) and \
55                        getattr(fileobj, 'name', ''))
56
57            if filename:
58                value = fileobj
59                isFile = True
60
61        kwargs = {}
62        if not value:
63            value = form.get(field.getName(), empty_marker)
64            if text_format is not empty_marker and text_format:
65                kwargs['mimetype'] = text_format
66
67        if value is empty_marker: return empty_marker
68
69        if value and not isFile:
70            # Value filled, no file uploaded
71            if kwargs.get('mimetype') == str(field.getContentType(instance)) \
72                   and instance.isBinary(field.getName()):
73                # Was an uploaded file, same content type
74                del kwargs['mimetype']
75
76        return value, kwargs
77
78registerWidget(WYSIWYMWidget,
79               title='WYSIWYM Widget',
80               description=('WYSIWYM Widget'),
81               used_for=('Products.Archetypes.Field.TextField',)
82               )
Note: See TracBrowser for help on using the repository browser.