diff --git a/src/gnomik/gnomik/__main__.py b/src/gnomik/gnomik/__main__.py index ea26330..6220b4c 100644 --- a/src/gnomik/gnomik/__main__.py +++ b/src/gnomik/gnomik/__main__.py @@ -11,6 +11,7 @@ async def main(): block_loader("standard", "config", safe=False), block_loader("standard", "database", safe=False), block_loader("standard", "statistics", safe=False), + block_loader("standard", "users", safe=False), block_loader("standard", "command_helper"), block_loader("standard", "roles", safe=False), block_loader("standard", "fsm_database_storage", safe=False), diff --git a/src/karkas_blocks/karkas_blocks/standard/users/__init__.py b/src/karkas_blocks/karkas_blocks/standard/users/__init__.py new file mode 100644 index 0000000..7a66bb9 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/users/__init__.py @@ -0,0 +1,15 @@ +from karkas_core.modules_system.public_api import ( + get_module, + register_outer_message_middleware, +) + +from .main import UsersMiddleware + + +def module_init(): + register_app_config = get_module("standard.database", "register_app_config") + from .db import APP_CONFIG + + register_app_config(APP_CONFIG) + + register_outer_message_middleware(UsersMiddleware()) diff --git a/src/karkas_blocks/karkas_blocks/standard/users/db/__init__.py b/src/karkas_blocks/karkas_blocks/standard/users/db/__init__.py new file mode 100644 index 0000000..e3e4a0b --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/users/db/__init__.py @@ -0,0 +1 @@ +from .piccolo_app import APP_CONFIG diff --git a/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_app.py b/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_app.py new file mode 100644 index 0000000..b340428 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_app.py @@ -0,0 +1,15 @@ +import os + +from karkas_piccolo.conf.apps import AppConfig + +from .tables import UserInfo + +CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) + +APP_CONFIG = AppConfig( + app_name="standard.users", + migrations_folder_path=os.path.join(CURRENT_DIRECTORY, "piccolo_migrations"), + table_classes=[UserInfo], + migration_dependencies=[], + commands=[], +) diff --git a/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_migrations/__init__.py b/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_migrations/standardusers_2024_08_20t17_06_44_026509.py b/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_migrations/standardusers_2024_08_20t17_06_44_026509.py new file mode 100644 index 0000000..781df32 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/users/db/piccolo_migrations/standardusers_2024_08_20t17_06_44_026509.py @@ -0,0 +1,104 @@ +from piccolo.apps.migrations.auto.migration_manager import MigrationManager +from piccolo.columns.column_types import Date, Integer, Text +from piccolo.columns.defaults.date import DateNow +from piccolo.columns.indexes import IndexMethod + +ID = "2024-08-20T17:06:44:026509" +VERSION = "1.16.0" +DESCRIPTION = "" + + +async def forwards(): + manager = MigrationManager( + migration_id=ID, app_name="standard.users", description=DESCRIPTION + ) + + manager.add_table( + class_name="UserInfo", tablename="user_info", schema=None, columns=None + ) + + manager.add_column( + table_class_name="UserInfo", + tablename="user_info", + column_name="user_id", + db_column_name="user_id", + column_class_name="Integer", + column_class=Integer, + params={ + "default": 0, + "null": False, + "primary_key": True, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="UserInfo", + tablename="user_info", + column_name="name", + db_column_name="name", + column_class_name="Text", + column_class=Text, + params={ + "default": "", + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="UserInfo", + tablename="user_info", + column_name="tag", + db_column_name="tag", + column_class_name="Text", + column_class=Text, + params={ + "default": "", + "null": True, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="UserInfo", + tablename="user_info", + column_name="created_at", + db_column_name="created_at", + column_class_name="Date", + column_class=Date, + params={ + "default": DateNow(), + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + return manager diff --git a/src/karkas_blocks/karkas_blocks/standard/users/db/tables.py b/src/karkas_blocks/karkas_blocks/standard/users/db/tables.py new file mode 100644 index 0000000..3760387 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/users/db/tables.py @@ -0,0 +1,9 @@ +from piccolo.columns import Date, Integer, Text +from piccolo.table import Table + + +class UserInfo(Table): + user_id = Integer(primary_key=True) + name = Text() + tag = Text(null=True) + created_at = Date() diff --git a/src/karkas_blocks/karkas_blocks/standard/users/info.json b/src/karkas_blocks/karkas_blocks/standard/users/info.json new file mode 100644 index 0000000..f43c023 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/users/info.json @@ -0,0 +1,13 @@ +{ + "id": "standard.users", + "name": "Пользователи", + "description": "Очень полезный модуль", + "author": "Karkas Team", + "version": "1.0.0", + "privileged": true, + "dependencies": { + "required": { + "standard.database": "^1.0.0" + } + } + } diff --git a/src/karkas_blocks/karkas_blocks/standard/users/main.py b/src/karkas_blocks/karkas_blocks/standard/users/main.py new file mode 100644 index 0000000..6c88781 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/users/main.py @@ -0,0 +1,36 @@ +from typing import Any, Awaitable, Callable, Dict + +from aiogram import BaseMiddleware +from aiogram.types import TelegramObject, User + +from .db.tables import UserInfo + + +async def update_user_info(user: User): + if user.last_name is None: + user_name = user.first_name + else: + user_name = user.first_name + " " + user.last_name + + await UserInfo.insert( + UserInfo(user_id=user.id, name=user_name, tag=user.username) + ).on_conflict( + action="DO UPDATE", + values=[ + UserInfo.name, + UserInfo.tag, + ], + ).run() + + +class UsersMiddleware(BaseMiddleware): + async def __call__( + self, + handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]], + event: TelegramObject, + data: Dict[str, Any], + ) -> Any: + user = event.from_user + await update_user_info(user) + result = await handler(event, data) + return result