Exemplo n.º 1
0
def look_up(request, item_type):
    """ Returns JSON data for entities
        limited by certain criteria
    """
    ent = Entity()
    qstring = ''
    class_uri = False
    project_uuid = False
    vocab_uri = False
    ent_type = False
    context_uuid = False
    data_type = False
    if len(item_type) < 2:
        item_type = False
    if 'q' in request.GET:
        qstring = request.GET['q']
    if 'class_uri' in request.GET:
        class_uri = request.GET['class_uri']
    if 'project_uuid' in request.GET:
        project_uuid = request.GET['project_uuid']
    if 'vocab_uri' in request.GET:
        vocab_uri = request.GET['vocab_uri']
    if 'ent_type' in request.GET:
        ent_type = request.GET['ent_type']
    if 'context_uuid' in request.GET:
        context_uuid = request.GET['context_uuid']
    if 'data_type' in request.GET:
        data_type = request.GET['data_type']
    entity_list = ent.search(qstring, item_type, class_uri, project_uuid,
                             vocab_uri, ent_type, context_uuid, data_type)
    json_output = json.dumps(entity_list, indent=4, ensure_ascii=False)
    return HttpResponse(json_output,
                        content_type='application/json; charset=utf8')
Exemplo n.º 2
0
def look_up(request, item_type):
    """ Returns JSON data for entities
        limited by certain criteria
    """
    ent = Entity()
    qstring = ''
    class_uri = False
    project_uuid = False
    vocab_uri = False
    ent_type = False
    context_uuid = False
    data_type = False
    context = False
    if len(item_type) < 2:
        item_type = False
    if 'q' in request.GET:
        qstring = request.GET['q']
    if 'class_uri' in request.GET:
        class_uri = request.GET['class_uri']
    if 'project_uuid' in request.GET:
        project_uuid = request.GET['project_uuid']
    if 'vocab_uri' in request.GET:
        vocab_uri = request.GET['vocab_uri']
    if 'ent_type' in request.GET:
        ent_type = request.GET['ent_type']
    if 'context_uuid' in request.GET:
        context_uuid = request.GET['context_uuid']
    if 'data_type' in request.GET:
        data_type = request.GET['data_type']
    if 'context' in request.GET:
        context = request.GET['context']
    entity_list = ent.search(qstring,
                             item_type,
                             class_uri,
                             project_uuid,
                             vocab_uri,
                             ent_type,
                             context_uuid,
                             data_type,
                             context)
    json_output = json.dumps(entity_list,
                             indent=4,
                             ensure_ascii=False)
    return HttpResponse(json_output,
                        content_type='application/json; charset=utf8')
Exemplo n.º 3
0
def look_up(request, item_type):
    """ Returns JSON data for entities
        limited by certain criteria
    """
    # -----------------------------------------------------------------
    # NOTE: This is mainly for "in-house" use as an API to to look up
    # entities already in Open Context's database. It is mainly
    # used in preparation of datasets for ingest, where we want to
    # reconcile entities in an outside dataset with data already in
    # Open Context.
    #
    # NOTE: This does not use the Solr index at all. It only uses data
    # in the postgres datastore because the Solr index may not have
    # all data relevant in projects that are in preparation.
    # -----------------------------------------------------------------
    ent = Entity()
    arg_list = [
        'qstring',
        'item_type',
        'class_uri',
        'project_uuid',
        'vocab_uri',
        'ent_type',
        'context_uuid',
        'data_type',
        'context',
        'label',
    ]
    # Get all the request parameters to make an argument dict.
    args = {arg: request.GET.get(arg) for arg in arg_list}
    args['qstring'] = request.GET.get('q', '')

    # Item type is set in the URL path
    if item_type and len(item_type) < 2:
        item_type = None
    args['item_type'] = item_type

    entity_list = ent.search(**args)
    json_output = json.dumps(entity_list, indent=4, ensure_ascii=False)
    return HttpResponse(json_output,
                        content_type='application/json; charset=utf8')
Exemplo n.º 4
0
def do_manifest_items_search_tests(project_uuid, item_type, class_uri, uuid):
    """Actually tests random combinations of args on manifest
       entity searches
    """
    man = Manifest.objects.get(uuid=uuid)

    # The qstring is meant for filter on partial string matches, so
    # make a list of different strings to filter on based on
    # different attributes of the manifest object.
    qstring_opts = [
        man.slug, man.slug[:5], man.uuid, man.uuid[:5], man.label,
        man.label[:5]
    ]

    all_args = {
        'qstring': random.choice(qstring_opts),
        'project_uuid': project_uuid,
        'item_type': item_type,
        'class_uri': class_uri,
        'label': man.label,
    }
    # The entity.search results are a list of dicts with slightly
    # different keys than the arguments used in the search.
    # Below is a mapping between args (keys) and result
    # item keys.
    key_mappings = {
        'project_uuid': 'partOf_id',
        'item_type': 'type',
    }
    # Randomly generate different argument dicts that
    # use only certain keys and values of the all_args dict
    # above.
    keys = list(all_args.keys())
    args_list = [all_args]
    for num_sample in range(2, len(keys)):
        sample_keys = random.sample(keys, num_sample)
        new_args = {}
        for key in sample_keys:
            new_args[key] = all_args[key]
            if key == 'qstring':
                # Selects a random query string option.
                new_args[key] = random.choice(qstring_opts)
        if not 'qstring' in new_args:
            # The qstring is required, so make sure that
            # it is always at least an empty string.
            new_args['qstring'] = ''
        args_list.append(new_args)
    for args in args_list:
        ent = Entity()
        # Get the search results using the args.
        results = ent.search(**args)
        # Now test to make sure that the search results have the
        # expected values based on the search filtering criteria
        # passed by args.
        for key, value in args.items():
            if key == 'qstring' or not value:
                # Don't check qstring or an empty / missing value.
                continue
            check = key_mappings.get(key, key)
            for result in results:
                assert result[check] == value