1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
"""Model of LOD database for Telegram""" |
3
|
|
|
|
4
|
|
|
from collections import defaultdict |
5
|
|
|
|
6
|
|
|
from callbaker import callback_from_info |
7
|
|
|
from keyboa import Keyboa |
8
|
|
|
from loglan_core.addons.word_selector import WordSelector |
9
|
|
|
from loglan_core.connect_tables import t_connect_keys |
10
|
|
|
from loglan_core.definition import BaseDefinition |
11
|
|
|
from loglan_core.key import BaseKey |
12
|
|
|
from loglan_core.word import BaseWord |
13
|
|
|
from sqlalchemy import select |
14
|
|
|
|
15
|
|
|
from app.bot.telegram import MIN_NUMBER_OF_BUTTONS |
16
|
|
|
from app.bot.telegram.variables import ( |
17
|
|
|
t, |
18
|
|
|
cbd, |
19
|
|
|
mark_action, |
20
|
|
|
mark_entity, |
21
|
|
|
mark_record_id, |
22
|
|
|
mark_slice_start, |
23
|
|
|
action_predy_send_card, |
24
|
|
|
entity_predy, |
25
|
|
|
action_predy_kb_cpx_show, |
26
|
|
|
action_predy_kb_cpx_hide, |
27
|
|
|
) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class TelegramDefinition(BaseDefinition): |
31
|
|
|
"""Definition class extensions for Telegram""" |
32
|
|
|
|
33
|
|
|
def export(self): |
34
|
|
|
""" |
35
|
|
|
Convert definition's data to str for sending as a telegram messages |
36
|
|
|
:return: Adopted for posting in telegram string |
37
|
|
|
""" |
38
|
|
|
d_usage = f"<b>{self.usage.replace('%', '—')}</b> " if self.usage else "" |
39
|
|
|
d_grammar = ( |
40
|
|
|
f"({self.slots if self.slots is not None else ''}{self.grammar_code}) " |
41
|
|
|
) |
42
|
|
|
d_body = ( |
43
|
|
|
self.body.replace("<", "<") |
44
|
|
|
.replace(">", ">") |
45
|
|
|
.replace("«", "<i>") |
46
|
|
|
.replace("»", "</i>") |
47
|
|
|
.replace("{", "<code>") |
48
|
|
|
.replace("}", "</code>") |
49
|
|
|
.replace("....", "….") |
50
|
|
|
.replace("...", "…") |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
d_case_tags = f" [{self.case_tags}]" if self.case_tags else "" |
54
|
|
|
return f"{d_usage}{d_grammar}{d_body}{d_case_tags}" |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class TelegramWord(BaseWord): |
58
|
|
|
"""Word class extensions for Telegram""" |
59
|
|
|
|
60
|
|
|
def format_affixes(self): |
61
|
|
|
return ( |
62
|
|
|
f" ({' '.join([w.name for w in self.affixes]).strip()}" |
63
|
|
|
if self.affixes |
64
|
|
|
else "" |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
def format_year(self): |
68
|
|
|
return "'" + str(self.year.year)[-2:] + " " if self.year else "" |
69
|
|
|
|
70
|
|
|
def format_origin(self): |
71
|
|
|
if self.origin or self.origin_x: |
72
|
|
|
return f"\n<i><{self.origin}{' = ' + self.origin_x if self.origin_x else ''}></i>" |
73
|
|
|
return "" |
74
|
|
|
|
75
|
|
|
def format_authors(self): |
76
|
|
|
return ( |
77
|
|
|
"/".join([a.abbreviation for a in self.authors]) + " " |
78
|
|
|
if self.authors |
79
|
|
|
else "" |
80
|
|
|
) |
81
|
|
|
|
82
|
|
|
def format_rank(self): |
83
|
|
|
return self.rank + " " if self.rank else "" |
84
|
|
|
|
85
|
|
|
def export(self, session) -> str: |
86
|
|
|
""" |
87
|
|
|
Convert word's data to str for sending as a telegram messages |
88
|
|
|
:return: List of str with technical info, definitions, used_in part |
89
|
|
|
""" |
90
|
|
|
w_affixes = self.format_affixes() |
91
|
|
|
w_match = self.match + " " if self.match else "" |
92
|
|
|
w_year = self.format_year() |
93
|
|
|
w_orig = self.format_origin() |
94
|
|
|
w_authors = self.format_authors() |
95
|
|
|
w_type = self.type.type + " " |
96
|
|
|
w_rank = self.format_rank() |
97
|
|
|
|
98
|
|
|
word_str = ( |
99
|
|
|
f"<b>{self.name}</b>{w_affixes}," |
100
|
|
|
f"\n{w_match}{w_type}{w_authors}{w_year}{w_rank}{w_orig}" |
101
|
|
|
) |
102
|
|
|
return f"{word_str}\n\n{self.get_definitions(session=session)}" |
103
|
|
|
|
104
|
|
|
def get_definitions(self, session) -> str: |
105
|
|
|
""" |
106
|
|
|
Get all definitions of the word |
107
|
|
|
:param session: Session |
108
|
|
|
:return: List of Definition objects ordered by position |
109
|
|
|
""" |
110
|
|
|
definitions = ( |
111
|
|
|
session.query(TelegramDefinition) |
112
|
|
|
.filter(BaseDefinition.word_id == self.id) |
113
|
|
|
.order_by(BaseDefinition.position.asc()) |
114
|
|
|
.all() |
115
|
|
|
) |
116
|
|
|
return "\n\n".join([d.export() for d in definitions]) |
117
|
|
|
|
118
|
|
|
@classmethod |
119
|
|
|
def translation_by_key(cls, session, request: str, language: str = None) -> str: |
120
|
|
|
""" |
121
|
|
|
We get information about loglan words by key in a foreign language |
122
|
|
|
:param session: Session |
123
|
|
|
:param request: Requested string |
124
|
|
|
:param language: Key language |
125
|
|
|
:return: Search results string formatted for sending to Telegram |
126
|
|
|
""" |
127
|
|
|
words_request = ( |
128
|
|
|
select(cls.name, TelegramDefinition) |
129
|
|
|
.join(BaseDefinition) |
130
|
|
|
.join(t_connect_keys) |
131
|
|
|
.join(BaseKey) |
132
|
|
|
.filter(BaseKey.word == request) |
133
|
|
|
.filter(BaseKey.language == language) |
134
|
|
|
.order_by(cls.id, BaseDefinition.position) |
135
|
|
|
) |
136
|
|
|
words = session.execute(words_request).all() |
137
|
|
|
result = defaultdict(list) |
138
|
|
|
|
139
|
|
|
for word in words: |
140
|
|
|
name, definition = word |
141
|
|
|
result[name].append(definition.export()) |
142
|
|
|
|
143
|
|
|
new = "\n" |
144
|
|
|
word_items = [ |
145
|
|
|
f"/{word_name},\n{new.join(definitions)}\n" |
146
|
|
|
for word_name, definitions in result.items() |
147
|
|
|
] |
148
|
|
|
return new.join(word_items).strip() |
149
|
|
|
|
150
|
|
|
def _keyboard_navi(self, index_start: int, index_end: int, delimiter: int): |
151
|
|
|
""" |
152
|
|
|
:param index_start: |
153
|
|
|
:param index_end: |
154
|
|
|
:param delimiter: |
155
|
|
|
:return: |
156
|
|
|
""" |
157
|
|
|
text_arrow_back = "\U0000276E" * 2 |
158
|
|
|
text_arrow_forward = "\U0000276F" * 2 |
159
|
|
|
button_back, button_forward = None, None |
160
|
|
|
|
161
|
|
|
common_data = { |
162
|
|
|
mark_entity: entity_predy, |
163
|
|
|
mark_action: action_predy_kb_cpx_show, |
164
|
|
|
mark_record_id: self.id, |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if index_start != 0: |
168
|
|
|
cbd_predy_kb_cpx_back = { |
169
|
|
|
**common_data, |
170
|
|
|
mark_slice_start: index_start - delimiter, |
171
|
|
|
} |
172
|
|
|
button_back = { |
173
|
|
|
t: text_arrow_back, |
174
|
|
|
cbd: callback_from_info(cbd_predy_kb_cpx_back), |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if index_end != len(self.complexes): |
178
|
|
|
cbd_predy_kb_cpx_forward = { |
179
|
|
|
**common_data, |
180
|
|
|
mark_slice_start: index_end, |
181
|
|
|
} |
182
|
|
|
button_forward = { |
183
|
|
|
t: text_arrow_forward, |
184
|
|
|
cbd: callback_from_info(cbd_predy_kb_cpx_forward), |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
nav_row = [b for b in [button_back, button_forward] if b] |
188
|
|
|
return Keyboa(nav_row, items_in_row=2)() |
189
|
|
|
|
190
|
|
|
def _keyboard_hide(self, total_number_of_complexes: int): |
191
|
|
|
""" |
192
|
|
|
:param total_number_of_complexes: |
193
|
|
|
:return: |
194
|
|
|
""" |
195
|
|
|
text_cpx_hide = f"Hide Complex{'es' if total_number_of_complexes > 1 else ''}" |
196
|
|
|
cbd_predy_kb_cpx_hide = { |
197
|
|
|
mark_entity: entity_predy, |
198
|
|
|
mark_action: action_predy_kb_cpx_hide, |
199
|
|
|
mark_record_id: self.id, |
200
|
|
|
} |
201
|
|
|
button_predy_kb_cpx_hide = [ |
202
|
|
|
{t: text_cpx_hide, cbd: callback_from_info(cbd_predy_kb_cpx_hide)}, |
203
|
|
|
] |
204
|
|
|
return Keyboa(button_predy_kb_cpx_hide)() |
205
|
|
|
|
206
|
|
|
def _keyboard_show(self, total_number_of_complexes: int): |
207
|
|
|
""" |
208
|
|
|
:param total_number_of_complexes: |
209
|
|
|
:return: |
210
|
|
|
""" |
211
|
|
|
text_cpx_show = ( |
212
|
|
|
f"Show Complex{'es' if total_number_of_complexes > 1 else ''}" |
213
|
|
|
f" ({total_number_of_complexes})" |
214
|
|
|
) |
215
|
|
|
cbd_predy_kb_cpx_show = { |
216
|
|
|
mark_entity: entity_predy, |
217
|
|
|
mark_action: action_predy_kb_cpx_show, |
218
|
|
|
mark_record_id: self.id, |
219
|
|
|
} |
220
|
|
|
button_show = [ |
221
|
|
|
{t: text_cpx_show, cbd: callback_from_info(cbd_predy_kb_cpx_show)}, |
222
|
|
|
] |
223
|
|
|
return Keyboa.combine((Keyboa(button_show)(), kb_close())) |
224
|
|
|
|
225
|
|
|
@staticmethod |
226
|
|
|
def _get_delimiter(total_number_of_complexes: int): |
227
|
|
|
""" |
228
|
|
|
:param total_number_of_complexes: |
229
|
|
|
:return: |
230
|
|
|
""" |
231
|
|
|
allowed_range = list(range(MIN_NUMBER_OF_BUTTONS, MIN_NUMBER_OF_BUTTONS + 11)) |
232
|
|
|
lst = [(total_number_of_complexes % i, i) for i in allowed_range] |
233
|
|
|
delimiter = min(lst, key=lambda x: abs(x[0] - MIN_NUMBER_OF_BUTTONS))[1] |
234
|
|
|
for i in lst: |
235
|
|
|
if i[0] == 0: |
236
|
|
|
delimiter = i[1] |
237
|
|
|
break |
238
|
|
|
return delimiter |
239
|
|
|
|
240
|
|
|
@staticmethod |
241
|
|
|
def _keyboard_data(current_complexes: list): |
242
|
|
|
""" |
243
|
|
|
:param current_complexes: |
244
|
|
|
:return: |
245
|
|
|
""" |
246
|
|
|
cpx_items = [ |
247
|
|
|
{ |
248
|
|
|
t: cpx.name, |
249
|
|
|
cbd: callback_from_info( |
250
|
|
|
{ |
251
|
|
|
mark_entity: entity_predy, |
252
|
|
|
mark_action: action_predy_send_card, |
253
|
|
|
mark_record_id: cpx.id, |
254
|
|
|
} |
255
|
|
|
), |
256
|
|
|
} |
257
|
|
|
for cpx in current_complexes |
258
|
|
|
] |
259
|
|
|
return Keyboa(items=cpx_items, alignment=True, items_in_row=4)() |
260
|
|
|
|
261
|
|
|
def keyboard_cpx(self, show_list: bool = False, slice_start: int = 0): |
262
|
|
|
""" |
263
|
|
|
:param show_list: |
264
|
|
|
:param slice_start: |
265
|
|
|
:return: |
266
|
|
|
""" |
267
|
|
|
|
268
|
|
|
total_num_of_cpx = len(self.complexes) |
269
|
|
|
|
270
|
|
|
if not total_num_of_cpx: |
271
|
|
|
return kb_close() |
272
|
|
|
|
273
|
|
|
if not show_list: |
274
|
|
|
return self._keyboard_show(total_num_of_cpx) |
275
|
|
|
|
276
|
|
|
current_delimiter = self._get_delimiter(total_num_of_cpx) |
277
|
|
|
|
278
|
|
|
kb_cpx_hide = self._keyboard_hide(total_num_of_cpx) |
279
|
|
|
|
280
|
|
|
last_allowed_item = slice_start + current_delimiter |
281
|
|
|
slice_end = min(last_allowed_item, total_num_of_cpx) |
282
|
|
|
|
283
|
|
|
current_cpx_set = self.complexes[slice_start:slice_end] |
284
|
|
|
kb_cpx_data = self._keyboard_data(current_cpx_set) |
285
|
|
|
|
286
|
|
|
kb_cpx_nav = None |
287
|
|
|
|
288
|
|
|
if total_num_of_cpx > current_delimiter: |
289
|
|
|
kb_cpx_nav = self._keyboard_navi(slice_start, slice_end, current_delimiter) |
290
|
|
|
|
291
|
|
|
kb_combo = (kb_cpx_hide, kb_cpx_data, kb_cpx_nav, kb_close()) |
292
|
|
|
|
293
|
|
|
return Keyboa.combine(kb_combo) |
294
|
|
|
|
295
|
|
|
def send_card_to_user(self, session, bot, user_id: int | str): |
296
|
|
|
""" |
297
|
|
|
:param session: |
298
|
|
|
:param bot: |
299
|
|
|
:param user_id: |
300
|
|
|
:return: |
301
|
|
|
""" |
302
|
|
|
bot.send_message( |
303
|
|
|
chat_id=user_id, text=self.export(session), reply_markup=self.keyboard_cpx() |
304
|
|
|
) |
305
|
|
|
|
306
|
|
|
@classmethod |
307
|
|
|
def by_request(cls, session, request: str) -> list: |
308
|
|
|
""" |
309
|
|
|
:param session: |
310
|
|
|
:param request: |
311
|
|
|
:return: |
312
|
|
|
""" |
313
|
|
|
if isinstance(request, int): |
314
|
|
|
return [ |
315
|
|
|
cls.get_by_id(session, request), |
316
|
|
|
] |
317
|
|
|
return ( |
318
|
|
|
session.execute(WordSelector(TelegramWord).by_name(request)).scalars().all() |
319
|
|
|
) |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
def kb_close(): |
323
|
|
|
""" |
324
|
|
|
:return: |
325
|
|
|
""" |
326
|
|
|
return Keyboa({t: "Close", cbd: "close"})() |
327
|
|
|
|