Skip to content

Commit f4ae5e5

Browse files
upgrade template
1 parent 67e3092 commit f4ae5e5

File tree

12 files changed

+128
-93
lines changed

12 files changed

+128
-93
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
# Unix-style newlines with a newline ending every file
4+
[*]
5+
end_of_line = lf
6+
insert_final_newline = true
7+
8+
9+
# 4 space indentation
10+
[*.py]
11+
indent_style = space
12+
indent_size = 4
13+
charset = utf-8

example/db_adapter.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from datetime import datetime
22

3+
from .handlers.custom_types import Question
4+
35

46
def get_connection_to_data_base():
57
pass
@@ -26,7 +28,8 @@ def create_tables_from_db(connection=None):
2628
@decorator_add_connection
2729
def delete_question_by_id_db(question_id: int, connection=None):
2830
"""
29-
удалить вопрос, варианты ответа, а также всю статистику связанную с ним из бд
31+
удалить вопрос, варианты ответа,
32+
а также всю статистику связанную с ним из бд
3033
"""
3134
pass
3235

@@ -58,23 +61,21 @@ def get_all_stat(connection=None) -> list[list]:
5861

5962

6063
@decorator_add_connection
61-
def get_personal_stat(telegram_id, connection=None) -> list[list]:
64+
def get_personal_stat(telegram_id: int, connection=None) -> list[list]:
6265
"""
6366
получить личную статистику пользователя
6467
"""
6568
return [[]]
6669

6770

6871
@decorator_add_connection
69-
def get_random_question(telegram_id, connection=None) -> tuple[int, str]:
72+
def get_random_question(telegram_id: int, connection=None) -> Question:
7073
"""
7174
получить случайный, неотвеченный вопрос из бд
7275
73-
return: tuple[int, str]
74-
int -- question.id
75-
str -- question.text
76+
return: Question
7677
"""
77-
return (0, "example")
78+
return Question(id=0, text="")
7879

7980

8081
@decorator_add_connection
@@ -86,7 +87,7 @@ def get_choices_by_question_id(question_id: int, connection=None) -> list[str]:
8687

8788

8889
@decorator_add_connection
89-
def add_user_vote_db(choice_id, telegram_id, connection=None):
90+
def add_user_vote_db(choice_id: int, telegram_id: int, connection=None):
9091
"""
9192
добавить голос пользователя выбранному варианту ответа
9293
"""

example/deco.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def wrapper():
55
print('g2')
66
return wrapper
77

8+
89
def h(func):
910
def wrapper():
1011
print('h1')
@@ -13,10 +14,13 @@ def wrapper():
1314
return wrapper
1415

1516
# @h
16-
# @g
17+
# @g
18+
19+
1720
def f():
1821
print('hello')
1922

23+
2024
f = h(g(f))
2125

22-
f()
26+
f()

example/handlers/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
def register_handlers():
2-
from . import add_question
3-
from . import delete_question
4-
from . import person_stats
5-
from . import all_stats
6-
from . import get_survey
1+
from . import (
2+
add_question,
3+
delete_question,
4+
person_stats,
5+
all_stats,
6+
get_survey,
7+
)
8+
9+
10+
def register_handlers(bot):
11+
add_question.register_handlers(bot)
12+
delete_question.register_handlers(bot)
13+
person_stats.register_handlers(bot)
14+
all_stats.register_handlers(bot)
15+
get_survey.register_handlers(bot)

example/handlers/add_question.py

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,56 @@
22
from telebot.handler_backends import StatesGroup, State
33
from datetime import datetime
44

5-
from db_adapter import *
6-
7-
from init_bot import bot
8-
5+
from db_adapter import (
6+
add_question_to_db,
7+
add_choice_to_db,
8+
)
99

1010

1111
class AdditionQuestion(StatesGroup):
1212
waiting_question = State()
1313
waiting_choices = State()
1414

