def unassociate_by_criteria(repo_id, criteria, notify_plugins=True): """ Unassociate units that are matched by the given criteria. :param repo_id: identifies the repo :type repo_id: str :param criteria: :param notify_plugins: if true, relevant plugins will be informed of the removal :type notify_plugins: bool """ criteria = UnitAssociationCriteria.from_dict(criteria) association_query_manager = manager_factory.repo_unit_association_query_manager( ) unassociate_units = association_query_manager.get_units( repo_id, criteria=criteria) if len(unassociate_units) == 0: return {} unit_map = {} # maps unit_type_id to a list of unit_ids for unit in unassociate_units: id_list = unit_map.setdefault(unit['unit_type_id'], []) id_list.append(unit['unit_id']) collection = RepoContentUnit.get_collection() for unit_type_id, unit_ids in unit_map.items(): spec = { 'repo_id': repo_id, 'unit_type_id': unit_type_id, 'unit_id': { '$in': unit_ids } } collection.remove(spec) unique_count = sum( 1 for unit_id in unit_ids if not RepoUnitAssociationManager.association_exists( repo_id, unit_id, unit_type_id)) if not unique_count: continue repo_controller.update_unit_count(repo_id, unit_type_id, -unique_count) repo_controller.update_last_unit_removed(repo_id) # Convert the units into transfer units. This happens regardless of whether or not # the plugin will be notified as it's used to generate the return result, transfer_units = create_transfer_units(unassociate_units) if notify_plugins: remove_from_importer(repo_id, transfer_units) # Match the return type/format as copy serializable_units = [u.to_id_dict() for u in transfer_units] return {'units_successful': serializable_units}
def unassociate_by_criteria(cls, repo_id, criteria, notify_plugins=True): """ Unassociate units that are matched by the given criteria. :param repo_id: identifies the repo :type repo_id: str :param criteria: :param notify_plugins: if true, relevant plugins will be informed of the removal :type notify_plugins: bool """ criteria = UnitAssociationCriteria.from_dict(criteria) repo = model.Repository.objects.get_repo_or_missing_resource(repo_id) unassociate_units = load_associated_units(repo_id, criteria) if len(unassociate_units) == 0: return {} # Convert the units into transfer units. This happens regardless of whether or not # the plugin will be notified as it's used to generate the return result. # If all source types have been converted to mongo, search via new style. repo_unit_types = set(repo.content_unit_counts.keys()) if repo_unit_types.issubset(set(plugin_api.list_unit_models())): transfer_units = list(cls._units_from_criteria(repo, criteria)) else: transfer_units = None if unassociate_units is not None: transfer_units = list(create_transfer_units(unassociate_units)) if notify_plugins: remove_from_importer(repo_id, transfer_units) unit_map = {} # maps unit_type_id to a list of unit_ids for unit in unassociate_units: id_list = unit_map.setdefault(unit["unit_type_id"], []) id_list.append(unit["unit_id"]) collection = RepoContentUnit.get_collection() for unit_type_id, unit_ids in unit_map.items(): spec = {"repo_id": repo_id, "unit_type_id": unit_type_id, "unit_id": {"$in": unit_ids}} collection.remove(spec) unique_count = sum( 1 for unit_id in unit_ids if not RepoUnitAssociationManager.association_exists(repo_id, unit_id, unit_type_id) ) if not unique_count: continue repo_controller.update_unit_count(repo_id, unit_type_id, -unique_count) repo_controller.update_last_unit_removed(repo_id) # Match the return type/format as copy serializable_units = [u.to_id_dict() for u in transfer_units] return {"units_successful": serializable_units}
def unassociate_by_criteria(repo_id, criteria, notify_plugins=True): """ Unassociate units that are matched by the given criteria. :param repo_id: identifies the repo :type repo_id: str :param criteria: :param notify_plugins: if true, relevant plugins will be informed of the removal :type notify_plugins: bool """ criteria = UnitAssociationCriteria.from_dict(criteria) repo = model.Repository.objects.get_repo_or_missing_resource(repo_id) unassociate_units = load_associated_units(repo_id, criteria) if len(unassociate_units) == 0: return {} # Convert the units into transfer units. This happens regardless of whether or not # the plugin will be notified as it's used to generate the return result. # If all source types have been converted to mongo, search via new style. repo_unit_types = set(repo.content_unit_counts.keys()) if repo_unit_types.issubset(set(plugin_api.list_unit_models())): transfer_units = list( RepoUnitAssociationManager._units_from_criteria( repo, criteria)) else: transfer_units = None if unassociate_units is not None: transfer_units = list(create_transfer_units(unassociate_units)) if notify_plugins: remove_from_importer(repo_id, transfer_units) unit_map = {} # maps unit_type_id to a list of unit_ids for unit in unassociate_units: id_list = unit_map.setdefault(unit['unit_type_id'], []) id_list.append(unit['unit_id']) collection = RepoContentUnit.get_collection() for unit_type_id, unit_ids in unit_map.items(): spec = { 'repo_id': repo_id, 'unit_type_id': unit_type_id, 'unit_id': { '$in': unit_ids } } collection.remove(spec) repo_controller.update_last_unit_removed(repo_id) repo_controller.rebuild_content_unit_counts(repo) # Match the return type/format as copy serializable_units = [u.to_id_dict() for u in transfer_units] return {'units_successful': serializable_units}
def unassociate_by_criteria(repo_id, criteria, notify_plugins=True): """ Unassociate units that are matched by the given criteria. :param repo_id: identifies the repo :type repo_id: str :param criteria: :param notify_plugins: if true, relevant plugins will be informed of the removal :type notify_plugins: bool """ association_query_manager = manager_factory.repo_unit_association_query_manager() unassociate_units = association_query_manager.get_units(repo_id, criteria=criteria) if len(unassociate_units) == 0: return {} unit_map = {} # maps unit_type_id to a list of unit_ids for unit in unassociate_units: id_list = unit_map.setdefault(unit['unit_type_id'], []) id_list.append(unit['unit_id']) collection = RepoContentUnit.get_collection() for unit_type_id, unit_ids in unit_map.items(): spec = {'repo_id': repo_id, 'unit_type_id': unit_type_id, 'unit_id': {'$in': unit_ids} } collection.remove(spec, safe=True) unique_count = sum( 1 for unit_id in unit_ids if not RepoUnitAssociationManager.association_exists( repo_id, unit_id, unit_type_id)) if not unique_count: continue repo_controller.update_unit_count(repo_id, unit_type_id, -unique_count) repo_controller.update_last_unit_removed(repo_id) # Convert the units into transfer units. This happens regardless of whether or not # the plugin will be notified as it's used to generate the return result, unit_type_ids = calculate_associated_type_ids(repo_id, unassociate_units) transfer_units = create_transfer_units(unassociate_units, unit_type_ids) if notify_plugins: remove_from_importer(repo_id, transfer_units) # Match the return type/format as copy serializable_units = [u.to_id_dict() for u in transfer_units] return {'units_successful': serializable_units}