Ejemplo n.º 1
0
 def __init__(self, request, parser=None, results=None, total=None,
              schema=None):
     self.request = request
     self.parser = parser or QueryParser(request.args, request.authz)
     self.schema = schema
     self.results = results or []
     self.total = total or 0
Ejemplo n.º 2
0
def entitysets(entity_id):
    """Returns a list of entitysets which the entity has references in
    ---
    get:
      summary: Shows EntitySets which reference the given entity
      description: >-
        Search for all entitysets which reference the given entity. The entity
        sets can be filtered based on it's collection id, label, type or the
        judgement of the entity within the set.
      parameters:
      - in: path
        name: entity_id
        required: true
        schema:
          type: string
      - in: query
        name: 'filter:type'
        description: Restrict to a EntitySets of a particular type
        required: false
        schema:
          items:
            type: string
          type: array
      - in: query
        name: 'filter:label'
        description: Restrict to a EntitySets with a particular label
        required: false
        schema:
          items:
            type: string
          type: array
      - in: query
        name: 'filter:judgement'
        description: Restrict to a specific profile judgement
        required: false
        schema:
          items:
            type: string
          type: array
      - in: query
        name: 'filter:collection_id'
        description: Restrict to entity sets within particular collections
        schema:
          items:
            type: string
          type: array
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/EntitySet'
          description: OK
      tags:
      - Entity
      - Profile
    """
    entity = get_index_entity(entity_id, request.authz.READ)

    parser = QueryParser(request.args, request.authz)
    collection_ids = [
        cid for cid in parser.filters.get("collection_id", [])
        if request.authz.can(cid, request.authz.READ)
    ]
    judgements = parser.filters.get("judgement")
    labels = parser.filters.get("label")
    types = parser.filters.get("type")

    if judgements is not None:
        judgements = list(map(Judgement, judgements))
    if not collection_ids:
        collection_ids = request.authz.collections(request.authz.READ)

    entitysets = EntitySet.by_entity_id(
        entity["id"],
        collection_ids=collection_ids,
        judgements=judgements,
        types=types,
        labels=labels,
    )
    result = DatabaseQueryResult(request, entitysets, parser=parser)
    return EntitySetSerializer.jsonify_result(result)
Ejemplo n.º 3
0
def expand(entity_id):
    """
    ---
    get:
      summary: Expand an entity to get its adjacent entities
      description: >-
        Get the property-wise list of entities adjacent to the entity
        with id `entity_id`.
      parameters:
      - in: path
        name: entity_id
        required: true
        schema:
          type: string
      - description: properties to filter on
        in: query
        name: 'filter:property'
        schema:
          type: string
      - in: query
        description: number of entities to return per property
        name: limit
        schema:
          type: number
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                allOf:
                - $ref: '#/components/schemas/QueryResponse'
                properties:
                  results:
                    type: array
                    items:
                      $ref: '#/components/schemas/EntityExpand'
      tags:
      - Entity
    """
    entity = get_index_entity(entity_id, request.authz.READ)
    proxy = model.get_proxy(entity)
    collection_id = entity.get("collection_id")
    tag_request(collection_id=collection_id)
    parser = QueryParser(request.args,
                         request.authz,
                         max_limit=MAX_EXPAND_ENTITIES)
    properties = parser.filters.get("property")
    results = expand_proxies(
        [proxy],
        properties=properties,
        authz=request.authz,
        limit=parser.limit,
    )
    result = {
        "status": "ok",
        "total": sum(result["count"] for result in results),
        "results": results,
    }
    return jsonify(result)
