Files
quickbot/bot/handlers/editors/string.py
Alexander Kalinovsky 6dbe0536ca init
2025-01-04 12:00:12 +01:00

95 lines
3.5 KiB
Python

from types import UnionType
from aiogram import Router
from aiogram.fsm.context import FSMContext
from aiogram.types import Message, CallbackQuery, InlineKeyboardButton, CopyTextButton
from aiogram.utils.keyboard import InlineKeyboardBuilder
from logging import getLogger
from typing import Any, get_args, get_origin
from ....model.descriptors import EntityFieldDescriptor, EntityDescriptor
from ....model.language import LanguageBase
from ....model.settings import Settings
from ....utils import serialize
from ..context import ContextData, CallbackCommand
from ..common import get_send_message, get_local_text
from .common import wrap_editor
logger = getLogger(__name__)
router = Router()
async def string_editor(message: Message | CallbackQuery,
field_descriptor: EntityFieldDescriptor,
entity_descriptor: EntityDescriptor,
callback_data: ContextData,
current_value: Any,
edit_prompt: str,
state: FSMContext,
locale_index: int = 0,
**kwargs):
keyboard_builder = InlineKeyboardBuilder()
_edit_prompt = edit_prompt
type_ = field_descriptor.type_
type_origin = get_origin(type_)
if type_origin == UnionType:
type_ = get_args(type_)[0]
if type_ == str and field_descriptor.localizable:
current_locale = list(LanguageBase.all_members.values())[locale_index]
context_data = ContextData(
command = CallbackCommand.FIELD_EDITOR_CALLBACK,
context = callback_data.context,
entity_name = callback_data.entity_name,
entity_id = callback_data.entity_id,
field_name = callback_data.field_name,
save_state = True)
_edit_prompt = f"{edit_prompt}\n{(await Settings.get(
Settings.APP_STRINGS_STRING_EDITOR_LOCALE_TEMPLATE_P_NAME)).format(name = current_locale)}"
_current_value = get_local_text(current_value, current_locale)
await state.update_data({
"context_data": context_data.pack(),
"edit_prompt": edit_prompt,
"locale_index": str(locale_index),
"current_value": current_value})
else:
context_data = ContextData(
command = CallbackCommand.FIELD_EDITOR_CALLBACK,
context = callback_data.context,
entity_name = callback_data.entity_name,
entity_id = callback_data.entity_id,
field_name = callback_data.field_name,
save_state = True)
_current_value = serialize(current_value, field_descriptor)
await state.update_data({
"context_data": context_data.pack()})
if _current_value:
_current_value_caption = f"{_current_value[:30]}..." if len(_current_value) > 30 else _current_value
keyboard_builder.row(InlineKeyboardButton(text = _current_value_caption,
copy_text = CopyTextButton(text = _current_value)))
await wrap_editor(keyboard_builder = keyboard_builder,
field_descriptor = field_descriptor,
entity_descriptor = entity_descriptor,
callback_data = callback_data,
state = state)
send_message = get_send_message(message)
await send_message(text = _edit_prompt, reply_markup = keyboard_builder.as_markup())
async def context_command_fiter(*args, **kwargs):
print(args, kwargs)
return True