Skip to content
Browse files

Add delayed write hack

  • Loading branch information...
1 parent 562b29b commit d9cedfafb55a8e0452485ac1e83d95d1dd772724 @optilude optilude committed
View
2 buildout.cfg
@@ -1,6 +1,6 @@
[buildout]
extends =
- http://svn.plone.org/svn/collective/buildout/plonetest/plone-4.0.x.cfg
+ http://svn.plone.org/svn/collective/buildout/plonetest/plone-4.1.x.cfg
package-name = plone.app.debugtoolbar
View
6 docs/TODO.txt
@@ -22,11 +22,9 @@ Request
Response
--------
-XXX: Renders too early - status and cache headers are not included
-
- [ ] Status
+ [x] Status
[x] Headers
- [ ] plone.app.caching rules
+ [x] plone.app.caching rules
Global
------
View
2 setup.py
@@ -36,6 +36,8 @@
install_requires=[
'setuptools',
'Products.CMFPlone',
+ 'zope.annotation',
+ 'plone.transformchain',
],
extras_require={'test': ['plone.app.testing']},
entry_points="""
View
1 src/plone/app/debugtoolbar/browser/resources/debugtoolbar.css
@@ -38,6 +38,7 @@
#debug-toolbar .debug-toolbar-body {
display: none;
+ margin: 0 1em;
}
/* Typography, layout, patterns from Bootstrap.css */
View
3 src/plone/app/debugtoolbar/browser/resources/debugtoolbar.js
@@ -1,6 +1,9 @@
jQuery(function($) {
$(function() {
+ $("#debug-toolbar").prependTo("html body");
+ $("#debug-toolbar-trigger").prependTo("html body");
+
$("#debug-toolbar-trigger").click(function() {
$('#debug-toolbar').slideDown();
return false;
View
45 src/plone/app/debugtoolbar/browser/response.pt
@@ -1,29 +1,40 @@
-<tal:block define="response request/response">
<h2 class="debug-toolbar-header" id="debug-toolbar-response" i18n:translate="debug_toolbar_response_title">
Response <small>returned to the browser</small>
- <span i18n:name="method_get" tal:condition="python:response.status == 200" class="label success" tal:content="response/status"/>
- <span i18n:name="method_post" tal:condition="python:response.status != 200" class="label important" tal:content="response/status"/>
+ <span id="debug-toolbar-response-status" i18n:name="method" class="label"></span>
</h2>
<div class="debug-toolbar-body" id="debug-toolbar-response-body">
<p class="debug-toolbar-help">
- This panel shows information about response headers and content types.
+ This panel shows information about the HTTP response
</p>
- <tal:block condition="nocall:view/headers">
- <h3 i18n:translate="">Response headers</h3>
+ <h3 i18n:translate="">Response headers</h3>
- <table class="zebra-striped">
- <tbody>
- <tr tal:repeat="v view/headers">
- <td tal:content="python:v[0]" />
- <td><code tal:content="python:v[1]" /></td>
- </tr>
- </tbody>
- </table>
- </tal:block>
-
+ <table id="debug-toolbar-response-headers" class="zebra-striped">
+ <tbody>
+ </tbody>
+ </table>
</div>
-</tal:block>
+<script type="text/javascript">
+jQuery(function($) {
+ $().ready(function() {
+
+ var status = PLONE_APP_DEBUGTOOLBAR_DATA['response_status'];
+ var headers = PLONE_APP_DEBUGTOOLBAR_DATA['response_headers'];
+
+ if(status == 200) {
+ $("#debug-toolbar-response-status").addClass('success').html(status);
+ } else {
+ $("#debug-toolbar-response-status").addClass('important').html(status);
+ }
+
+ for(var i = 0; i < headers.length; ++i) {
+ $("#debug-toolbar-response-headers tbody").append(
+ "<" + "tr><" + "td>" + headers[i][0] + "<" + "/td><" + "td><" + "code>" + headers[i][1] + "<" + "/code><" + "/td><" + "/tr>"
+ );
+ }
+ });
+});
+</script>
View
20 src/plone/app/debugtoolbar/browser/response.py
@@ -1,10 +1,20 @@
from zope.viewlet.viewlet import ViewletBase
+from plone.app.debugtoolbar.delayedwrite import delay
+
+def captureHeaders(request):
+ headers = []
+
+ for k in sorted(request.response.headers):
+ header = '-'.join([x.capitalize() for x in k.split('-')])
+ headers.append((header, request.response.headers[k],))
+
+ return headers
+
+def captureStatus(request):
+ return request.response.status
class ResponseViewlet(ViewletBase):
def update(self):
- self.headers = []
-
- for k in sorted(self.request.response.headers):
- header = '-'.join([x.capitalize() for x in k.split('-')])
- self.headers.append((header, self.request.response.headers[k],))
+ delay(self.request, 'response_headers', captureHeaders)
+ delay(self.request, 'response_status', captureStatus)
View
1 src/plone/app/debugtoolbar/browser/toolbar.pt
@@ -1,3 +1,4 @@
+<script type="text/javascript">var PLONE_APP_DEBUGTOOLBAR_DATA = {};</script>
<a id="debug-toolbar-trigger" class="debug-toolbar-trigger-button" href="#debug-toolbar" i18n:translate="debug_toolbar_trigger">&darr; Debug</a>
<div id="debug-toolbar">
View
7 src/plone/app/debugtoolbar/configure.zcml
@@ -5,6 +5,8 @@
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
i18n_domain="plone">
+ <include package="zope.annotation" />
+ <include package="plone.transformchain" />
<include package=".browser" />
<genericsetup:registerProfile
@@ -16,4 +18,9 @@
/>
<!-- -*- extra stuff goes here -*- -->
+ <adapter
+ factory=".delayedwrite.DelayedWriteTransformer"
+ name="plone.app.debugtoolbar.delayedwrite"
+ />
+
</configure>
View
57 src/plone/app/debugtoolbar/delayedwrite.py
@@ -0,0 +1,57 @@
+import json
+
+from zope.interface import Interface
+from zope.interface import implements
+from zope.component import adapts
+from zope.annotation.interfaces import IAnnotations
+
+from plone.transformchain.interfaces import ITransform
+
+from plone.app.debugtoolbar.browser.interfaces import IDebugToolbarLayer
+
+def delay(request, name, fn):
+ """Register a function that will be called at the end of the request.
+ """
+
+ ann = IAnnotations(request, None)
+ if ann is None:
+ return
+ ann.setdefault('plone.app.debugtoolbar.delayed', {})[name] = fn
+
+class DelayedWriteTransformer(object):
+ implements(ITransform)
+ adapts(Interface, IDebugToolbarLayer)
+
+ order = 9999
+
+ varname = "PLONE_APP_DEBUGTOOLBAR_DATA"
+
+ def __init__(self, published, request):
+ self.published = published
+ self.request = request
+
+ def transformUnicode(self, result, encoding):
+ return result.replace(u'var %s = {};' % self.varname,
+ u'var %s = %s;' % (self.varname, self.getData()))
+
+ def transformBytes(self, result, encoding):
+ return result.replace('var %s = {};' % self.varname,
+ 'var %s = %s;' % (self.varname, self.getData()))
+
+ def transformIterable(self, result, encoding):
+ res = []
+ for r in result:
+ res.append(self.transformBytes(r, encoding))
+ return res
+
+ def getData(self):
+ data = {}
+ ann = IAnnotations(self.request, None)
+ if ann is None:
+ return data
+
+ delayed = ann.get('plone.app.debugtoolbar.delayed', {})
+ for key, fn in delayed.items():
+ data[key] = fn(self.request)
+
+ return json.dumps(data)

0 comments on commit d9cedfa

Please sign in to comment.
Something went wrong with that request. Please try again.