Refactor: store current user on `before_request()`

This commit is contained in:
Santiago L 2022-01-04 11:59:03 +01:00
parent 5b9a9d482d
commit e1786ddcb2
1 changed files with 7 additions and 9 deletions

View File

@ -7,10 +7,10 @@ import click
import click_spinner import click_spinner
import ereuse_utils.cli import ereuse_utils.cli
from ereuse_utils.session import DevicehubClient from ereuse_utils.session import DevicehubClient
from flask.globals import _app_ctx_stack, g from flask import _app_ctx_stack, g
from flask_login import current_user from flask_login import LoginManager, current_user
from flask import g as gg
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from teal.db import SchemaSQLAlchemy from teal.db import SchemaSQLAlchemy
from teal.teal import Teal 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.dummy.dummy import Dummy
from ereuse_devicehub.resources.device.search import DeviceSearch from ereuse_devicehub.resources.device.search import DeviceSearch
from ereuse_devicehub.resources.inventory import Inventory, InventoryDef 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.resources.user.models import User
from ereuse_devicehub.templating import Environment
class Devicehub(Teal): class Devicehub(Teal):
@ -80,7 +76,6 @@ class Devicehub(Teal):
@login_manager.user_loader @login_manager.user_loader
def load_user(user_id): def load_user(user_id):
gg.user = current_user
return User.query.get(user_id) return User.query.get(user_id)
# noinspection PyMethodOverriding # noinspection PyMethodOverriding
@ -172,6 +167,9 @@ class Devicehub(Teal):
inv = g.inventory = Inventory.current # type: Inventory inv = g.inventory = Inventory.current # type: Inventory
g.tag_provider = DevicehubClient(base_url=inv.tag_provider, g.tag_provider = DevicehubClient(base_url=inv.tag_provider,
token=DevicehubClient.encode_token(inv.tag_token)) 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'): def create_client(self, email='user@dhub.com', password='1234'):
client = UserClient(self, email, password, response_wrapper=self.response_class) client = UserClient(self, email, password, response_wrapper=self.response_class)