добавлен блок standard.users

This commit is contained in:
Maxim Slipenko 2024-08-20 17:08:51 +03:00
parent fa89265197
commit a3f0298288
No known key found for this signature in database
GPG Key ID: 6DEA9FA7DE98C7D6
9 changed files with 194 additions and 0 deletions

View File

@ -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),

View File

@ -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())

View File

@ -0,0 +1 @@
from .piccolo_app import APP_CONFIG

View File

@ -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=[],
)

View File

@ -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

View File

@ -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()

View File

@ -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"
}
}
}

View File

@ -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