Search code examples
pythonhttp-redirectpyramid

Validating pyramid requests


I have a pyramid application with many defined routes. I want to do a few different server-side checks whenever a user makes a request and possibly redirect the request if certain criteria are met.

I realize I could add these checks to each of the view functions, but I'd like to avoid having to update and add boilerplate to dozens of view functions.

Is there a way in pyramid to perform some checks for all requests, before they're sent onto the view functions?


Solution

  • I believe you are looking for a NewRequest event:

    from pyramid.events import NewRequest
    from pyramid.events import subscriber
    
    @subscriber(NewRequest)
    def new_request_subscriber(event):
        validate_request(event.request)
    

    More information in the offical docs:
    http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/events.html