def update(self,
            old_name: str,
            *,
            new_name: str = None,
            new_password: str = None,
            new_identity: str = None):
     logging.info(
         f'prepare to update user {old_name}, new information {new_name, new_password, new_identity}'
     )
     if all(map(lambda x: x is None,
                [new_name, new_password, new_identity])):
         return
     session = Session()
     query = session.query(Users).filter(Users.name == old_name)
     try:
         user = query.one()  # NoResultFound
         self.update_name(user, new_name)
         self.update_password(user, new_password)
         self.update_identity(user, new_identity)
         session.commit()
         logging.info(f'old user {old_name} has been updated')
     except NoResultFound:
         logging.info(f'user {old_name} does not exist')
         raise UserDoesNotExistError(f'用户 {old_name} 不存在')
     except IntegrityError as e:
         logging.info(
             f'database IntegrityError, error code {e.orig.args[0]}')
         session.rollback()
         if e.orig.args[0] == 1452:
             raise IdentityDoesNotExistError(f'新用户身份 {new_identity} 不存在')
         else:
             assert e.orig.args[0] == 1062
             raise UserExistError(f'新用户名 {new_name} 已存在,无法进行更新')
     finally:
         session.close()
 def delete_single(self, name: str):
     logging.info(f'user {name} prepare to be deleted')
     session = Session()
     query = session.query(Users).filter(Users.name == name)
     try:
         user = query.one()
         session.delete(user)
         session.commit()
         logging.info(f'user {name} has been deleted')
     except NoResultFound:
         session.rollback()
         logging.info(f'fail to delete user {name}')
         raise UserDoesNotExistError(f'用户 {name} 不存在')
     finally:
         session.close()
Beispiel #3
0
 def modify_password(self, new_password: str):
     logging.info(f'prepare update user password to {new_password}')
     session = Session()
     query = session.query(Users).filter(Users.id == self.user.id)
     try:
         u = query.one()
         hex_password = sha1_encrypt(new_password)
         u.password = hex_password
         session.commit()
         # 事务提交后要更新下当前的用户密码
         self.user.password = hex_password
     except NoResultFound:
         raise UserDoesNotExistError(f'用户不存在, 当前用户 {self.user} 已失效!')
     finally:
         session.close()
     logging.info(f'user password been changed to {new_password}')
 def delete_set(self, name_set: set):
     # 避免对容器类型数据直接操作
     internal_name_set = copy.copy(name_set)
     logging.info(f'these users {internal_name_set} prepare to be deleted')
     session = Session()
     query = session.query(Users).filter(Users.name.in_(internal_name_set))
     try:
         users = query.all()
         for user in users:
             internal_name_set.remove(user.name)
             session.delete(user)
         if len(internal_name_set) != 0:
             session.rollback()
             logging.info(f'these users {internal_name_set} do not exist, transaction has rolled back')
             raise UserDoesNotExistError(f'这些用户不存在 {internal_name_set}')
         session.commit()
         logging.info(f'all users have been deleted')
     finally:
         # 关闭会话
         session.close()
    def register_user(self, name: str, password: str, identity: str) -> bool:
        # 会话工厂生产一个具体会话
        session = Session()
        sha1_password = sha1_encrypt(password)
        new_user = Users(name=name,
                         password=sha1_password,
                         remote_identity=identity)
        result = False
        try:
            # 添加记录
            session.add(new_user)
            # 提交事务
            session.commit()
            result = True
        except Exception as e:
            session.rollback()
            logging.info(f'add user {name} failure -> {e}')
        finally:
            # 关闭会话
            session.close()

        return result
Beispiel #6
0
    def modify_name(self, new_name: str):
        logging.info(
            f'prepare update user name from {self.user.name} to {new_name}')
        session = Session()
        query_new_name = session.query(Users).filter(Users.name == new_name)

        # 如果用户名已经存在,引起错误
        if query_new_name.one_or_none():
            raise UserExistError(f'用户名 {new_name} 已存在')

        # 用户名不存在,则更新用户名
        query = session.query(Users).filter(Users.id == self.user.id)
        try:
            u = query.one()
            u.name = new_name
            session.commit()
            # 事务提交后要更新下当前的用户名
            self.user.name = new_name
        except NoResultFound:
            raise UserDoesNotExistError(f'用户不存在, 当前用户 {self.user} 已失效!')
        finally:
            session.close()
        logging.info(f'user name been changed to {new_name}')