add command params
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from babel.support import LazyProxy
|
||||
from inspect import signature
|
||||
from aiogram.types import Message, CallbackQuery
|
||||
from aiogram.utils.i18n import I18n
|
||||
from typing import Any, TYPE_CHECKING
|
||||
import ujson as json
|
||||
|
||||
@@ -9,7 +10,7 @@ from ..model.bot_enum import BotEnum
|
||||
from ..model.settings import Settings
|
||||
|
||||
from ..model.descriptors import (
|
||||
EntityFieldDescriptor,
|
||||
FieldDescriptor,
|
||||
EntityDescriptor,
|
||||
EntityItemCaptionCallable,
|
||||
EntityFieldCaptionCallable,
|
||||
@@ -36,7 +37,13 @@ def get_user_permissions(
|
||||
return permissions
|
||||
|
||||
|
||||
def get_local_text(text: str, locale: str) -> str:
|
||||
def get_local_text(text: str, locale: str = None) -> str:
|
||||
if not locale:
|
||||
i18n = I18n.get_current(no_error=True)
|
||||
if i18n:
|
||||
locale = i18n.current_locale
|
||||
else:
|
||||
locale = "en"
|
||||
try:
|
||||
obj = json.loads(text) # @IgnoreException
|
||||
except Exception:
|
||||
@@ -116,7 +123,7 @@ def get_entity_item_repr(
|
||||
|
||||
|
||||
def get_value_repr(
|
||||
value: Any, field_descriptor: EntityFieldDescriptor, locale: str | None = None
|
||||
value: Any, field_descriptor: FieldDescriptor, locale: str | None = None
|
||||
) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
@@ -157,7 +164,7 @@ def get_callable_str(
|
||||
| EntityItemCaptionCallable
|
||||
| EntityFieldCaptionCallable
|
||||
),
|
||||
descriptor: EntityFieldDescriptor | EntityDescriptor,
|
||||
descriptor: FieldDescriptor | EntityDescriptor,
|
||||
entity: Any = None,
|
||||
value: Any = None,
|
||||
) -> str:
|
||||
@@ -185,9 +192,17 @@ def get_entity_descriptor(
|
||||
|
||||
def get_field_descriptor(
|
||||
app: "QBotApp", callback_data: ContextData
|
||||
) -> EntityFieldDescriptor | None:
|
||||
) -> FieldDescriptor | None:
|
||||
if callback_data.context == CommandContext.SETTING_EDIT:
|
||||
return Settings.list_params()[callback_data.field_name]
|
||||
elif callback_data.context == CommandContext.COMMAND_FORM:
|
||||
command = app.bot_commands[callback_data.user_command.split("&")[0]]
|
||||
if (
|
||||
command
|
||||
and command.param_form
|
||||
and callback_data.field_name in command.param_form
|
||||
):
|
||||
return command.param_form[callback_data.field_name]
|
||||
elif callback_data.context in [
|
||||
CommandContext.ENTITY_CREATE,
|
||||
CommandContext.ENTITY_EDIT,
|
||||
|
||||
48
utils/navigation.py
Normal file
48
utils/navigation.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from ..bot.handlers.context import ContextData, CallbackCommand
|
||||
|
||||
|
||||
def save_navigation_context(
|
||||
callback_data: ContextData, state_data: dict
|
||||
) -> list[ContextData]:
|
||||
stack = [
|
||||
ContextData.unpack(item) for item in state_data.get("navigation_stack", [])
|
||||
]
|
||||
data_nc = state_data.get("navigation_context")
|
||||
navigation_context = ContextData.unpack(data_nc) if data_nc else None
|
||||
if callback_data.back:
|
||||
callback_data.back = False
|
||||
if stack:
|
||||
stack.pop()
|
||||
else:
|
||||
if (
|
||||
stack
|
||||
and navigation_context
|
||||
and navigation_context.command == callback_data.command
|
||||
and navigation_context.command != CallbackCommand.USER_COMMAND
|
||||
):
|
||||
navigation_context = callback_data
|
||||
elif navigation_context:
|
||||
stack.append(navigation_context)
|
||||
|
||||
state_data["navigation_stack"] = [item.pack() for item in stack]
|
||||
state_data["navigation_context"] = callback_data.pack()
|
||||
|
||||
return stack
|
||||
|
||||
|
||||
def pop_navigation_context(stack: list[ContextData]) -> ContextData | None:
|
||||
if stack:
|
||||
data = stack[-1]
|
||||
data.back = True
|
||||
return data
|
||||
|
||||
|
||||
def get_navigation_context(
|
||||
state_data: dict,
|
||||
) -> tuple[list[ContextData], ContextData | None]:
|
||||
data_nc = state_data.get("navigation_context")
|
||||
context = ContextData.unpack(data_nc) if data_nc else None
|
||||
return (
|
||||
[ContextData.unpack(item) for item in state_data.get("navigation_stack", [])],
|
||||
context,
|
||||
)
|
||||
@@ -8,7 +8,7 @@ import ujson as json
|
||||
|
||||
from ..model.bot_entity import BotEntity
|
||||
from ..model.bot_enum import BotEnum
|
||||
from ..model.descriptors import EntityFieldDescriptor
|
||||
from ..model.descriptors import FieldDescriptor
|
||||
|
||||
|
||||
async def deserialize[T](session: AsyncSession, type_: type[T], value: str = None) -> T:
|
||||
@@ -73,7 +73,7 @@ async def deserialize[T](session: AsyncSession, type_: type[T], value: str = Non
|
||||
return type_(value)
|
||||
|
||||
|
||||
def serialize(value: Any, field_descriptor: EntityFieldDescriptor) -> str:
|
||||
def serialize(value: Any, field_descriptor: FieldDescriptor) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
type_ = field_descriptor.type_base
|
||||
|
||||
Reference in New Issue
Block a user