From f7c8ae10375cfdc9b0fb78bcc9ef9a141b38516f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 3 May 2023 06:30:42 +0200 Subject: [PATCH] blacked and add some typings --- flaschengeist/cli/InterfaceGenerator.py | 4 +--- flaschengeist/cli/export_cmd.py | 2 +- flaschengeist/cli/plugin_cmd.py | 1 - flaschengeist/cli/run_cmd.py | 1 - flaschengeist/controller/pluginController.py | 8 ++++++-- flaschengeist/controller/userController.py | 7 +++++-- flaschengeist/database/__init__.py | 5 +++-- flaschengeist/database/types.py | 19 +++++++++++-------- flaschengeist/models/user.py | 6 +++--- flaschengeist/plugins/__init__.py | 7 ++++--- flaschengeist/plugins/balance/__init__.py | 3 ++- .../plugins/balance/balance_controller.py | 1 - flaschengeist/plugins/scheduler.py | 1 + flaschengeist/plugins/users/__init__.py | 6 ++++-- 14 files changed, 41 insertions(+), 30 deletions(-) diff --git a/flaschengeist/cli/InterfaceGenerator.py b/flaschengeist/cli/InterfaceGenerator.py index 3bcb545..e03dde2 100644 --- a/flaschengeist/cli/InterfaceGenerator.py +++ b/flaschengeist/cli/InterfaceGenerator.py @@ -37,7 +37,6 @@ class InterfaceGenerator: if origin is typing.ForwardRef: # isinstance(cls, typing.ForwardRef): return "", "this" if cls.__forward_arg__ == self.this_type else cls.__forward_arg__ if origin is typing.Union: - if len(arguments) == 2 and arguments[1] is type(None): return "?", self.pytype(arguments[0])[1] else: @@ -81,7 +80,6 @@ class InterfaceGenerator: d = {} for param, ptype in typing.get_type_hints(module[1], globalns=None, localns=None).items(): if not param.startswith("_") and not param.endswith("_"): - d[param] = self.pytype(ptype) if len(d) == 1: @@ -115,7 +113,7 @@ class InterfaceGenerator: return buffer def write(self): - with (open(self.filename, "w") if self.filename else sys.stdout) as file: + with open(self.filename, "w") if self.filename else sys.stdout as file: if self.namespace: file.write(f"declare namespace {self.namespace} {{\n") for line in self._write_types().getvalue().split("\n"): diff --git a/flaschengeist/cli/export_cmd.py b/flaschengeist/cli/export_cmd.py index 4e0fa03..0a611a3 100644 --- a/flaschengeist/cli/export_cmd.py +++ b/flaschengeist/cli/export_cmd.py @@ -9,7 +9,7 @@ from importlib.metadata import entry_points @click.option("--no-core", help="Skip models / types from flaschengeist core", is_flag=True) def export(namespace, output, no_core, plugin): from flaschengeist import logger, models - from .InterfaceGenerator import InterfaceGenerator + from flaschengeist.cli.InterfaceGenerator import InterfaceGenerator gen = InterfaceGenerator(namespace, output, logger) if not no_core: diff --git a/flaschengeist/cli/plugin_cmd.py b/flaschengeist/cli/plugin_cmd.py index 5356eac..b2a9260 100644 --- a/flaschengeist/cli/plugin_cmd.py +++ b/flaschengeist/cli/plugin_cmd.py @@ -53,7 +53,6 @@ def disable(ctx, plugin): def install(ctx: click.Context, plugin, all): """Install one or more plugins""" all_plugins = entry_points(group="flaschengeist.plugins") - if all: plugins = [ep.name for ep in all_plugins] elif len(plugin) > 0: diff --git a/flaschengeist/cli/run_cmd.py b/flaschengeist/cli/run_cmd.py index ddca7c8..60c93af 100644 --- a/flaschengeist/cli/run_cmd.py +++ b/flaschengeist/cli/run_cmd.py @@ -10,7 +10,6 @@ class PrefixMiddleware(object): self.prefix = prefix def __call__(self, environ, start_response): - if environ["PATH_INFO"].startswith(self.prefix): environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix) :] environ["SCRIPT_NAME"] = self.prefix diff --git a/flaschengeist/controller/pluginController.py b/flaschengeist/controller/pluginController.py index 486ffea..6325f98 100644 --- a/flaschengeist/controller/pluginController.py +++ b/flaschengeist/controller/pluginController.py @@ -23,7 +23,11 @@ __required_plugins = ["users", "roles", "scheduler", "auth"] def get_authentication_provider(): - return [current_app.config["FG_PLUGINS"][plugin.name] for plugin in get_loaded_plugins().values() if isinstance(plugin, AuthPlugin)] + return [ + current_app.config["FG_PLUGINS"][plugin.name] + for plugin in get_loaded_plugins().values() + if isinstance(plugin, AuthPlugin) + ] def get_loaded_plugins(plugin_name: str = None): @@ -108,7 +112,7 @@ def install_plugin(plugin_name: str): directory /= loc if directory.exists(): database_upgrade(revision=f"{plugin_name}@head") - + db.session.commit() return plugin diff --git a/flaschengeist/controller/userController.py b/flaschengeist/controller/userController.py index 87f67c3..b5e427d 100644 --- a/flaschengeist/controller/userController.py +++ b/flaschengeist/controller/userController.py @@ -2,6 +2,7 @@ import re import secrets from io import BytesIO +from typing import Optional from sqlalchemy import exc from sqlalchemy_utils import merge_references from datetime import datetime, timedelta, timezone @@ -41,15 +42,17 @@ def _generate_password_reset(user): return reset -def get_provider(userid: str): +def get_provider(userid: str) -> AuthPlugin: return [p for p in pluginController.get_authentication_provider() if p.user_exists(userid)][0] @Hook -def update_user(user: User, backend: AuthPlugin): +def update_user(user: User, backend: Optional[AuthPlugin] = None): """Update user data from backend This is seperate function to provide a hook""" + if not backend: + backend = get_provider(user.userid) backend.update_user(user) if not user.display_name: user.display_name = "{} {}.".format(user.firstname, user.lastname[0]) diff --git a/flaschengeist/database/__init__.py b/flaschengeist/database/__init__.py index 66428d0..914db1d 100644 --- a/flaschengeist/database/__init__.py +++ b/flaschengeist/database/__init__.py @@ -6,7 +6,8 @@ from sqlalchemy import MetaData from flaschengeist.alembic import alembic_script_path from flaschengeist import logger -from flaschengeist.controller import pluginController + +# from flaschengeist.controller import pluginController # https://alembic.sqlalchemy.org/en/latest/naming.html metadata = MetaData( @@ -20,7 +21,7 @@ metadata = MetaData( ) -db = SQLAlchemy(metadata=metadata) +db = SQLAlchemy(metadata=metadata, session_options={"expire_on_commit": False}) migrate = Migrate() diff --git a/flaschengeist/database/types.py b/flaschengeist/database/types.py index 645ecdd..0b34a60 100644 --- a/flaschengeist/database/types.py +++ b/flaschengeist/database/types.py @@ -16,13 +16,16 @@ class ModelSerializeMixin: module = import_module("flaschengeist.models").__dict__ - hint = typing.get_type_hints(self.__class__, globalns=module)[param] - if ( - typing.get_origin(hint) is typing.Union - and len(typing.get_args(hint)) == 2 - and typing.get_args(hint)[1] is type(None) - ): - return getattr(self, param) is None + try: + hint = typing.get_type_hints(self.__class__, globalns=module, locals=locals())[param] + if ( + typing.get_origin(hint) is typing.Union + and len(typing.get_args(hint)) == 2 + and typing.get_args(hint)[1] is type(None) + ): + return getattr(self, param) is None + except: + pass def serialize(self): """Serialize class to dict @@ -35,7 +38,7 @@ class ModelSerializeMixin: if not param.startswith("_") and not param.endswith("_") and not self.__is_optional(param) } if len(d) == 1: - key, value = d.popitem() + _, value = d.popitem() return value return d diff --git a/flaschengeist/models/user.py b/flaschengeist/models/user.py index a51cedc..74f7950 100644 --- a/flaschengeist/models/user.py +++ b/flaschengeist/models/user.py @@ -27,7 +27,7 @@ class Permission(db.Model, ModelSerializeMixin): id_ = db.Column("id", Serial, primary_key=True) plugin_id_: int = db.Column("plugin", Serial, db.ForeignKey("plugin.id")) - plugin_ = db.relationship("Plugin", lazy="select", back_populates="permissions", enable_typechecks=False) + plugin_ = db.relationship("Plugin", lazy="subquery", back_populates="permissions", enable_typechecks=False) class Role(db.Model, ModelSerializeMixin): @@ -62,8 +62,8 @@ class User(db.Model, ModelSerializeMixin): deleted: bool = db.Column(db.Boolean(), default=False) birthday: Optional[date] = db.Column(db.Date) mail: str = db.Column(db.String(60)) - permissions: Optional[list[str]] = None roles: List[str] = [] + permissions: Optional[list[str]] = [] # Protected stuff for backend use only id_ = db.Column("id", Serial, primary_key=True) @@ -81,7 +81,7 @@ class User(db.Model, ModelSerializeMixin): ) @property - def roles(self): + def roles(self) -> List[str]: return [role.name for role in self.roles_] def set_attribute(self, name, value): diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index c9c5cd1..2907166 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -169,14 +169,15 @@ class Plugin(BasePlugin): Args: permissions: List of permissions to install """ - cur_perm = set(x.name for x in self.permissions or []) + cur_perm = set(x for x in self.permissions or []) all_perm = set(permissions) new_perms = all_perm - cur_perm - _perms = [ Permission(name=x, plugin_=self) for x in new_perms ] - #self.permissions = list(filter(lambda x: x.name in permissions, self.permissions and isinstance(self.permissions, list) or [])) + _perms = [Permission(name=x, plugin_=self) for x in new_perms] + # self.permissions = list(filter(lambda x: x.name in permissions, self.permissions and isinstance(self.permissions, list) or [])) self.permissions.extend(_perms) + class AuthPlugin(Plugin): """Base class for all authentification plugins diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index 0f923c4..03b5e4a 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -56,7 +56,7 @@ def service_debit(): class BalancePlugin(Plugin): - #id = "dev.flaschengeist.balance" + # id = "dev.flaschengeist.balance" models = models def install(self): @@ -64,6 +64,7 @@ class BalancePlugin(Plugin): def load(self): from .routes import blueprint + self.blueprint = blueprint @plugins_loaded diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index e11b6b7..48a261b 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -147,7 +147,6 @@ def get_balances(start: datetime = None, end: datetime = None, limit=None, offse all = {} for user in users: - all[user.userid] = [user.get_credit(start, end), 0] all[user.userid][1] = user.get_debit(start, end) diff --git a/flaschengeist/plugins/scheduler.py b/flaschengeist/plugins/scheduler.py index 1a31c8a..ea26282 100644 --- a/flaschengeist/plugins/scheduler.py +++ b/flaschengeist/plugins/scheduler.py @@ -61,6 +61,7 @@ class SchedulerPlugin(Plugin): def run_tasks(self): from ..database import db + self = db.session.merge(self) changed = False diff --git a/flaschengeist/plugins/users/__init__.py b/flaschengeist/plugins/users/__init__.py index e819486..af344e2 100644 --- a/flaschengeist/plugins/users/__init__.py +++ b/flaschengeist/plugins/users/__init__.py @@ -106,7 +106,7 @@ def frontend(userid, current_session): raise Forbidden if request.method == "POST": - if request.content_length > 1024 ** 2: + if request.content_length > 1024**2: raise BadRequest current_session.user_.set_attribute("frontend", request.get_json()) return no_content() @@ -218,7 +218,9 @@ def edit_user(userid, current_session): userController.set_roles(user, roles) userController.modify_user(user, password, new_password) - userController.update_user(user) + userController.update_user( + user, + ) return no_content()