From e1786ddcb284a4ab3d0b4bc69279d26ea927b365 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Tue, 4 Jan 2022 11:59:03 +0100 Subject: [PATCH] Refactor: store current user on `before_request()` --- ereuse_devicehub/devicehub.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index f97e7b74..f286abc0 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -7,10 +7,10 @@ import click import click_spinner import ereuse_utils.cli from ereuse_utils.session import DevicehubClient -from flask.globals import _app_ctx_stack, g -from flask_login import current_user -from flask import g as gg +from flask import _app_ctx_stack, g +from flask_login import LoginManager, current_user from flask_sqlalchemy import SQLAlchemy +from flask_wtf.csrf import CSRFProtect from teal.db import SchemaSQLAlchemy from teal.teal import Teal @@ -21,12 +21,8 @@ from ereuse_devicehub.db import db from ereuse_devicehub.dummy.dummy import Dummy from ereuse_devicehub.resources.device.search import DeviceSearch from ereuse_devicehub.resources.inventory import Inventory, InventoryDef -from ereuse_devicehub.templating import Environment - - -from flask_login import LoginManager -from flask_wtf.csrf import CSRFProtect from ereuse_devicehub.resources.user.models import User +from ereuse_devicehub.templating import Environment class Devicehub(Teal): @@ -80,7 +76,6 @@ class Devicehub(Teal): @login_manager.user_loader def load_user(user_id): - gg.user = current_user return User.query.get(user_id) # noinspection PyMethodOverriding @@ -172,6 +167,9 @@ class Devicehub(Teal): inv = g.inventory = Inventory.current # type: Inventory g.tag_provider = DevicehubClient(base_url=inv.tag_provider, token=DevicehubClient.encode_token(inv.tag_token)) + # NOTE: models init methods expects that current user is + # available on g.user (e.g. to initialize object owner) + g.user = current_user def create_client(self, email='user@dhub.com', password='1234'): client = UserClient(self, email, password, response_wrapper=self.response_class)