Pylons / pyramid_layout

Pyramid add-on for managing UI layouts.
Other
24 stars 26 forks source link

TypeError when serving static files #2

Closed TWry closed 11 years ago

TWry commented 11 years ago

I am getting a type error when serving static files in a project that is using pyramid_layout. This seems to be the case for certain file types only. .js files seem to work fine whereas others cause the traceback below.

I worked around it by testing if the path of the request starts with /static/ but I don't think that this is a good solution:

def add_renderer_globals(event): request = event['request']

if the rendering is done from a script or otherwise outside a

# regular request, the request will be None, so globals can't be set
if request is None or request.path.startswith('/static/'):
    return

012-11-09 12:12:13,118 ERROR [waitress][Dummy-3] Exception when serving /static/src/dijit/templates/test.htm Traceback (most recent call last): File "C:\Data\prodash\env\lib\site-packages\waitress-0.8.1-py2.7.egg\waitress\channel.py", line 329, in service task.service() File "C:\Data\prodash\env\lib\site-packages\waitress-0.8.1-py2.7.egg\waitress\task.py", line 173, in service self.execute() File "C:\Data\prodash\env\lib\site-packages\waitress-0.8.1-py2.7.egg\waitress\task.py", line 380, in execute app_iter = self.channel.server.application(env, start_response) File "C:\Data\prodash\env\lib\site-packages\pyramid-1.3.3-py2.7.egg\pyramid\router.py", line 187, in call response = self.handle_request(request) File "C:\Data\prodash\env\lib\site-packages\pyramid_debugtoolbar-1.0.2-py2.7.egg\pyramid_debugtoolbar\toolbar.py", line 165, in toolbar_tween toolbar.process_response(response) File "C:\Data\prodash\env\lib\site-packages\pyramid_debugtoolbar-1.0.2-py2.7.egg\pyramid_debugtoolbar\toolbar.py", line 56, in process_response vars, request=request) File "C:\Data\prodash\env\lib\site-packages\pyramid-1.3.3-py2.7.egg\pyramid\renderers.py", line 81, in render return helper.render(value, None, request=request) File "C:\Data\prodash\env\lib\site-packages\pyramid-1.3.3-py2.7.egg\pyramid\renderers.py", line 420, in render registry.notify(systemvalues) File "C:\Data\prodash\env\lib\site-packages\pyramid-1.3.3-py2.7.egg\pyramid\registry.py", line 74, in notify [ for _ in self.subscribers(events, None) ] File "C:\Data\prodash\env\lib\site-packages\zope.interface-4.0.1-py2.7-win32.egg\zope\interface\registry.py", line 323, in subscribers return self.adapters.subscribers(objects, provided) File "C:\Data\prodash\env\lib\site-packages\zope.interface-4.0.1-py2.7-win32.egg\zope\interface\adapter.py", line 601, in subscribers subscription(*objects) File "C:\Data\prodash\env\lib\site-packages\pyramid_layout-0.5-py2.7.egg\pyramid_layout\config.py", line 35, in add_renderer_globals layout = layout_manager.layout File "C:\Data\prodash\env\lib\site-packages\pyramid-1.3.3-py2.7.egg\pyramid\decorator.py", line 17, in get val = self.wrapped(inst) File "C:\Data\prodash\env\lib\site-packages\pyramid_layout-0.5-py2.7.egg\pyramid_layout\layout.py", line 41, in layout return find_layout(self.context, self.request) File "C:\Data\prodash\env\lib\site-packages\pyramid_layout-0.5-py2.7.egg\pyramid_layout\layout.py", line 64, in find_layout return layout(context, request) TypeError: 'NoneType' object is not callable

mcdonc commented 11 years ago
diff --git a/pyramid_layout/layout.py b/pyramid_layout/layout.py
index c655e65..2a38084 100644
--- a/pyramid_layout/layout.py
+++ b/pyramid_layout/layout.py
@@ -61,7 +61,8 @@ class LayoutManager(object):
 def find_layout(context, request, name=''):
     adapters = request.registry.adapters
     layout = adapters.lookup((providedBy(context),), ILayout, name=name)
-    return layout(context, request)
+    if layout is not None:
+        return layout(context, request)

This is a possible fix. But not sure if it's correct, I'm doing this on behalf of someone in IRC quickly.

TWry commented 11 years ago

It seems also config.py needs to check for "layout is not None" in add_renderer_globals.

chrisrossi commented 11 years ago

TWry,

Can you check to see if the fix I checked in on the 'handle-broken-context' branch addresses this issue?

Thanks, Chris

chrisrossi commented 11 years ago

Closing for lack of feedback.

umeboshi2 commented 11 years ago

I'm sorry that I haven't had time to be responsive.
This is how I have handled this problem: https://github.com/umeboshi2/pyramid_layout/commit/7cdad2d63556845f4a0c031e77d5212660952800

I get these "NoneType" errors whenever I include pyramid_layout but don't assign a layout to a view. I have been working around this by assigning the base layout to views that render in json or string, even though the layout is unnecessary in those views. However, that simple workaround didn't work when starting to use other pyramid projects (i.e. velruse) in my source tree. I am not sure whether my solution is proper, but it seems to work.

tonthon commented 11 years ago

I'm facing this issue too, @chrisrossi, the fix provided for the 'handle-broken-context' doesn't concern this problem at all. The problem is that pyramid_debugtoolbar does some view rendering during the beforeRender event, before the launch of the add_renderer_globals function.

Could you please re-open this issue ?