68 lines
2.9 KiB
Python
68 lines
2.9 KiB
Python
from types import NoneType, UnionType
|
|
from typing import get_args, get_origin
|
|
from aiogram.fsm.context import FSMContext
|
|
from aiogram.types import InlineKeyboardButton
|
|
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
|
|
|
from ....model.descriptors import EntityFieldDescriptor, EntityDescriptor
|
|
from ....model.settings import Settings
|
|
from ..context import ContextData, CallbackCommand, CommandContext
|
|
from ..navigation import get_navigation_context
|
|
|
|
|
|
async def wrap_editor(keyboard_builder: InlineKeyboardBuilder,
|
|
field_descriptor: EntityFieldDescriptor,
|
|
entity_descriptor: EntityDescriptor,
|
|
callback_data: ContextData,
|
|
state: FSMContext):
|
|
|
|
if callback_data.context in [CommandContext.ENTITY_CREATE, CommandContext.ENTITY_EDIT]:
|
|
|
|
btns = []
|
|
field_index = entity_descriptor.field_sequence.index(field_descriptor.name)
|
|
|
|
stack, context = await get_navigation_context(state)
|
|
|
|
if field_index > 0:
|
|
btns.append(InlineKeyboardButton(
|
|
text = (await Settings.get(Settings.APP_STRINGS_BACK_BTN)),
|
|
callback_data = ContextData(
|
|
command = CallbackCommand.FIELD_EDITOR,
|
|
context = callback_data.context,
|
|
entity_name = callback_data.entity_name,
|
|
entity_id = callback_data.entity_id,
|
|
field_name = entity_descriptor.field_sequence[field_index - 1],
|
|
save_state = True).pack()))
|
|
|
|
if get_origin(field_descriptor.type_) == UnionType:
|
|
args = get_args(field_descriptor.type_)
|
|
if args[1] == NoneType:
|
|
btns.append(InlineKeyboardButton(
|
|
text = (await Settings.get(Settings.APP_STRINGS_SKIP_BTN)),
|
|
callback_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).pack()))
|
|
|
|
keyboard_builder.row(*btns)
|
|
|
|
|
|
keyboard_builder.row(InlineKeyboardButton(
|
|
text = (await Settings.get(Settings.APP_STRINGS_CANCEL_BTN)),
|
|
callback_data = context.pack()))
|
|
|
|
elif callback_data.context == CommandContext.SETTING_EDIT:
|
|
|
|
keyboard_builder.row(
|
|
InlineKeyboardButton(
|
|
text = (await Settings.get(Settings.APP_STRINGS_CANCEL_BTN)),
|
|
callback_data = ContextData(
|
|
command = CallbackCommand.FIELD_EDITOR_CALLBACK,
|
|
context = callback_data.context,
|
|
field_name = callback_data.field_name,
|
|
data = "cancel").pack()))
|
|
|