Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update kb #82

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Working DRAFT
torrua committed Dec 11, 2024
commit b7cfbce0f0a8fc518d43e87b587a0888cc192e69
30 changes: 5 additions & 25 deletions app/bot/telegram/handlers/bib_functions.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
from app.bot.telegram import bot, cbq
from app.bot.telegram.keyboards import WordKeyboard
from app.bot.telegram.models import export_as_str
from app.bot.telegram.variables import mark_record_id, mark_slice_start
from app.bot.telegram.variables import mark_record_id, mark_slice_start, mark_action
from app.decorators import logging_time
from app.engine import async_session_maker

@@ -24,7 +24,7 @@ async def bib_cancel(call: cbq):
@logging_time
async def bib_predy_send_card(call: cbq):
"""
Обработка нажатия кнопки со словом на логлане
Обработка нажатия кнопки со logpua
:param call:
:return:
"""
@@ -46,15 +46,15 @@ async def bib_predy_send_card(call: cbq):


@logging_time
async def bib_predy_kb_cpx_switcher(call: cbq, state: bool):
async def bib_predy_kb_cpx_switcher(call: cbq):
"""
Обработка нажатия кнопки отображения/скрытия комплексных слов
:param call:
:param state:
:return:
"""
info = info_from_callback(call.data)
slice_start = info.pop(mark_slice_start, 0)
action = info.pop(mark_action, "")

async with async_session_maker() as session:
word = await (
@@ -65,31 +65,11 @@ async def bib_predy_kb_cpx_switcher(call: cbq, state: bool):
)

keyboard = WordKeyboard(word).keyboard_cpx(
show_list=state, slice_start=slice_start
action=action, slice_start=slice_start
)

await bot.edit_message_reply_markup(
chat_id=call.from_user.id,
message_id=call.message.message_id,
reply_markup=keyboard,
)


@logging_time
async def bib_predy_kb_cpx_show(call: cbq):
"""
Обработка нажатия кнопки отображения комплексных слов
:param call:
:return:
"""
await bib_predy_kb_cpx_switcher(call, True)


@logging_time
async def bib_predy_kb_cpx_hide(call: cbq):
"""
Обработка нажатия кнопки скрытия комплексных слов
:param call:
:return:
"""
await bib_predy_kb_cpx_switcher(call, False)
20 changes: 9 additions & 11 deletions app/bot/telegram/handlers/inline.py
Original file line number Diff line number Diff line change
@@ -8,15 +8,11 @@
from app.bot.telegram.handlers.bib_functions import bib_cancel
from app.bot.telegram.handlers.bib_functions import (
bib_predy_send_card,
bib_predy_kb_cpx_hide,
bib_predy_kb_cpx_show,
)
from app.bot.telegram.variables import (
action_predy_send_card,
action_predy_kb_cpx_hide,
action_predy_kb_cpx_show,
bib_predy_kb_cpx_switcher,
)

from app.bot.telegram.variables import (
Action,
cancel,
close,
mark_entity,
@@ -72,10 +68,12 @@ async def action_selector_predy(call: cbq):
current_action = info.get(mark_action, None)

actions = {
action_predy_send_card: bib_predy_send_card,
action_predy_kb_cpx_hide: bib_predy_kb_cpx_hide,
action_predy_kb_cpx_show: bib_predy_kb_cpx_show,
Action.send_card: bib_predy_send_card,
Action.kb_cpx_hide: bib_predy_kb_cpx_switcher,
Action.kb_cpx_show: bib_predy_kb_cpx_switcher,
Action.kb_afx_hide: bib_predy_kb_cpx_switcher,
Action.kb_afx_show: bib_predy_kb_cpx_switcher,
}

if action_to_run := actions.get(current_action):
if action_to_run := actions.get(current_action): # TODO Add default keyboard
await action_to_run(call)
143 changes: 85 additions & 58 deletions app/bot/telegram/keyboards.py
Original file line number Diff line number Diff line change
@@ -6,13 +6,11 @@
mark_entity,
entity_predy,
mark_action,
action_predy_kb_cpx_show,
mark_record_id,
mark_slice_start,
t,
cbd,
action_predy_kb_cpx_hide,
action_predy_send_card,
Action,
)


@@ -49,7 +47,7 @@ def keyboard_data(slice_start: int, items: list):
cbd: callback_from_info(
{
mark_entity: entity_predy,
mark_action: action_predy_send_card,
mark_action: Action.send_card,
mark_record_id: item.id,
}
),
@@ -117,47 +115,20 @@ def kb_close():
return Keyboa({t: "Close", cbd: "close"})()


def keyboard_hide(title: str, word_id: int):
def keyboard_show_hide(title: str, word_id: int, action_mark: str):
"""
:return:
"""

text_hide = f"Hide {title}"
cbd_predy_kb_cpx_hide = {
mark_entity: entity_predy,
mark_action: action_predy_kb_cpx_hide,
mark_record_id: word_id,
}
button_predy_kb_cpx_hide = [
{t: text_hide, cbd: callback_from_info(cbd_predy_kb_cpx_hide)},
]
return Keyboa(button_predy_kb_cpx_hide)()