15-
ADMIN_IDS = [68360888707]
16-
17-
def is_admin(func):
18-
def wrapper(*args, **kwargs):
19-
message = args[0]
20-
if message.from_user.id in ADMIN_IDS:
21-
func(*args, **kwargs)
22-
else:
23-
bot.reply_to(message, "Нет прав")
24-
return wrapper
25-
26-
@is_admin
27-
@bot.message_handler(commands=['create_question'])
28-
def start_addition_question(message: telebot.types.Message):
29-
if message.from_user.id in ADMIN_IDS:
15+
16+
ADMIN_IDS = [
17+
369937974, # @the_real_math
18+
]
19+
20+
logger = telebot.logger
21+
22+
23+
def register_handlers(bot):
24+
def is_admin(func):
25+
def wrapper(*args, **kwargs):
26+
message = args[0]
27+
if message.from_user.id in ADMIN_IDS:
28+
func(*args, **kwargs)
29+
else:
30+
bot.reply_to(message, "Нет прав")
31+
return wrapper
32+
33+
@bot.message_handler(commands=['create_question'])
34+
@is_admin
35+
def start_addition_question(message: telebot.types.Message):
3036
bot.reply_to(message, "Введите вопрос")
3137
bot.set_state(message.from_user.id, AdditionQuestion.waiting_question)
32-
else:
33-
bot.reply_to(message, "Нет прав")
34-
35-
36-
@is_admin
37-
@bot.message_handler(state=AdditionQuestion.waiting_question)
38-
def wait_question(message: telebot.types.Message):
39-
bot.reply_to(message, "Вопрос получен. Введите варианты ответа")
40-
question_text = message.text
41-
publish_date = datetime.now()
42-
inserted_question_id = add_question_to_db(question_text, publish_date)
43-
with bot.retrieve_data(message.from_user.id) as data:
44-
data['question_id'] = inserted_question_id
45-
bot.set_state(message.from_user.id, AdditionQuestion.waiting_choices)
46-
47-
48-
@is_admin
49-
@bot.message_handler(state=AdditionQuestion.waiting_choices)
50-
def wait_choice(message: telebot.types.Message):
51-
bot.reply_to(message, "Ответы получены. Сохранил")
52-
with bot.retrieve_data(message.from_user.id) as data:
53-
inserted_question_id = data['question_id']
54-
for x in message.text.split('\n'):
55-
add_choice_to_db(x, inserted_question_id)
38+
39+
@bot.message_handler(state=AdditionQuestion.waiting_question)
40+
@is_admin
41+
def wait_question(message: telebot.types.Message):
42+
bot.reply_to(message, "Вопрос получен. Введите варианты ответа")
43+
question_text = message.text
44+
publish_date = datetime.now()
45+
inserted_question_id = add_question_to_db(question_text, publish_date)
46+
with bot.retrieve_data(message.from_user.id) as data:
47+
data['question_id'] = inserted_question_id
48+
bot.set_state(message.from_user.id, AdditionQuestion.waiting_choices)
49+
50+
@bot.message_handler(state=AdditionQuestion.waiting_choices)
51+
@is_admin
52+
def wait_choice(message: telebot.types.Message):
53+
bot.reply_to(message, "Ответы получены. Сохранил")
54+
with bot.retrieve_data(message.from_user.id) as data:
55+
inserted_question_id = data['question_id']
56+
for x in message.text.split('\n'):
57+
add_choice_to_db(x, inserted_question_id)

example/handlers/all_stats.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import telebot.types
22

3-
from init_bot import bot
43

5-
6-
@bot.message_handler()
7-
def get_all_stats(message: telebot.types.Message):
8-
pass
4+
def register_handlers(bot):
5+
@bot.message_handler()
6+
def get_all_stats(message: telebot.types.Message):
7+
pass

example/handlers/custom_types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass(frozen=True, kw_only=True)
5+
class Question:
6+
id: int
7+
text: str
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import telebot.types
22

3-
from init_bot import bot
43

4+
def register_handlers(bot):
5+
@bot.message_handler()
6+
def start_delete_question(message: telebot.types.Message):
7+
pass
58

6-
@bot.message_handler()
7-
def start_delete_question(message: telebot.types.Message):
8-
pass
9-
10-
11-
@bot.message_handler()
12-
def process_user_answer(message: telebot.types.Message):
13-
pass
9+
@bot.message_handler()
10+
def process_user_answer(message: telebot.types.Message):
11+
pass

example/handlers/get_survey.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import telebot.types
22

3-
from init_bot import bot
43

4+
def register_handlers(bot):
5+
@bot.message_handler()
6+
def start_survey(message: telebot.types.Message):
7+
pass
58

6-
@bot.message_handler()
7-
def start_survey(message: telebot.types.Message):
8-
pass
9-
10-
11-
@bot.message_handler()
12-
def process_user_answer(message: telebot.types.Message):
13-
pass
9+
@bot.message_handler()
10+
def process_user_answer(message: telebot.types.Message):
11+
pass

example/handlers/person_stats.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import telebot.types
22

3-
from init_bot import bot
43

5-
6-
@bot.message_handler()
7-
def get_my_personal_stat(message: telebot.types.Message):
8-
pass
4+
def register_handlers(bot):
5+
@bot.message_handler()
6+
def get_my_personal_stat(message: telebot.types.Message):
7+
pass

0 commit comments

Comments
 (0)