def update_messages_stats(session: DBSession, *, user_id: int = None, login: str = None, role: str, context: Context) -> DBUser: db_user = None if user_id is not None: db_user = session.get_user_by_id(user_id) elif login is not None: db_user = session.get_user_by_login(login) # to pretend data race while context.is_locked: time.sleep(0.05) context.lock() if role == 'sender': db_user.sent_messages += 1 else: db_user.received_messages += 1 context.unlock() return db_user
def get_uid_by_login(session: DBSession, login: str) -> int: uid = None uid = session.get_user_by_login(login) if uid is None: raise DBUserNotExistsException return uid.id
def get_user(session: DBSession, *, user_id: int = None, login: str = None) -> DBUser: db_user = None if login is not None: db_user = session.get_user_by_login(login) elif user_id is not None: db_user = session.get_user_by_id(user_id) if db_user is None: raise DBUserNotExistsException return db_user
def create_user(session: DBSession, user: RequestCreateUserDto) -> DBUser: new_user = DBUser( login=user.login, password=user.password, ) if session.get_user_by_login(new_user.login) is not None: raise UserExistException session.add_model(new_user) return new_user
def create_user(session: DBSession, user: RequestCreateUserDto, hashed_password: bytes) -> DBUser: new_user = DBUser( first_name=user.first_name, last_name=user.last_name, login=user.login, password=hashed_password, ) if session.get_user_by_login(new_user.login) is not None: raise DBLoginExistsException session.add_model(new_user) return new_user
def create_message(session: DBSession, message: RequestCreateMessageDto, sender_id: int) -> DBMessage: # Поиск по login в users recipient = session.get_user_by_login(message.recipient) if recipient is None: raise DBUserNotExistsException new_message = DBMessage(sender_id=sender_id, recipient_id=recipient.id, message=message.message) session.add_model(new_message) return new_message
def change_login(session: DBSession, new_login: str, user_id: int) -> DBUser: db_user_same_login = session.get_user_by_login(new_login) if db_user_same_login is not None: raise DBUserAlreadyExistsException db_user = session.get_user_by_id(user_id) if db_user.is_deleted: raise DBUserDeletedException # если исключение не зарейзилось, значит пользователь с таким логином отсутствует db_user.login = new_login return db_user
def create_message(session: DBSession, message: RequestCreateMessageDto, uid: int) -> DBMessage: recipient = session.get_user_by_login(login=message.recipient, add_filter='users.is_delete = False') if recipient is None: raise DBUserNotExistsException new_message = DBMessage( sender_id=uid, message=message.message, recipient_id=recipient.id, ) session.add_model(new_message) return new_message
def patch_user(session: DBSession, user: RequestPatchUserDto, uid: int, *, hashed_password: bytes = None) -> DBUser: db_user = session.get_user_by_id(uid) if hasattr(user, 'login'): if session.get_user_by_login(user.login) is not None: raise DBLoginExistsException for attr in user.fields: if attr == 'password' and hashed_password is not None: setattr(db_user, attr, hashed_password) elif hasattr(user, attr): setattr(db_user, attr, getattr(user, attr)) return db_user
def get_user(session: DBSession, login: str = None, user_id: int = None, undelete: bool = False) -> DBUser: db_user = None if login is not None: db_user = session.get_user_by_login(login) elif user_id is not None: db_user = session.get_user_by_id(user_id) if db_user is None: raise DBUserNotFoundException if db_user.is_deleted is True: if not undelete: raise DBUserDeletedException return db_user
def create_user(session: DBSession, user: RequestCreateUserDto, hashed_password: bytes, secret_word: bytes) -> DBUser: # создание модели DBUser new_user = DBUser( login=user.login, password=hashed_password, # записываем в базу хэшированный пароль first_name=user.first_name, last_name=user.last_name, secret_word=secret_word, ) # сначала попробуем получить пользователя по login перед созданием записи в БД # если не None, то получается, что пользователь с таким логином уже есть в БД -> рейзим исключение if session.get_user_by_login(new_user.login) is not None: raise DBUserAlreadyExistsException # добавляем модель в базу данных session.add_model(new_user) return new_user
def restore_user(login: str, session: DBSession): db_user = session.get_user_by_login(login) db_user.is_deleted = False return db_user