Skip to content

Commit

Permalink
Restore serving MessageDialogs as text/html
Browse files Browse the repository at this point in the history
Now that Zope uses text/plain by default, MessageDialogs were served as
text/plain. Keep compatibility by returning a special string with
`asHTML` method, that ZPublisher.HTTPResponse.HTTPResponse.setBody
understands.

MessageDialogs are deprecated and do not integrate well in Zope >= 4
ZMI, but they are used in some old products.
  • Loading branch information
perrinjerome committed Dec 17, 2022
1 parent 6c199cd commit eb3c8c1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ https://zope.readthedocs.io/en/2.13/CHANGES.html
4.8.6 (unreleased)
------------------

- Adjust the old ``App.Dialogs.MessageDialog`` to be served as HTML after
from PR https://github.com/zopefoundation/Zope/pull/1075 .

4.8.5 (2022-12-17)
------------------
Expand Down
15 changes: 14 additions & 1 deletion src/App/Dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@
from App.special_dtml import HTML


MessageDialog = HTML("""
class MessageDialogHTML(HTML):
"""An special version of HTML which is always published as text/html
"""
def __call__(self, *args, **kw):
class _HTMLString(str):
"""A special string that will be published as text/html
"""
def asHTML(self):
return self
return _HTMLString(
super(MessageDialogHTML, self).__call__(*args, **kw))


MessageDialog = MessageDialogHTML("""
<HTML>
<HEAD>
<TITLE>&dtml-title;</TITLE>
Expand Down
18 changes: 18 additions & 0 deletions src/App/tests/testManagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,21 @@ def test_manage_content_type(self):

self.folder.manage(req)
self.assertIn('text/html', req.RESPONSE.getHeader('Content-Type'))


class TestDialogsMessageDialog(Testing.ZopeTestCase.ZopeTestCase):

def test_publish_set_content_type(self):
from App.Dialogs import MessageDialog

md = MessageDialog(
title='dialog title',
message='dialog message',
action='action'
)
self.assertIn('dialog title', md)
self.assertIn('dialog message', md)
self.assertIn('action', md)
req = self.app.REQUEST
req.RESPONSE.setBody(md)
self.assertIn('text/html', req.RESPONSE.getHeader('Content-Type'))

0 comments on commit eb3c8c1

Please sign in to comment.