From ba08aff3464d67d03c6a5b67296282247b0bce1a Mon Sep 17 00:00:00 2001 From: Scoder12 <34356756+Scoder12@users.noreply.github.com> Date: Mon, 3 Aug 2020 15:34:21 -0700 Subject: [PATCH] Add flask proxies and local_redirect function --- src/replit/maqpy/__init__.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/replit/maqpy/__init__.py b/src/replit/maqpy/__init__.py index 39ea777..67f1485 100644 --- a/src/replit/maqpy/__init__.py +++ b/src/replit/maqpy/__init__.py @@ -14,7 +14,22 @@ from ..database import db auth = LocalProxy(lambda: flask.request.auth) signed_in = LocalProxy(lambda: flask.request.signed_in) +request = LocalProxy(lambda: flask.request) +render_template = flask.render_template +redirect = flask.redirect -# TODO: signinwall(exclude=['/a', '/b']) -# TODO: @need_signin -# TODO: Param checking with @needs_params + +def local_redirect(location: str, code: int = 302) -> flask.Response: + """Perform a redirection to a local path without downgrading to HTTP. + + Args: + location (str): The path to redirect to. + code (int): The code to use for the redirect. Defaults to 302. + + Returns: + flask.Response: The redirect response. + """ + # Use a LocalProxy so that it can be called before the request context is available + return LocalProxy( + lambda: redirect("https://" + request.headers["host"] + location, code) + )