Skip to content

Commit bda10c1

Browse files
web 2024-12-08
1 parent edbd3e4 commit bda10c1

File tree

4 files changed

+79
-15
lines changed

4 files changed

+79
-15
lines changed

example/db_adapter.py

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

33
import sqlalchemy
4+
from sqlalchemy import select, update
45
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session
56

6-
from handlers import custom_types
7+
# from handlers import custom_types
78

89
USERNAME = 'umschooluser'
910
PASSWORD = 'umschoolpswd'
@@ -129,32 +130,71 @@ def get_personal_stat(telegram_id: int, session: Session) -> list[list]:
129130

130131

131132
@decorator_add_session
132-
def get_random_question(telegram_id: int, session: Session) -> custom_types.Question:
133+
def get_random_question(telegram_id: int, session: Session):
133134
"""
134135
получить случайный, неотвеченный вопрос из бд
135136
136-
return: Question
137+
return: tuple
137138
"""
138-
return custom_types.Question(id=0, text="")
139+
available_ids = set()
140+
answered_ids = set()
141+
text = ""
142+
id_ = -1
143+
with session.begin():
144+
stmt = select(Question.id).where(1==1)
145+
for id_ in session.scalars(stmt):
146+
available_ids.add(id_)
147+
stmt = select(UserStat).where(UserStat.tg_user_id == telegram_id)
148+
for id_ in session.scalars(stmt):
149+
answered_ids.add(id_)
150+
id_ = list(available_ids - answered_ids)[0]
151+
stmt = select(Question.question_text).where(Question.id == id_)
152+
for txt in session.scalars(stmt):
153+
text = txt
154+
return (id_, text)
139155

140156

141157
@decorator_add_session
142158
def get_choices_by_question_id(question_id: int, session: Session) -> list[str]:
143159
"""
144160
получить ответы по заданному question_id
145161
"""
146-
return []
162+
choice_text = []
163+
with session.begin():
164+
stmt = select(Choice).where(Choice.question_id == question_id)
165+
for choice in session.scalars(stmt):
166+
choice_text.append(choice.choice_text)
167+
return choice_text
147168

148169

149170
@decorator_add_session
150171
def add_user_vote_db(choice_id: int, telegram_id: int, session: Session):
151172
"""
152173
добавить голос пользователя выбранному варианту ответа
153174
"""
154-
pass
175+
with session.begin():
176+
stmt = select(Choice).where(Choice.id == choice_id)
177+
result = session.execute(stmt)
178+
votes = 0
179+
for choice in result.scalars():
180+
print(choice)
181+
votes = choice.votes
182+
183+
new_votes = votes + 1
184+
stmt = (
185+
update(Choice).
186+
where(Choice.id == choice_id).
187+
values(votes=new_votes)
188+
)
189+
print(stmt, new_votes, choice_id)
190+
session.execute(stmt)
191+
session.commit()
192+
155193

156194

157195
if __name__ == "__main__":
158-
create_tables_in_db()
159-
add_question_to_db("test-question", datetime.now())
160-
add_choice_to_db("test-choice", 20)
196+
print(get_random_question(1))
197+
print(get_choices_by_question_id(1))
198+
# create_tables_in_db()
199+
# add_question_to_db("test-question", datetime.now())
200+
# add_choice_to_db("test-choice", 20)

example/handlers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def register_handlers(bot):
1111
add_question.register_handlers(bot)
12+
get_survey.register_handlers(bot)
1213
delete_question.register_handlers(bot)
1314
person_stats.register_handlers(bot)
1415
all_stats.register_handlers(bot)
15-
get_survey.register_handlers(bot)

example/handlers/get_survey.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import telebot.types
2+
from telebot.handler_backends import StatesGroup, State
23

34

5+
from db_adapter import (
6+
get_random_question,
7+
get_choices_by_question_id,
8+
add_user_vote_db,
9+
)
10+
11+
class AnswerQuestion(StatesGroup):
12+
waiting_answer = State()
13+
14+
logger = telebot.logger
15+
416
def register_handlers(bot):
5-
@bot.message_handler()
17+
@bot.message_handler(commands=['get_new_question'])
618
def start_survey(message: telebot.types.Message):
7-
pass
19+
# а что на все вопросы?
20+
question = get_random_question(message.from_user.id)
21+
choices = get_choices_by_question_id(question[0])
22+
bot.set_state(message.from_user.id, AnswerQuestion.waiting_answer)
23+
bot.send_message(message.from_user.id, question[1])
24+
bot.send_message(
25+
message.from_user.id,
26+
'Варианты ответа:\n'+ '\n'.join(choices),
27+
)
828

9-
@bot.message_handler()
29+
@bot.message_handler(state=AnswerQuestion.waiting_answer)
1030
def process_user_answer(message: telebot.types.Message):
11-
pass
31+
print(message.text)
32+
user_answer = int(message.text) # посмотреть
33+
add_user_vote_db(user_answer, message.from_user.id)
34+
bot.send_message(message.from_user.id, "Записал :)")
35+

example/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from init_bot import create_bot
99

1010

11-
telebot.logger.setLevel(logging.DEBUG) # Outputs messages to console.
11+
# telebot.logger.setLevel(logging.DEBUG) # Outputs messages to console.
1212

1313
if __name__ == "__main__":
1414
create_tables_in_db()

0 commit comments

Comments
 (0)