Revised the init_app function of the "accounting.utils.permission" module to register the "can_view" and "can_edit" functions under the blueprint instead of the whole application.

This commit is contained in:
依瑪貓 2023-02-07 17:05:27 +08:00
parent c3cedf714b
commit 699db20308
2 changed files with 7 additions and 6 deletions

View File

@ -55,7 +55,7 @@ def init_app(app: Flask, user_utils: AbstractUserUtils,
locale.init_app(app, bp)
from .utils import permission
permission.init_app(app, can_view_func, can_edit_func)
permission.init_app(bp, can_view_func, can_edit_func)
from . import base_account
base_account.init_app(app, bp)

View File

@ -21,7 +21,7 @@ This module should not import any other module from the application.
"""
import typing as t
from flask import Flask, abort
from flask import abort, Blueprint
from accounting.utils.user import get_current_user
@ -87,11 +87,12 @@ def can_edit() -> bool:
return __can_edit_func()
def init_app(app: Flask, can_view_func: t.Callable[[], bool] | None = None,
def init_app(bp: Blueprint,
can_view_func: t.Callable[[], bool] | None = None,
can_edit_func: t.Callable[[], bool] | None = None) -> None:
"""Initializes the application.
:param app: The Flask application.
:param bp: The blueprint of the accounting application.
:param can_view_func: A callback that returns whether the current user can
view the accounting data.
:param can_edit_func: A callback that returns whether the current user can
@ -103,5 +104,5 @@ def init_app(app: Flask, can_view_func: t.Callable[[], bool] | None = None,
__can_view_func = can_view_func
if can_edit_func is not None:
__can_edit_func = can_edit_func
app.jinja_env.globals["can_view_accounting"] = can_view
app.jinja_env.globals["can_edit_accounting"] = can_edit
bp.add_app_template_global(can_view, "can_view_accounting")
bp.add_app_template_global(can_edit, "can_edit_accounting")