def make_text_list_categories(session, telegram_user_id): user = User.get_user_by_telegram_user_id(session, telegram_user_id) categories_text = CategoryEarning.get_all_categories_by_text( session=session, user_id=user.id) result = '' for index, text in enumerate(categories_text): result += f'{index+1}. {text}\n' return result
def test_add_category_in_db(self): add_example_user(session) new_category = 'Стипендия' expected = CategoryEarning(id=1, category=new_category, user_id=1) EarningCategoryManager.add_category_in_db( session, new_category, example_user['telegram_user_id']) answer = session.query(CategoryEarning).get(1) self.assertEqual(answer, expected)
def test_get_all_categories(self): add_example_user(session) add_example_category_earning(session) expected = [ CategoryEarning(id=1, user_id=example_user['id'], category=example_category_earning['category']) ] answer = session.query(CategoryEarning).all() self.assertEqual(answer, expected)
def send_message_to_choose_category(telegram_user_id, amount_money): with session_scope() as session: user = session.query(User).filter(User.telegram_user_id == telegram_user_id).first() buttons = make_buttons_for_choose_category(count_buttons_per_row=config['buttons_per_row'], categories=CategoryEarning.get_all_categories(session, user.id)) buttons = add_buttons_exit_and_back(buttons) keyboard = ReplyKeyboardMarkup(buttons, resize_keyboard=True) bot.send_message(chat_id=telegram_user_id, text=text_to_choose_category(amount_money), parse_mode=telegram.ParseMode.HTML, reply_markup=keyboard)
def handler_to_choose_category(update: Update, context: CallbackContext): text = update.message.text with session_scope() as session: user = session.query(User).filter(User.telegram_user_id == update.message.from_user.id).first() categories = CategoryEarning.get_all_categories_by_text(session, user.id) if text in categories: with session_scope() as session: add_earning_in_db(session, update.message.from_user.id, context.user_data['amount_money_earning'], text) bot.send_message(chat_id=update.message.from_user.id, text=text_success_add_earning(), reply_markup=ReplyKeyboardRemove()) return ConversationHandler.END
def get_all_categories_by_text(cls, session, telegram_user_id): user = session.query(User).filter( User.telegram_user_id == telegram_user_id).first() return CategoryEarning.get_all_categories_by_text(session, user.id)
def add_category_in_db(session, new_category, telegram_user_id): user = User.get_user_by_telegram_user_id(session, telegram_user_id) CategoryEarning.add_category(session, user.id, new_category)
def test_query_category_earning(self): add_example_category_earning(session) expected = [CategoryEarning(id=1, category='Работа')] category_earning = session.query(CategoryEarning).all() self.assertEqual(category_earning, expected)
def get_categories_by_text(self, session): return CategoryEarning.get_all_categories_by_text( session, self.user.id)
def get_keyboard_choose_categories(self, session): categories = CategoryEarning.get_all_categories(session, self.user.id) buttons = make_buttons_for_choose_category(config['buttons_per_row'], categories) return ReplyKeyboardMarkup(add_buttons_exit_and_back(buttons), resize_keyboard=True)
def create_default_categories_for_earning_and_consumption(telegram_user_id): with session_scope() as session: user = session.query(User).filter(User.telegram_user_id == telegram_user_id).first() CategoryEarning.create_default_categories(session, user.id) CategoryConsumption.create_default_categories(session, user.id)
def add_example_category_earning(session): session.add( CategoryEarning(category=example_category_earning['category'], user_id=example_category_earning['user_id'])) session.commit()