enhanced strings and elements visibility delegates
All checks were successful
Build Docs / changes (push) Successful in 27s
Build Docs / build-docs (push) Has been skipped
Build Docs / deploy-docs (push) Has been skipped

This commit is contained in:
Alexander Kalinovsky
2025-04-01 00:19:51 +07:00
parent 6a7355996c
commit e10d7ff7bf
15 changed files with 458 additions and 206 deletions

View File

@@ -5,6 +5,7 @@ from aiogram.utils.keyboard import InlineKeyboardBuilder
from typing import Any, Callable, TYPE_CHECKING, Literal, Union
from babel.support import LazyProxy
from dataclasses import dataclass, field
from fastapi.datastructures import State
from sqlmodel.ext.asyncio.session import AsyncSession
from .role import RoleBase
@@ -16,29 +17,32 @@ if TYPE_CHECKING:
from ..main import QBotApp
from .user import UserBase
EntityCaptionCallable = Callable[["EntityDescriptor"], str]
EntityItemCaptionCallable = Callable[["EntityDescriptor", Any], str]
EntityFieldCaptionCallable = Callable[["FieldDescriptor", Any, Any], str]
# EntityCaptionCallable = Callable[["EntityDescriptor"], str]
# EntityItemCaptionCallable = Callable[["EntityDescriptor", Any], str]
# EntityFieldCaptionCallable = Callable[["FieldDescriptor", Any, Any], str]
@dataclass
class FieldEditButton:
field_name: str
caption: str | LazyProxy | EntityFieldCaptionCallable | None = None
visibility: Callable[[Any], bool] | None = None
caption: str | LazyProxy | Callable[["BotEntity", "BotContext"], str] | None = None
visibility: Callable[["BotEntity", "BotContext"], bool] | None = None
@dataclass
class CommandButton:
command: ContextData | Callable[[ContextData, Any], ContextData] | str
caption: str | LazyProxy | EntityItemCaptionCallable
visibility: Callable[[Any], bool] | None = None
caption: str | LazyProxy | Callable[["BotEntity", "BotContext"], str] | None = None
visibility: Callable[["BotEntity", "BotContext"], bool] | None = None
@dataclass
class InlineButton:
inline_button: InlineKeyboardButton | Callable[[Any], InlineKeyboardButton]
visibility: Callable[[Any], bool] | None = None
inline_button: (
InlineKeyboardButton
| Callable[["BotEntity", "BotContext"], InlineKeyboardButton]
)
visibility: Callable[["BotEntity", "BotContext"], bool] | None = None
@dataclass
@@ -66,8 +70,10 @@ class Filter:
@dataclass
class EntityList:
caption: str | LazyProxy | EntityCaptionCallable | None = None
item_repr: EntityItemCaptionCallable | None = None
caption: (
str | LazyProxy | Callable[["EntityDescriptor", "BotContext"], str] | None
) = None
item_repr: Callable[["BotEntity", "BotContext"], str] | None = None
show_add_new_button: bool = True
item_form: str | None = None
pagination: bool = True
@@ -79,7 +85,7 @@ class EntityList:
@dataclass
class EntityForm:
item_repr: EntityItemCaptionCallable | None = None
item_repr: Callable[["BotEntity", "BotContext"], str] | None = None
edit_field_sequence: list[str] = None
form_buttons: list[list[FieldEditButton | CommandButton | InlineButton]] = None
show_edit_button: bool = True
@@ -89,11 +95,24 @@ class EntityForm:
@dataclass(kw_only=True)
class _BaseFieldDescriptor:
icon: str = None
caption: str | LazyProxy | EntityFieldCaptionCallable | None = None
description: str | LazyProxy | EntityFieldCaptionCallable | None = None
edit_prompt: str | LazyProxy | EntityFieldCaptionCallable | None = None
caption_value: EntityFieldCaptionCallable | None = None
is_visible: bool | None = None
caption: (
str | LazyProxy | Callable[["FieldDescriptor", "BotContext"], str] | None
) = None
description: (
str | LazyProxy | Callable[["FieldDescriptor", "BotContext"], str] | None
) = None
edit_prompt: (
str
| LazyProxy
| Callable[["FieldDescriptor", Union["BotEntity", Any], "BotContext"], str]
| None
) = None
caption_value: (
Callable[["FieldDescriptor", Union["BotEntity", Any], "BotContext"], str] | None
) = None
is_visible: (
bool | Callable[["FieldDescriptor", "BotEntity", "BotContext"], bool] | None
) = None
localizable: bool = False
bool_false_value: str | LazyProxy = "no"
bool_true_value: str | LazyProxy = "yes"
@@ -140,10 +159,16 @@ class FieldDescriptor(_BaseFieldDescriptor):
@dataclass(kw_only=True)
class _BaseEntityDescriptor:
icon: str = "📘"
full_name: str | LazyProxy | EntityCaptionCallable | None = None
full_name_plural: str | LazyProxy | EntityCaptionCallable | None = None
description: str | LazyProxy | EntityCaptionCallable | None = None
item_repr: EntityItemCaptionCallable | None = None
full_name: (
str | LazyProxy | Callable[["EntityDescriptor", "BotContext"], str] | None
) = None
full_name_plural: (
str | LazyProxy | Callable[["EntityDescriptor", "BotContext"], str] | None
) = None
description: (
str | LazyProxy | Callable[["EntityDescriptor", "BotContext"], str] | None
) = None
item_repr: Callable[["BotEntity", "BotContext"], str] | None = None
default_list: EntityList = field(default_factory=EntityList)
default_form: EntityForm = field(default_factory=EntityForm)
lists: dict[str, EntityList] = field(default_factory=dict[str, EntityList])
@@ -164,9 +189,9 @@ class _BaseEntityDescriptor:
EntityPermission.DELETE_ALL: [RoleBase.SUPER_USER],
}
)
on_created: Callable[["BotEntity", "EntityEventContext"], None] | None = None
on_deleted: Callable[["BotEntity", "EntityEventContext"], None] | None = None
on_updated: Callable[["BotEntity", "EntityEventContext"], None] | None = None
on_created: Callable[["BotEntity", "BotContext"], None] | None = None
on_deleted: Callable[["BotEntity", "BotContext"], None] | None = None
on_updated: Callable[["BotEntity", "BotContext"], None] | None = None
@dataclass(kw_only=True)
@@ -202,9 +227,10 @@ class CommandCallbackContext[UT: UserBase]:
@dataclass(kw_only=True)
class EntityEventContext:
class BotContext:
db_session: AsyncSession
app: "QBotApp"
app_state: State
message: Message | CallbackQuery | None = None