making before_render events conditional ?

19 views
Skip to first unread message

Jonathan Vanasco

unread,
May 17, 2013, 2:47:09 PM5/17/13
to pylons-...@googlegroups.com
I've got a subscriber to before_render that does some "tweaking" of objects and priming of an object cache for Mako/Jinja/etc templates.

The problem that I've run into – json calls to the api trigger this.

The following code works, just wondering if anyone has a better idea , that would let me escape not out of '/api' calls, but anything matching a json/jsonp or other render type.


def BeforeRender_prime(event):
    request = event.get("request") or threadlocal.get_current_request()
    if not request :
        log.debug( "BeforeRender_prime | NO_REQUEST" )
        return
    # only run once ; a fix for the pyramid_debugtoolbar multiple request
    if hasattr( request , 'BeforeRender_prime' ):
        return
    request.BeforeRender_prime = True
    # don't run on /api , which is all json
    if request.path_info[:5] == '/api/':
        return
    ... 
    ... expensive stuff ...
    ...

Tres Seaver

unread,
May 17, 2013, 3:04:18 PM5/17/13
to pylons-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 05/17/2013 02:47 PM, Jonathan Vanasco wrote:
> I've got a subscriber to before_render that does some "tweaking" of
> objects and priming of an object cache for Mako/Jinja/etc templates.
>
> The problem that I've run into � json calls to the api trigger this.
>
> The following code works, just wondering if anyone has a better idea ,
> that would let me escape not out of '/api' calls, but anything
> matching a json/jsonp or other render type.
>
>
> def BeforeRender_prime(event): request = event.get("request") or
> threadlocal.get_current_request() if not request : log.debug(
> "BeforeRender_prime | NO_REQUEST" ) return # only run once ; a fix for
> the pyramid_debugtoolbar multiple request if hasattr( request ,
> 'BeforeRender_prime' ): return request.BeforeRender_prime = True #
> don't run on /api , which is all json if request.path_info[:5] ==
> '/api/': return

You should be able to filter on the 'renderer_name' for the event, e.g.:

if event.get('renderer_name') == 'json':
return


Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 tse...@palladion.com
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iEYEARECAAYFAlGWfzIACgkQ+gerLs4ltQ5mjQCgkF0xx9l0VdQced9EtS8KPcLR
ecAAn1LEU4jNsN4Bj+P+SZ68Uv98VVPJ
=tDsQ
-----END PGP SIGNATURE-----

Jonathan Vanasco

unread,
May 17, 2013, 3:22:37 PM5/17/13
to pylons-...@googlegroups.com, tse...@palladion.com


On Friday, May 17, 2013 3:04:18 PM UTC-4, Tres Seaver wrote:
  if event.get('renderer_name') == 'json':
      return

That works perfectly, thanks!

Reply all
Reply to author
Forward
0 new messages