From a9026f77e86544dd6ab9a9d90491193d5e704bb0 Mon Sep 17 00:00:00 2001 From: Scoder12 <34356756+Scoder12@users.noreply.github.com> Date: Tue, 28 Jul 2020 12:21:27 -0700 Subject: [PATCH] Switch to shorter login_wall all_pages_sign_in was too long --- examples/maqpy/all_pages_sign_in.py | 15 --------------- examples/maqpy/login_wall.py | 23 +++++++++++++++++++++++ src/replit/maqpy/app.py | 6 ++++-- 3 files changed, 27 insertions(+), 17 deletions(-) delete mode 100644 examples/maqpy/all_pages_sign_in.py create mode 100644 examples/maqpy/login_wall.py diff --git a/examples/maqpy/all_pages_sign_in.py b/examples/maqpy/all_pages_sign_in.py deleted file mode 100644 index 84bebc9..0000000 --- a/examples/maqpy/all_pages_sign_in.py +++ /dev/null @@ -1,15 +0,0 @@ -"""An example of the all_pages_sign_in() feature.""" -from replit import maqpy - -app = maqpy.App(__name__) -app.all_pages_sign_in(exclude=["/"]) - - -@app.route("/") -def index() -> str: - return "Some introduction " + maqpy.Link("check out my page", href="/page") - - -@app.route("/page") -def page() -> str: - return "Hello, if you are reading this you signed in." diff --git a/examples/maqpy/login_wall.py b/examples/maqpy/login_wall.py new file mode 100644 index 0000000..4980c06 --- /dev/null +++ b/examples/maqpy/login_wall.py @@ -0,0 +1,23 @@ +"""An example of the app.login_wall() feature.""" +from replit import maqpy + +app = maqpy.App(__name__) + + +def login_page() -> str: + return "Hello, please sign in to access this page!\n" + maqpy.signin() + + +# this is an example of the arguments, but the exclude kwarg already defaults to this +# and there is a default handler as well. +app.login_wall(exclude=["/"], handler=login_page) + + +@app.route("/") +def index() -> str: + return "Some introduction " + maqpy.Link("check out my page", href="/page") + + +@app.route("/page") +def page() -> str: + return f"Hello {maqpy.auth.name}, if you are reading this you signed in." diff --git a/src/replit/maqpy/app.py b/src/replit/maqpy/app.py index a833682..4f669b9 100644 --- a/src/replit/maqpy/app.py +++ b/src/replit/maqpy/app.py @@ -72,12 +72,14 @@ class App(flask.Flask): request_class = Request - def all_pages_sign_in(self, exclude: Set[str] = ("/",)) -> None: - """Require sign-in on all pages. + def login_wall(self, exclude: Set[str] = ("/",), handler: Callable = None) -> None: + """Require users to be logged-in on all pages. Args: exclude (Tuple[str]): The routes that should not require sign in. Defaults to just /. + handler (Callable): The handler to call when the user is not signed in. If + not provided, defaults to maqpy.signin() """ self._apsi_exclude = set(exclude) or set()