def start_update(self): collection = CategoryModel.COLLECTION new_categories: List[CategoryModel] = [] raw_categories_old_structure: List[ dict] = self.database_manager.find_all(collection=collection, filter={}) for idx, old_raw_category in enumerate(raw_categories_old_structure): new_categories.append( self.__convert_category_to_new_structure(old_raw_category, index=idx)) """qb = QueryBuilder() qb.query = QueryBuilder.and_( [QueryBuilder.gt_('parent_id', 0), {'parent_id': QueryBuilder.not_(QueryBuilder.type_(10))}]) raw_sub_categories_old_structure: List[dict] = self.database_manager.find_all(collection=collection, filter=qb.query)""" self.database_manager.delete_collection( collection=CategoryModel.COLLECTION) self.database_manager.create_collection(CategoryModel.COLLECTION) self.database_manager.create_indexes(CategoryModel.COLLECTION, CategoryModel.get_index_keys()) for category in new_categories: try: self.object_manager.insert_category(category) except ObjectManagerInsertError: continue self.__clear_up_types() super(Update20200512, self).increase_updater_version(20200512)
def tree(self) -> CategoryTree: # Currently only a work around until the other managers were converted to the new format - MH types = CmdbObjectManager( database_manager=self._database_manager).get_all_types() categories = [ CategoryModel.from_data(category) for category in super(CategoryManager, self).get_many({}) ] return CategoryTree(categories=categories, types=types)
def get(self, public_id: Union[PublicID, int]) -> CategoryModel: """ Get a single type by its id. Args: public_id (int): ID of the type. Returns: CategoryModel: Instance of CategoryModel with data. """ cursor_result = self._get(self.collection, filter={'public_id': public_id}, limit=1) for resource_result in cursor_result.limit(-1): return CategoryModel.from_data(resource_result) raise ManagerGetError(f'Category with ID: {public_id} not found!')
def insert(self, category: dict) -> PublicID: """ Insert a single category into the system. Args: category (dict): Raw data of the category. 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 category """ if isinstance(category, CategoryModel): category = CategoryModel.to_json(category) return self._insert(self.collection, resource=category)
def __convert_category_to_new_structure(self, old_raw_category: dict, index: int) -> CategoryModel: """Converts a category from old < 20200512 structure to new format """ old_raw_category['meta'] = { 'icon': old_raw_category.get('icon', None), 'order': index } parent = old_raw_category.get('parent_id', None) if parent == 0: parent = None old_raw_category['parent'] = parent category = CategoryModel.from_data(old_raw_category) category.types = self.__get_types_in_category( old_raw_category.get('public_id')) return category
def update(self, public_id: Union[PublicID, int], category: Union[CategoryModel, dict]): """ Update a existing category in the system. Args: public_id (int): PublicID of the category in the system. category: New category data Notes: If a CategoryModel instance was passed as type argument, \ it will be auto converted via the model `to_json` method. """ if isinstance(category, CategoryModel): category = CategoryModel.to_json(category) update_result = self._update(self.collection, filter={'public_id': public_id}, resource=category) if update_result.matched_count != 1: raise ManagerUpdateError(f'Something happened during the update!') return update_result
def get(self, public_id: Union[PublicID, int]) -> CategoryModel: result = super(CategoryManager, self).get(public_id=public_id) return CategoryModel.from_data(result)