def keyboard_complete(slice_start: int, items: list, word_id: int, title: str):
kb_data = keyboard_data(slice_start, items)
kb_navi = keyboard_navi(slice_start, len(items), word_id, action_predy_kb_cpx_show)
kb_hide = keyboard_hide(title, word_id)
kb_combo = (kb_hide, kb_data, kb_navi, kb_close())
return Keyboa.combine(kb_combo)


def keyboard_show(number_of_items: int, title: str, word_id: int, action_mark: str):
"""
:return:
"""
number = f" ({number_of_items})" if number_of_items > 1 else ""
text_cpx_show = f"Show {title}{number}"

cbd_predy_kb_cpx_show = {
cbd_predy = {
mark_entity: entity_predy,
mark_action: action_mark,
mark_record_id: word_id,
}
button_show = [
{t: text_cpx_show, cbd: callback_from_info(cbd_predy_kb_cpx_show)},
button = [
{t: title, cbd: callback_from_info(cbd_predy)},
]
return Keyboa.combine((Keyboa(button_show)(), kb_close()))
return Keyboa(button)()


class WordKeyboard:
@@ -171,29 +142,85 @@ def _get_items(self):
return self.word.parents
return self.word.complexes

def get_title(self):
if self.word.type.parentable:
return "Parent" + f"{'s' if len(self.word.parents) > 1 else ''}"
return "Complex" + f"{'es' if len(self.word.complexes) > 1 else ''}"

def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0):
def get_title(self, show: bool, items_type: str):
show_text = "Show" if show else "Hide"

match items_type:
case "parent":
return (
f"{show_text} Parent"
+ f"{f's ({len(self.word.parents)})' if len(self.word.parents) > 1 else ''}"
)
case "djifoa":
return (
f"{show_text} Djifoa"
+ f"{f' ({len(self.word.affixes)})' if len(self.word.affixes) > 1 else ''}"
)
case "complex":
return (
f"{show_text} Complex"
+ f"{f'es ({len(self.word.complexes)})' if len(self.word.complexes) > 1 else ''}"
)
case _:
return show_text

def keyboard_cpx(self, action: str = "", slice_start: int = 0):
"""
:param show_list:
:param action:
:param slice_start:
:return:
"""

if not self.items:
return kb_close()

if not show_list:
return keyboard_show(
len(self.items),
self.get_title(),
self.word.id,
action_predy_kb_cpx_show,
)

return keyboard_complete(
slice_start, self.items, self.word.id, self.get_title()
)
print(action)

match action:
case Action.kb_cpx_show:
title_djifoa = self.get_title(show=True, items_type="djifoa")
kb_hide_djifoa = keyboard_show_hide(
title_djifoa, self.word.id, Action.kb_cpx_hide
)

title_cpx = self.get_title(show=False, items_type="complex")
kb_hide_cpx = keyboard_show_hide(
title_cpx, self.word.id, Action.kb_cpx_hide
)
kb_data = keyboard_data(slice_start, self.word.complexes)

kb_navi = keyboard_navi(
slice_start,
len(self.word.complexes),
self.word.id,
Action.kb_cpx_show,
)
kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_data, kb_navi, kb_close())
return Keyboa.combine(kb_combo)

case Action.kb_cpx_hide:
title_djifoa = self.get_title(show=True, items_type="djifoa")
kb_hide_djifoa = keyboard_show_hide(
title_djifoa, self.word.id, Action.kb_cpx_show
)

title_cpx = self.get_title(show=True, items_type="complex")
kb_hide_cpx = keyboard_show_hide(
title_cpx, self.word.id, Action.kb_cpx_show
)
kb_combo = (kb_hide_djifoa, kb_hide_cpx, kb_close())
return Keyboa.combine(kb_combo)

case Action.kb_afx_show:
print("apkas")
case Action.kb_afx_hide:
print("apkah")
case _:

title_djifoa = self.get_title(show=True, items_type="djifoa")
kb_hide_djifoa = keyboard_show_hide(
title_djifoa, self.word.id, Action.kb_cpx_show
)

title_cpx = self.get_title(show=True, items_type="complex")
kb_hide_cpx = keyboard_show_hide(
title_cpx, self.word.id, Action.kb_cpx_show
)

return Keyboa.combine((kb_hide_djifoa, kb_hide_cpx, kb_close()))
10 changes: 7 additions & 3 deletions app/bot/telegram/variables.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,11 @@
# entities
entity_predy = "ep"


# actions
action_predy_send_card = "apsc"
action_predy_kb_cpx_show = "apkcs"
action_predy_kb_cpx_hide = "apkch"
class Action:
send_card = "apsc"
kb_cpx_show = "apkcs"
kb_cpx_hide = "apkch"
kb_afx_show = "apkas"
kb_afx_hide = "apkah"