def upsert_records(records: list[object]) -> None: session = db_session(expire_on_commit=False) for record in records: session.add(record) session.commit() session.expunge_all() session.close()
def query_list_item(command: str, list_name: str) -> Item: list_name = list_name session = db_session() if command.isdigit(): item_index = int(command) - 1 item = session.query(Item).filter( Item.item_list == list_name).order_by(Item.id).all()[item_index] else: item = session.query(Item).filter(Item.item_list == list_name, Item.item_name == command).first() session.close() return item
def set_next_episode(update: telegram.Update, context: telegram.ext.CallbackContext): episode_date_str = update.message.text next_episode = datetime.fromisoformat(date_string=episode_date_str) session = db_session() item_list = session.query(ItemList).filter( ItemList.list_name == 'Pleepapier').first() item_list.episode_date = next_episode upsert_records(item_list) context.bot.send_message( chat_id=update.effective_chat.id, text=f'volgende aflevering is op {episode_date_str}') session.close()
def get_kratjes(update: telegram.Update, context: telegram.ext.CallbackContext): session = db_session() kratjes = session.query(Kratjes).order_by(Kratjes.created_date).all() message_text = '' for bet in kratjes: message_text += f'{bet.better} stelt: ' bet.due_date = bet.due_date.strftime( '%d-%m-%Y') if bet.due_date else 'het einde van dit leven' message_text += '"{1}" voor "{2}" met "{0}" als onderpand\n\n'.format( bet.stake, bet.bet_description, bet.due_date) context.bot.send_message(chat_id=update.effective_chat.id, text=message_text) session.close()
def pleepapier(update: telegram.Update, context: telegram.ext.CallbackContext): list_name = 'Pleepapier' session = db_session() item_list = session.query(Item).filter( Item.item_list == list_name).order_by(Item.id).all() pleepapier_string = 'Pleepapier:\n' items = [ str(index + 1) + '. ' + item.item_name + ' (' + item.created_by + ')\n' for index, item in enumerate(item_list) ] pleepapier_string = pleepapier_string + ''.join(items) context.bot.send_message(chat_id=update.effective_chat.id, text=pleepapier_string) session.close()
def new(update: telegram.Update, context: telegram.ext.CallbackContext): command = process_input(update.message.text) list_name = command.value response = '' try: session = db_session() existing_list = session.query(ItemList).filter( ItemList.list_name == list_name).all() if len(existing_list) > 0: response += 'Gast "' + list_name + '" bestaat al...' else: item_list = ItemList(list_name=list_name, created_by=update.effective_user.first_name) session.add(item_list) session.commit() response += '"' + list_name + '" is aangemaakt!' session.close() except ValueError: response += 'Lullo, je moet wel wat invullen he' context.bot.send_message(chat_id=update.effective_chat.id, text=response)
def get_random_insult(victim: str) -> str: session = db_session() insult = session.query(Insult).order_by(random()).first().insult insult = victim + ' ' + insult return insult.capitalize()
def delete_records(records: list[object]) -> None: session = db_session() for record in records: session.delete(record) session.commit() session.close()