Beispiel #1
0
 def get_user_by(self, **requirements) -> UserModel:
     """Get user by requirement"""
     try:
         return UserModel.from_data(
             self._get_by(collection=UserModel.COLLECTION, **requirements))
     except NoDocumentFound:
         raise UserManagerGetError(f'UserModel not found')
Beispiel #2
0
 def get_users(self) -> List[UserModel]:
     """Get all users"""
     user_list = []
     for founded_user in self._get_many(collection=UserModel.COLLECTION):
         try:
             user_list.append(UserModel.from_data(founded_user))
         except CMDBError:
             continue
     return user_list
Beispiel #3
0
 def get_users_by(self, sort='public_id', **requirements) -> List[UserModel]:
     """Get a list of users by requirement"""
     user_list = []
     users_in_database = self._get_many(collection=UserModel.COLLECTION, sort=sort, **requirements)
     for user in users_in_database:
         try:
             user_ = UserModel.from_data(user)
         except CMDBError as err:
             LOGGER.error(f'[UserManager] Error while inserting database user into return list: {err}')
             continue
         user_list.append(user_)
     return user_list
Beispiel #4
0
    def get_many(self, query: Query = None) -> List[UserModel]:
        """
        Get a collection of users by a query. Passing no query means all users.

        Args:
            query (Query): A database query for filtering.

        Returns:
            List[UserModel]: A list of all users which matches the query.
        """
        query = query or {}
        results = self._get(self.collection, filter=query)
        return [UserModel.from_data(user) for user in results]
Beispiel #5
0
    def get_by(self, query: Query) -> UserModel:
        """
        Get a single user by a query.

        Args:
            query (Query): Query filter of user parameters.

        Returns:
            UserModel: Instance of UserModel with matching data.
        """
        result = self._get(self.collection, filter=query, limit=1)
        for resource_result in result.limit(-1):
            return UserModel.from_data(resource_result)
        raise ManagerGetError(f'User with the query: {query} not found!')
Beispiel #6
0
    def get(self, public_id: Union[PublicID, int]) -> UserModel:
        """
        Get a single user by its id.

        Args:
            public_id (int): ID of the user.

        Returns:
            UserModel: Instance of UserModel with data.
        """
        result = self._get(self.collection, filter={'public_id': public_id}, limit=1)
        for resource_result in result.limit(-1):
            return UserModel.from_data(resource_result)
        raise ManagerGetError(f'User with ID: {public_id} not found!')