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!')
Beispiel #7
0
    def insert(self, user: Union[UserModel, dict]) -> PublicID:
        """
        Insert a single user into the system.

        Args:
            user (dict): Raw data of the user.

        Notes:
            If no public id was given, the database manager will auto insert the next available ID.

        Returns:
            int: The Public ID of the new inserted user
        """
        if isinstance(user, UserModel):
            user = UserModel.to_data(user)
        return self._insert(self.collection, resource=user)
Beispiel #8
0
    def update(self, public_id: Union[PublicID, int], user: Union[UserModel, dict]):
        """
        Update a existing user in the system.

        Args:
            public_id (int): PublicID of the user in the system.
            user(UserModel): Instance or dict of UserModel

        Notes:
            If a UserModel instance was passed as user argument, \
            it will be auto converted via the model `to_dict` method.
        """
        if isinstance(user, UserModel):
            user = UserModel.to_dict(user)
        update_result = self._update(collection=self.collection, filter={'public_id': public_id}, resource=user)

        if update_result.matched_count != 1:
            raise ManagerUpdateError(f'Something happened during the update!')
        return update_result