示例#1
0
class ContentUnitSearch(search.SearchView):
    """
    Adds GET and POST searching for content units.
    """
    optional_bool_fields = ('include_repos', )
    manager = content_query.ContentQueryManager()

    @staticmethod
    def _add_repo_memberships(units, type_id):
        """
        For a list of units, find what repos each is a member of and add a list
        of repo_ids to each unit.

        :param units:   list of unit documents
        :type  units:   list of dicts
        :param type_id: content type id
        :type  type_id: str
        :return:    same list of units that was passed in, only for convenience.
                    units are modified in-place
        """
        # quick return if there is nothing to do
        if not units:
            return units

        unit_ids = [unit['_id'] for unit in units]
        criteria = Criteria(filters={
            'unit_id': {
                '$in': unit_ids
            },
            'unit_type_id': type_id
        },
                            fields=('repo_id', 'unit_id'))
        associations = factory.repo_unit_association_query_manager(
        ).find_by_criteria(criteria)
        unit_ids = None
        criteria = None
        association_map = {}
        for association in associations:
            association_map.setdefault(association['unit_id'],
                                       set()).add(association['repo_id'])

        for unit in units:
            unit['repository_memberships'] = list(
                association_map.get(unit['_id'], []))
        return units

    @classmethod
    def get_results(cls, query, search_method, options, *args, **kwargs):
        """
        Overrides the base class so additional information can optionally be added.
        """
        type_id = kwargs['type_id']
        units = list(search_method(type_id, query))
        units = [_process_content_unit(unit, type_id) for unit in units]
        if options.get('include_repos') is True:
            cls._add_repo_memberships(units, type_id)
        return units
示例#2
0
    def setUp(self, mock_repo_qs, mock_imp_qs, mock_remove):
        super(RepoSyncConduitTests, self).setUp()
        mock_plugins.install()
        types_database.update_database([TYPE_1_DEF, TYPE_2_DEF])

        self.association_manager = association_manager.RepoUnitAssociationManager()
        self.association_query_manager = association_query_manager.RepoUnitAssociationQueryManager()
        self.content_manager = content_manager.ContentManager()
        self.query_manager = query_manager.ContentQueryManager()

        self.conduit = RepoSyncConduit('repo-1', 'test-importer', 'abc123')
        importer_controller.set_importer('repo-1', 'mock-importer', {})
示例#3
0
    def setUp(self):
        super(RepoSyncConduitTests, self).setUp()
        mock_plugins.install()
        types_database.update_database([TYPE_1_DEF, TYPE_2_DEF])

        self.repo_manager = repo_manager.RepoManager()
        self.importer_manager = importer_manager.RepoImporterManager()
        self.sync_manager = sync_manager.RepoSyncManager()
        self.association_manager = association_manager.RepoUnitAssociationManager()
        self.association_query_manager = association_query_manager.RepoUnitAssociationQueryManager()
        self.content_manager = content_manager.ContentManager()
        self.query_manager = query_manager.ContentQueryManager()

        self.repo_manager.create_repo('repo-1')
        self.importer_manager.set_importer('repo-1', 'mock-importer', {})
        self.conduit = RepoSyncConduit('repo-1', 'test-importer', 'importer', 'importer-id')