Question

Is there a global way to redirect users w/ certain ip's in pyramid? Right now, we have to redirect on a view by view basis.

For instance, each view will return 'my custom message' for every page someone w/ ip address of 'xyx.xxx.xx' visits. If you don't have that ip address, then the page will load as normal.

@view_config(renderer="json", route_name="myview")
def myview(request):
    redirector(request)
    ......
def redirector(request):
    if request.remote_addr.startswith('66.'): return viewA(request)
    else: return 'my custom message'

Is there a way we could redirect globally in pyramid (other than through .htaccess)?

Thanks!

Was it helpful?

Solution

You can use Pyramid's events system :

@subscriber('pyramid.events.NewRequest')
def newrequest(event):
    request = event.request
    if request.remote_addr.startswith('66.'):
        raise SomeException('my custom message')

@view_config(context=SomeException, renderer='string')
def exc_view(exc, request):
    return exc.message
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top