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 patch_user(session: DBSession, user: RequestPatchUserDto, user_id: int) -> DBUser: db_user = session.get_user_by_id(user_id) for attr in user.fields: value = getattr(user, attr) setattr(db_user, attr, value) return db_user
def delete_user(session: DBSession, user_id: int) -> DBUser: db_user = session.get_user_by_id(user_id) try: db_user.is_delete = True except AttributeError: raise DBUserNotExistsException return db_user
def change_password(session: DBSession, hashed_password: bytes, user_id: int) -> DBUser: db_user = session.get_user_by_id(user_id) if db_user.is_deleted: raise DBUserDeletedException db_user.password = hashed_password return db_user
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 patch_user(session: DBSession, user: RequestPatchUserDto, user_id: int) -> DBUser: db_user = session.get_user_by_id(user_id) if db_user is None: raise DBUserNotExistsException for attr in user.fields: setattr(db_user, attr, getattr(user, attr)) return db_user
def patch_user(session: DBSession, user: RequestPatchUserDto, user_id: int) -> DBUser: db_user = session.get_user_by_id(user_id) # attrs = ('first_name', 'last_name') # for attr in attrs: for attr in user.fields: if hasattr(user, attr): value = getattr(user, attr) setattr(db_user, attr, value) return db_user
def delete_user(session: DBSession, user_id: int) -> DBUser: db_user = session.get_user_by_id(user_id) if db_user is None: raise DBUserNotFoundException if db_user.is_deleted is False: db_user.is_deleted = True else: raise DBUserDeletedException return db_user
def create_message(session: DBSession, message: RequestCreateMessageDto, sender: int) -> DBMessage: if session.get_user_by_id(uid=message.recipient_id) is None: raise DBUserNotExistsException new_message = DBMessage( recipient_id=message.recipient_id, message=message.message, sender_id=sender, ) 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 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 patch_user(session: DBSession, user: RequestPatchUserDto, user_id: int = None) -> DBUser: db_user = None if 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: raise DBUserDeletedException # атрибуты, которые хотим изменить # attrs = ('first_name', 'last_name') # for attr in attrs: for attr in user.fields: if hasattr(user, attr): setattr(db_user, attr, getattr(user, attr)) return db_user
def delete_user(session: DBSession, user_id: int) -> DBUser: db_user = session.get_user_by_id(user_id) db_user.is_delete = True return db_user