76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
from aiogram.types import Message, CallbackQuery
|
|
from decimal import Decimal
|
|
from datetime import datetime, time
|
|
|
|
from ....model.bot_entity import BotEntity
|
|
from ....model.bot_enum import BotEnum
|
|
from ....model.descriptors import EntityFieldDescriptor
|
|
from ....model.settings import Settings
|
|
from ....model.user import UserBase
|
|
from ....utils.main import get_callable_str, get_value_repr
|
|
from ..context import ContextData, CommandContext
|
|
from .boolean import bool_editor
|
|
from .date import date_picker, time_picker
|
|
from .entity import entity_picker
|
|
from .string import string_editor
|
|
|
|
|
|
async def show_editor(message: Message | CallbackQuery, **kwargs):
|
|
field_descriptor: EntityFieldDescriptor = kwargs["field_descriptor"]
|
|
current_value = kwargs["current_value"]
|
|
user: UserBase = kwargs["user"]
|
|
callback_data: ContextData = kwargs.get("callback_data", None)
|
|
state_data: dict = kwargs["state_data"]
|
|
|
|
value_type = field_descriptor.type_base
|
|
|
|
if field_descriptor.edit_prompt:
|
|
edit_prompt = get_callable_str(
|
|
field_descriptor.edit_prompt, field_descriptor, None, current_value
|
|
)
|
|
else:
|
|
if field_descriptor.caption:
|
|
caption_str = get_callable_str(
|
|
field_descriptor.caption, field_descriptor, None, current_value
|
|
)
|
|
else:
|
|
caption_str = field_descriptor.name
|
|
if callback_data.context == CommandContext.ENTITY_EDIT:
|
|
edit_prompt = (
|
|
await Settings.get(
|
|
Settings.APP_STRINGS_FIELD_EDIT_PROMPT_TEMPLATE_P_NAME_VALUE
|
|
)
|
|
).format(
|
|
name=caption_str,
|
|
value=get_value_repr(current_value, field_descriptor, user.lang),
|
|
)
|
|
else:
|
|
edit_prompt = (
|
|
await Settings.get(
|
|
Settings.APP_STRINGS_FIELD_CREATE_PROMPT_TEMPLATE_P_NAME
|
|
)
|
|
).format(name=caption_str)
|
|
|
|
kwargs["edit_prompt"] = edit_prompt
|
|
|
|
if value_type not in [int, float, Decimal, str]:
|
|
state_data.update({"context_data": callback_data.pack()})
|
|
|
|
if value_type is bool:
|
|
await bool_editor(message=message, **kwargs)
|
|
|
|
elif value_type in [int, float, Decimal, str]:
|
|
await string_editor(message=message, **kwargs)
|
|
|
|
elif value_type is datetime:
|
|
await date_picker(message=message, **kwargs)
|
|
|
|
elif value_type is time:
|
|
await time_picker(message=message, **kwargs)
|
|
|
|
elif issubclass(value_type, BotEntity) or issubclass(value_type, BotEnum):
|
|
await entity_picker(message=message, **kwargs)
|
|
|
|
else:
|
|
raise ValueError(f"Unsupported field type: {value_type}")
|