Пример #1
0
    def _search(self,
                model,
                fields,
                add_fields,
                q=None,
                f=None,
                page=0,
                limit=None,
                **kw):
        all_fields = fields + [(fld, fld) for fld in add_fields]
        c.search_form = W.admin_search_form(all_fields)
        c.page_list = W.page_list
        c.page_size = W.page_size
        count = 0
        objects = []
        limit, page, start = g.handle_paging(limit, page, default=25)
        if q:
            match = search.site_admin_search(model,
                                             q,
                                             f,
                                             rows=limit,
                                             start=start)
            if match:
                count = match.hits
                objects = match.docs

                ids = [obj['id'] for obj in objects]
                mongo_objects = search.mapped_artifacts_from_index_ids(
                    ids, model)
                for i in range(len(objects)):
                    obj = objects[i]
                    _id = obj['id'].split('#')[1]
                    obj['object'] = mongo_objects.get(_id)
                # Some objects can be deleted, but still have index in solr, should skip those
                objects = [o for o in objects if o.get('object')]

        def convert_fields(obj):
            # throw the type away (e.g. '_s' from 'url_s')
            result = {}
            for k, val in obj.iteritems():
                name = k.rsplit('_', 1)
                if len(name) == 2:
                    name = name[0]
                else:
                    name = k
                result[name] = val
            return result

        return {
            'q': q,
            'f': f,
            'objects': map(convert_fields, objects),
            'count': count,
            'page': page,
            'limit': limit,
            'fields': fields,
            'additional_fields': add_fields,
            'type_s': model.type_s,
        }
Пример #2
0
    def _search(self, model, fields, add_fields, q=None, f=None, page=0, limit=None, **kw):
        all_fields = fields + [(fld, fld) for fld in add_fields]
        c.search_form = W.admin_search_form(all_fields)
        c.page_list = W.page_list
        c.page_size = W.page_size
        count = 0
        objects = []
        limit, page, start = g.handle_paging(limit, page, default=25)
        if q:
            match = search.site_admin_search(model, q, f, rows=limit, start=start)
            if match:
                count = match.hits
                objects = match.docs
                ids = [obj['id'].split('#')[1] for obj in objects]
                ids = [bson.ObjectId(_id) for _id in ids if _id != 'None']
                mongo_objects = {}
                for obj in model.query.find({'_id': {'$in': ids}}):
                    mongo_objects[str(obj._id)] = obj

                for i in range(len(objects)):
                    obj = objects[i]
                    _id = obj['id'].split('#')[1]
                    obj['object'] = mongo_objects.get(_id)
                # Some objects can be deleted, but still have index in solr, should skip those
                objects = [o for o in objects if o.get('object')]

        def convert_fields(obj):
            # throw the type away (e.g. '_s' from 'url_s')
            result = {}
            for k,val in obj.iteritems():
                name = k.rsplit('_', 1)
                if len(name) == 2:
                    name = name[0]
                else:
                    name = k
                result[name] = val
            return result

        return {
            'q': q,
            'f': f,
            'objects': map(convert_fields, objects),
            'count': count,
            'page': page,
            'limit': limit,
            'fields': fields,
            'additional_fields': add_fields,
            'type_s': model.type_s,
        }
Пример #3
0
    def test_site_admin_search(self, search):
        from allura.lib.search import site_admin_search
        from allura.model import Project, User
        fq = ['type_s:Project']
        site_admin_search(Project, 'test', 'shortname', rows=25)
        search.assert_called_once_with(
            'shortname_s:test', fq=fq, ignore_errors=False, rows=25)

        search.reset_mock()
        site_admin_search(Project, 'shortname:test || shortname:test2', '__custom__')
        search.assert_called_once_with(
            'shortname_s:test || shortname_s:test2', fq=fq, ignore_errors=False)

        fq = ['type_s:User']
        search.reset_mock()
        site_admin_search(User, 'test-user', 'username', rows=25)
        search.assert_called_once_with(
            'username_s:test-user', fq=fq, ignore_errors=False, rows=25)

        search.reset_mock()
        site_admin_search(User, 'username:admin1 || username:root', '__custom__')
        search.assert_called_once_with(
            'username_s:admin1 || username_s:root', fq=fq, ignore_errors=False)