From dc24af1db094cb973ddb17d6b3661e3cf8c24a40 Mon Sep 17 00:00:00 2001 From: imacat Date: Tue, 7 Feb 2023 13:40:01 +0800 Subject: [PATCH] Added the get_current_user function to the "accounting.utils.user" module to retrieve the currently logged-in user and cache it in the current request. --- src/accounting/utils/user.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/accounting/utils/user.py b/src/accounting/utils/user.py index 1d2a243..f7c20ad 100644 --- a/src/accounting/utils/user.py +++ b/src/accounting/utils/user.py @@ -23,6 +23,7 @@ import typing as t from abc import ABC, abstractmethod import sqlalchemy as sa +from flask import g from flask_sqlalchemy.model import Model T = t.TypeVar("T", bound=Model) @@ -95,7 +96,7 @@ def get_current_user_pk() -> int: :return: The primary key value of the currently logged-in user. """ - return __user_utils.get_pk(__user_utils.current_user) + return __user_utils.get_pk(get_current_user()) def has_user(username: str) -> bool: @@ -114,3 +115,14 @@ def get_user_pk(username: str) -> int: :return: The primary key value of the user by the username. """ return __user_utils.get_pk(__user_utils.get_by_username(username)) + + +def get_current_user() -> user_cls: + """Returns the currently logged-in user. The result is cached in the + current request. + + :return: The currently logged-in user. + """ + if not hasattr(g, "_accounting_user"): + setattr(g, "_accounting_user", __user_utils.current_user) + return getattr(g, "_accounting_user")