[KSS-checkins] r35995 - in kukit/kss.core/trunk: . browser tests
reebalazs at codespeak.net
reebalazs at codespeak.net
Wed Dec 27 13:51:51 CET 2006
Author: reebalazs
Date: Wed Dec 27 13:51:49 2006
New Revision: 35995
Added:
kukit/kss.core/trunk/browser/errorresponse.pt
kukit/kss.core/trunk/browserview.py
kukit/kss.core/trunk/tests/test_browserview.py
Modified:
kukit/kss.core/trunk/configure.zcml
Log:
Implement the error attach code with browser views
Added: kukit/kss.core/trunk/browser/errorresponse.pt
==============================================================================
--- (empty file)
+++ kukit/kss.core/trunk/browser/errorresponse.pt Wed Dec 27 13:51:49 2006
@@ -0,0 +1,11 @@
+<?xml version="1.0"?>
+<root xmlns:kukit="http://www.kukit.org/commands/1.0"
+ xmlns:tal="http://xml.zope.org/namespaces/tal"
+ xmlns:metal="http://xml.zope.org/namespaces/metal">
+<kukit:commands>
+ <kukit:command name="error">
+ <kukit:param name="type" tal:content="options/type | nothing">system</kukit:param>
+ <kukit:param name="message" tal:content="options/message">Exception: reason</kukit:param>
+ </kukit:command>
+</kukit:commands>
+</root>
Added: kukit/kss.core/trunk/browserview.py
==============================================================================
--- (empty file)
+++ kukit/kss.core/trunk/browserview.py Wed Dec 27 13:51:49 2006
@@ -0,0 +1,42 @@
+# -*- coding: UTF-8 -*-
+# Copyright (c) 2006
+# Authors: KSS project contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as published
+# by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+# 02111-1307, USA.
+#
+
+import cgi
+from Products.Five.browser import BrowserView
+#from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+from zope.pagetemplate.pagetemplatefile import PageTemplateFile
+
+class KssBrowserView(BrowserView):
+
+ # XML output gets rendered via a page template
+ # XXX note: barefoot rendering, use python: only after zope2.9
+ render_error = PageTemplateFile('browser/errorresponse.pt')
+
+ def attach_error(self, err_type, err_value):
+ 'Attach the error payload on the response'
+ message = '%s: %s' % (err_type, err_value)
+ message = cgi.escape(message)
+ payload = self.render_error(type='system', message=message)
+ self.attach_payload(payload)
+
+ def attach_payload(self, payload, header_name='X-KSSCOMMANDS'):
+ 'Attach the commands on the response'
+ # get rid of newlines
+ payload = payload.replace('\n', ' ')
+ self.request.RESPONSE.setHeader(header_name, payload)
Modified: kukit/kss.core/trunk/configure.zcml
==============================================================================
--- kukit/kss.core/trunk/configure.zcml (original)
+++ kukit/kss.core/trunk/configure.zcml Wed Dec 27 13:51:49 2006
@@ -43,4 +43,13 @@
factory=".commands.CommandView"
/>
+ <!-- (non-kss) browser view -->
+ <browser:page
+ for="*"
+ name="kss_view"
+ permission="zope.Public"
+ class=".browserview.KssBrowserView"
+ allowed_attributes="attach_error"
+ />
+
</configure>
Added: kukit/kss.core/trunk/tests/test_browserview.py
==============================================================================
--- (empty file)
+++ kukit/kss.core/trunk/tests/test_browserview.py Wed Dec 27 13:51:49 2006
@@ -0,0 +1,40 @@
+# -*- coding: ISO-8859-15 -*-
+# Copyright (c) 2005-2006
+# Authors: KSS project contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as published
+# by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+# 02111-1307, USA.
+#
+import unittest
+from base import AzaxViewTestCase
+
+class TestBrowserView(AzaxViewTestCase):
+
+ def test_attach_error(self):
+ 'Test if errors are attached properly'
+ # just render any page
+ context = self.folder
+ view = context.restrictedTraverse('@@kss_view')
+ self.assert_(view is not None)
+ view.attach_error(err_type='TheError', err_value='the_<>message\n\n')
+ response = view.request.RESPONSE
+ header = response.getHeader('x-ksscommands')
+ self.assert_('the_&lt;&gt;message' in header) # no < > in the message
+ self.assert_('\n' not in header) # no /n in the payload: would destroy the page
+ self.assertEqual(header, '<?xml version="1.0"?> <root xmlns:kukit="http://www.kukit.org/commands/1.0"> <kukit:commands> \t<kukit:command name="error"> \t\t<kukit:param name="type">system</kukit:param> \t\t<kukit:param name="message">TheError: the_&lt;&gt;message </kukit:param> \t</kukit:command> </kukit:commands> </root> ')
+
+def test_suite():
+ suites = []
+ suites.append(unittest.makeSuite(TestBrowserView))
+ return unittest.TestSuite(suites)
More information about the Kukit-checkins
mailing list