Пример #1
0
async def search_get(context, request):
    search = query_utility(ICatalogUtility)
    if search is None:
        return {'@id': request.url.human_repr(), 'items': [], 'items_total': 0}

    parser = resolve_dotted_name(app_settings['search_parser'])
    call_params, full_objects = parser(request, context)()
    result = await search.get_by_path(**call_params)

    real_result = {
        '@id': request.url.human_repr(),
        'items': [],
        'items_total': result['items_count']
    }

    for member in result['member']:
        if full_objects:
            obj = await get_object_by_oid(member['uuid'])

            view = DefaultGET(obj, request)
            serialization = await view()
            real_result['items'].append(serialization)
        else:
            member['@id'] = member['@absolute_url']
            del member['@absolute_url']
    real_result['aggregations'] = {
        key: value['buckets']
        for key, value in result.get('aggregations', {}).items()
    }
    real_result['items'] = result['member']
    return real_result
Пример #2
0
    async def _query(self,
                     context: IResource,
                     query: ParsedQueryInfo,
                     unrestricted: bool = False):
        sql, arguments = self.build_query(context,
                                          query, ["id", "zoid", "json"],
                                          unrestricted=unrestricted)
        txn = get_current_transaction()
        conn = await txn.get_connection()
        results = []
        fullobjects = query["fullobjects"]
        container = find_container(context)
        if container is None:
            raise ContainerNotFound()

        try:
            context_url = get_object_url(container)
            request = get_current_request()
        except RequestNotFound:
            context_url = get_content_path(container)
            request = None

        logger.debug(f"Running search:\n{sql}\n{arguments}")

        async with txn.lock:
            records = await conn.fetch(sql, *arguments)
        for record in records:
            data = json.loads(record["json"])
            if fullobjects and request is not None and txn is not None:
                # Get Object
                obj = await txn.get(data["uuid"])
                # Serialize object
                view = DefaultGET(obj, request)
                result = await view()
            else:
                result = self.load_meatdata(query, data)
                result["@name"] = record["id"]
                result["@uid"] = record["zoid"]
                result["@id"] = data[
                    "@absolute_url"] = context_url + data["path"]
            results.append(result)

        # also do count...
        total = len(results)
        if total >= query["size"] or query["_from"] != 0:
            sql, arguments = self.build_count_query(context,
                                                    query,
                                                    unrestricted=unrestricted)
            logger.debug(f"Running search:\n{sql}\n{arguments}")
            async with txn.lock:
                records = await conn.fetch(sql, *arguments)
            total = records[0]["count"]
        return {"items": results, "items_total": total}
Пример #3
0
async def get_default(context, request):
    get = DefaultGET(context, request)
    return await get()