I have some controller functions (in different controllers) decorated with @auth.requires_membership(role='manager')
. If this is an http request, I would like to make sure these (and only these) get redirected to https versions.
I've tried abstracting a function like this into a module, which I call at the start of the function in any controller which needs redirecting but web2py complains that "global name 'redirect' is not defined":
from gluon import current
def https_redirect() :
request = current.request
if not request.is_local and not request.is_https:
redirect(URL(scheme='https', args=request.args, vars=request.vars))
There must be a better way of doing this. What are the recommended options?
There is already the built-in method request.requires_https()
-- when called, if the current request is not over HTTPS, it redirects to the current URL with the https
scheme.
Regarding redirect
, that is a global defined in the web2py execution environment, so only available in models, controllers, and views. You can access it in a module via the current
object using current.globalenv['redirect']
.
You can also redirect by using the HTTP
exception directly (which is what redirect
does behind the scenes):
from gluon.http import HTTP
from gluon.html import URL
raise HTTP(303, location=URL(...))