From ac43d0e3e36abad0c8629e53d67f8957b93bf8fa Mon Sep 17 00:00:00 2001 From: Scoder12 <34356756+Scoder12@users.noreply.github.com> Date: Mon, 3 Aug 2020 15:38:08 -0700 Subject: [PATCH] Move local_redirect to utils --- src/replit/maqpy/__init__.py | 16 ---------------- src/replit/maqpy/utils.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/replit/maqpy/__init__.py b/src/replit/maqpy/__init__.py index 67f1485..d24af6f 100644 --- a/src/replit/maqpy/__init__.py +++ b/src/replit/maqpy/__init__.py @@ -17,19 +17,3 @@ signed_in = LocalProxy(lambda: flask.request.signed_in) request = LocalProxy(lambda: flask.request) render_template = flask.render_template redirect = flask.redirect - - -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) - ) diff --git a/src/replit/maqpy/utils.py b/src/replit/maqpy/utils.py index 38e1e1b..7c89322 100644 --- a/src/replit/maqpy/utils.py +++ b/src/replit/maqpy/utils.py @@ -3,6 +3,7 @@ from functools import wraps from typing import Any, Callable, Union import flask +from werkzeug.local import LocalProxy from .html import Page @@ -112,3 +113,21 @@ def needs_params( return handler return decorator + + +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: flask.redirect( + "https://" + flask.request.headers["host"] + location, code + ) + )