def set_db_user(bot_instance, message): db_user = UserRepository.get_by_id(message.from_user.id) if not db_user: UserRepository.insert(User(message.from_user)) message.db_user = UserRepository.get_by_id(message.from_user.id) else: message.db_user = db_user
def create_room(message): if RoomRepository.get_by_id(message.text): bot.reply_to(message, 'This name is already used') else: message.db_user.tmp_room = Room(message.text, message.chat.id) UserRepository.set_state(message.db_user.id, CreateRoomBotState.GET_ROOM_DESCRIPTION) bot.reply_to(message, 'Name saved, now add some description')
def create_description(message): message.db_user.tmp_room.description = message.text print(message.db_user.tmp_room) RoomRepository.insert(message.db_user.tmp_room) UserRepository.set_state(message.db_user.id, CommonBotState.DEFAULT_STATE) bot.send_message(message.chat.id, f'Saved room:\n, {message.db_user.tmp_room.id}\n', reply_markup=main_keyboard_markup)
from os import getenv from base64 import b64decode from datetime import timedelta from fastapi import APIRouter, Depends from api.middlewares import basic_auth from fastapi.encoders import jsonable_encoder from dependencies import database, hash_password from starlette.responses import RedirectResponse from db.repositories import User as UserRepository from fastapi.security import OAuth2PasswordRequestForm from api.security import BasicAuth, Token, create_access_token from exceptions import UnauthorizedException, ExceptionScheme, RedirectScheme router = APIRouter() repo = UserRepository(next(database())) @router.post("/token", responses={ 200: { "model": Token }, 401: { "model": ExceptionScheme } }) def receive_token(form_data: OAuth2PasswordRequestForm = Depends()) -> Token: user = repo.login(form_data.username, hash_password(form_data.password)) if user is None: raise UnauthorizedException(detail="Incorrect username or password")
def check_state(message, state): db_user = message.db_user if db_user: curr_state = UserRepository.get_state(db_user.id) return curr_state == state return false
def create_new_room(message): UserRepository.set_state(message.db_user.id, CreateRoomBotState.GET_ROOM_NAME) bot.send_message(message.chat.id, 'New room:', reply_markup=remove_keyboard)