mirror of
https://gitflic.ru/project/alt-gnome/karkas.git
synced 2024-12-23 16:23:02 +03:00
добавлен блок standard.users
This commit is contained in:
parent
fa89265197
commit
a3f0298288
@ -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),
|
||||
|
15
src/karkas_blocks/karkas_blocks/standard/users/__init__.py
Normal file
15
src/karkas_blocks/karkas_blocks/standard/users/__init__.py
Normal 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())
|
@ -0,0 +1 @@
|
||||
from .piccolo_app import APP_CONFIG
|
@ -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=[],
|
||||
)
|
@ -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
|
@ -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()
|
13
src/karkas_blocks/karkas_blocks/standard/users/info.json
Normal file
13
src/karkas_blocks/karkas_blocks/standard/users/info.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
36
src/karkas_blocks/karkas_blocks/standard/users/main.py
Normal file
36
src/karkas_blocks/karkas_blocks/standard/users/main.py
Normal 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
|
Loading…
Reference in New Issue
Block a user