Ejemplo n.º 4
0
def expand(entity_id):
    """Returns a list of diagrams for the role
    ---
    get:
      summary: Expand an entity to get its adjacent entities
      description: >-
        Get the property-wise list of entities adjacent to the entity
        with id `entity_id`.
      parameters:
      - in: path
        name: entity_id
        required: true
        schema:
          type: string
      - in: query
        name: edge_types
        description: types of edges to expand. Must is a matchable FtM type
        required: true
        schema:
          type: string
      - description: properties to filter on
        in: query
        name: 'filter:property'
        schema:
          type: string
      - in: query
        description: number of entities to return per property
        name: limit
        schema:
          type: number
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                allOf:
                - $ref: '#/components/schemas/QueryResponse'
                properties:
                  results:
                    type: array
                    items:
                      $ref: '#/components/schemas/EntityExpand'
      tags:
      - Entity
    """
    enable_cache()
    entity = get_index_entity(entity_id, request.authz.READ)
    edge_types = request.args.getlist('edge_types')
    collection_id = entity.get('collection_id')
    tag_request(collection_id=collection_id)
    parser = QueryParser(request.args,
                         request.authz,
                         max_limit=MAX_EXPAND_ENTITIES)
    properties = parser.filters.get('property')
    results = []
    for (prop, total, proxies) in entity_expand(entity,
                                                collection_ids=[collection_id],
                                                edge_types=edge_types,
                                                properties=properties,
                                                authz=request.authz,
                                                limit=parser.limit):
        results.append({
            'count': total,
            'property': prop.name,
            'entities': [proxy.to_dict() for proxy in proxies]
        })
    return jsonify({
        'status': 'ok',
        'total': sum(result['count'] for result in results),
        'results': results
    })
Ejemplo n.º 5
0
from unittest import TestCase
from aleph.search.parser import QueryParser

args = QueryParser([('offset', '5'), ('filter:key1', 'foo1'),
                    ('filter:key1', 'foo2'), ('filter:key2', 'foo3'),
                    ('filter:key3', 'foo4'), ('filter:key3', 'foo4'),
                    ('filter:key2', 'foo5'), ('myint', 100), ('myint', 105),
                    ('mybadint', 'six'), ('mybool', 't'),
                    ('mybadbool', 'oui')], None)


class QueryParserTestCase(TestCase):
    def test_offset(self):
        self.assertEqual(args.offset, 5)

    def test_filters(self):
        self.assertItemsEqual(args.filters.keys(), ['key1', 'key2', 'key3'])
        self.assertEqual(args.filters['key1'], set(['foo1', 'foo2']))
        self.assertEqual(args.filters['key2'], set(['foo3', 'foo5']))
        self.assertEqual(args.filters['key3'], set(['foo4']))

    def test_getintlist(self):
        self.assertEqual(args.getintlist('myint'), [100, 105])
        self.assertEqual(args.getintlist('notakey'), [])
        self.assertEqual(args.getintlist('notakey', [8]), [8])

    def test_getint(self):
        self.assertEqual(args.getint('myint'), 100)
        self.assertEqual(args.getint('mybadint'), None)
        self.assertEqual(args.getint('notakey'), None)
        self.assertEqual(args.getint('notakey', 8), 8)
Ejemplo n.º 6
0
from unittest import TestCase
from aleph.search.parser import QueryParser

args = QueryParser(
    [
        ("offset", "5"),
        ("filter:key1", "foo1"),
        ("filter:key1", "foo2"),
        ("filter:key2", "foo3"),
        ("filter:key3", "foo4"),
        ("filter:key3", "foo4"),
        ("filter:key2", "foo5"),
        ("myint", 100),
        ("myint", 105),
        ("mybadint", "six"),
        ("mybool", "t"),
        ("mybadbool", "oui"),
    ],
    None,
)


class QueryParserTestCase(TestCase):
    def test_offset(self):
        self.assertEqual(args.offset, 5)

    def test_filters(self):
        self.assertCountEqual(list(args.filters.keys()),
                              ["key1", "key2", "key3"])  # noqa
        self.assertEqual(args.filters["key1"], set(["foo1", "foo2"]))
        self.assertEqual(args.filters["key2"], set(["foo3", "foo5"]))