예제 #1
0
def delete_category(public_id: int):
    """
    HTTP `DELETE` route for delete a single category resource.

    Args:
        public_id (int): Public ID of the deletable category

    Raises:
        ManagerGetError: When the category with the `public_id` was not found.
        ManagerDeleteError: When something went wrong during the deletion.

    Returns:
        DeleteSingleResponse: Delete result with the deleted category as data.
    """
    category_manager: CategoryManager = CategoryManager(
        database_manager=current_app.database_manager)
    try:
        deleted_category = category_manager.delete(
            public_id=PublicID(public_id))
        api_response = DeleteSingleResponse(
            raw=CategoryModel.to_json(deleted_category),
            model=CategoryModel.MODEL)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerDeleteError as err:
        return abort(404, err.message)
    return api_response.make_response()
예제 #2
0
def update_category(public_id: int, data: dict):
    """
    HTTP `PUT`/`PATCH` route for update a single category resource.

    Args:
        public_id (int): Public ID of the updatable category
        data (CategoryModel.SCHEMA): New category data to update

    Raises:
        ManagerGetError: When the category with the `public_id` was not found.
        ManagerUpdateError: When something went wrong during the update.

    Returns:
        UpdateSingleResponse: With update result of the new updated category.
    """
    category_manager: CategoryManager = CategoryManager(
        database_manager=current_app.database_manager)
    try:
        category = CategoryModel.from_data(data=data)
        category_manager.update(public_id=PublicID(public_id),
                                category=CategoryModel.to_json(category))
        api_response = UpdateSingleResponse(result=data,
                                            url=request.url,
                                            model=CategoryModel.MODEL)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerUpdateError as err:
        return abort(400, err.message)

    return api_response.make_response()
예제 #3
0
def get_category(public_id: int):
    """
    HTTP `GET`/`HEAD` route for a single category resource.

    Args:
        public_id (int): Public ID of the category.

    Raises:
        ManagerGetError: When the selected category does not exists.

    Returns:
        GetSingleResponse: Which includes the json data of a CategoryModel.
    """
    category_manager: CategoryManager = CategoryManager(
        database_manager=current_app.database_manager)
    body = True if not request.method != 'HEAD' else False

    try:
        category_instance = category_manager.get(public_id)
    except ManagerGetError as err:
        return abort(404, err.message)
    api_response = GetSingleResponse(CategoryModel.to_json(category_instance),
                                     url=request.url,
                                     model=CategoryModel.MODEL,
                                     body=body)
    return api_response.make_response()
예제 #4
0
def insert_category(data: dict):
    """
    HTTP `POST` route for insert a single category resource.

    Args:
        data (CategoryModel.SCHEMA): Insert data of a new category.

    Raises:
        ManagerGetError: If the inserted resource could not be found after inserting.
        ManagerInsertError: If something went wrong during insertion.

    Returns:
        InsertSingleResponse: Insert response with the new category and its public_id.
    """
    category_manager: CategoryManager = CategoryManager(
        database_manager=current_app.database_manager)
    data.setdefault('creation_time', datetime.utcnow())
    try:
        result_id: PublicID = category_manager.insert(data)
        raw_doc = category_manager.get(public_id=result_id)
    except ManagerGetError as err:
        return abort(404, err.message)
    except ManagerInsertError as err:
        return abort(400, err.message)
    api_response = InsertSingleResponse(result_id,
                                        raw=CategoryModel.to_json(raw_doc),
                                        url=request.url,
                                        model=CategoryModel.MODEL)
    return api_response.make_response(prefix='categories')
예제 #5
0
 def __init__(self):
     scr = SystemConfigReader()
     self.database_manager = DatabaseManagerMongo(
         **scr.get_all_values_from_section('Database'))
     self.object_manager = CmdbObjectManager(
         database_manager=self.database_manager)
     self.category_manager = CategoryManager(
         database_manager=self.database_manager)
     self.type_manager = TypeManager(database_manager=self.database_manager)
     self.log_manager = ExportdLogManager(
         database_manager=self.database_manager)
     super().__init__(self.database_manager)