def filter_instances(query_params):
    if not class_exists(query_params):
        error_message = u"Class {0} in graph {1} does not exist".format(
            query_params["class_uri"], query_params["graph_uri"])
        raise HTTPError(404, log_message=error_message)

    keymap = {
        "label": "title",
        "subject": "@id",
        "sort_object": shorten_uri(query_params["sort_by"]),
    }
    for p, o, index in extract_po_tuples(query_params):
        keymap[o[1:]] = shorten_uri(p)

    result_dict = query_filter_instances(query_params)
    if not result_dict or not result_dict['results']['bindings']:
        return None

    items_list = compress_keys_and_values(
        result_dict,
        keymap=keymap,
        ignore_keys=["total"],
        do_expand_uri=query_params['expand_uri'] == u"1")

    items_list = merge_by_id(items_list)
    for item in items_list:
        uri = item["@id"]
        item["instance_prefix"] = u"{0}/".format(uri.rsplit('/', 1)[0])
        item["class_prefix"] = expand_uri(query_params['class_prefix'])

    decorate_with_resource_id(items_list)
    return build_json(items_list, query_params)
Example #2
0
def assemble_list_json(query_params, query_result_dict):
    context = MemorizeContext()
    expand_uri = bool(int(query_params.get('expand_uri', '0')))
    items_list = compress_keys_and_values(
        query_result_dict,
        keymap={"class": "@id", "label": "title"},
        context=context,
        expand_uri=expand_uri)
    items_list = compress_duplicated_ids(items_list)
    decorate_with_resource_id(items_list)
    decorate_with_class_prefix(items_list)

    context_section = context.context
    context_section.update({"@language": query_params.get("lang")})

    json_dict = {
        '_base_url': remove_last_slash(query_params.base_url),
        'items': items_list,
        '@context': context_section,
        '@id': query_params['graph_uri']
    }

    def calculate_total_items():
        count_query_result_dict = query_count_classes(query_params)
        total_items = int(get_one_value(count_query_result_dict, "total_items"))
        return total_items
    decorate_dict_with_pagination(json_dict, query_params, calculate_total_items)

    return json_dict
Example #3
0
 def test_query(self):
     expected_classes = [{
         u'class': u'http://example.onto/PlaceWithoutLanguage',
         u'label': u'Place'
     }, {
         u'class': u'http://example.onto/Lugar',
         u'label': u'Lugar'
     }, {
         u'class': u'http://example.onto/Place',
         u'label': u'Lugar'
     }, {
         u'class': u'http://example.onto/City',
         u'label': u'Cidade'
     }]
     query_params = Params({
         "graph_uri":
         self.graph_uri,
         "lang":
         "pt",
         "page":
         0,
         "per_page":
         10,
         'lang_filter_label':
         '\n    FILTER(langMatches(lang(?label), "pt") OR langMatches(lang(?label), "")) .\n'
     })
     response = get_context.query_classes_list(query_params)
     compressed_response = compress_keys_and_values(response)
     self.assertItemsEqual(expected_classes, compressed_response)
Example #4
0
 def test_query(self):
     expected_classes = [
         {u'class': u'http://example.onto/PlaceWithoutLanguage', u'label': u'Place'},
         {u'class': u'http://example.onto/Lugar', u'label': u'Lugar'},
         {u'class': u'http://example.onto/Place', u'label': u'Lugar'},
         {u'class': u'http://example.onto/City', u'label': u'Cidade'}]
     query_params = Params({
         "graph_uri": self.graph_uri,
         "lang": "pt",
         "page": 0,
         "per_page": 10,
         'lang_filter_label': '\n    FILTER(langMatches(lang(?label), "pt") OR langMatches(lang(?label), "")) .\n'
     })
     response = get_context.query_classes_list(query_params)
     compressed_response = compress_keys_and_values(response)
     self.assertItemsEqual(expected_classes, compressed_response)
Example #5
0
def execute_query(query_id, stored_query, querystring_params):
    query = get_query(stored_query, querystring_params)

    # TODO extract_method?
    request_dict = copy.copy(querystring_params.triplestore_config)
    request_dict.update({"query_id": query_id})
    request_dict.update({"query": query})
    log.logger.info(QUERY_EXECUTION_LOG_FORMAT.format(**request_dict))

    result_dict = query_sparql(query,
                               querystring_params.triplestore_config)
    items = compress_keys_and_values(result_dict)
    if not items:
        message = NO_RESULTS_MESSAGE_FORMAT.format(querystring_params.triplestore_config["url"], query)
        return {
            "items": [],
            # TODO explain in which instance of Virtuoso the query was executed?
            "warning": message}
    return {"items": items}
Example #6
0
def execute_query(query_id, stored_query, querystring_params):
    query = get_query(stored_query, querystring_params)

    # TODO extract_method?
    request_dict = copy.copy(querystring_params.triplestore_config)
    request_dict.update({"query_id": query_id})
    request_dict.update({"query": query})
    log.logger.info(QUERY_EXECUTION_LOG_FORMAT.format(**request_dict))

    result_dict = query_sparql(query, querystring_params.triplestore_config)
    items = compress_keys_and_values(result_dict)
    if not items:
        message = NO_RESULTS_MESSAGE_FORMAT.format(
            querystring_params.triplestore_config["url"], query)
        return {
            "items": [],
            # TODO explain in which instance of Virtuoso the query was executed?
            "warning": message
        }
    return {"items": items}
Example #7
0
def filter_instances(query_params):
    if not class_exists(query_params):
        error_message = u"Class {0} in graph {1} does not exist".format(
            query_params["class_uri"], query_params["graph_uri"])
        raise HTTPError(404, log_message=error_message)

    keymap = {
        "label": "title",
        "subject": "@id",
        "sort_object": shorten_uri(query_params["sort_by"]),
    }
    for p, o, index in extract_po_tuples(query_params):
        keymap[o[1:]] = shorten_uri(p)

    result_dict = query_filter_instances(query_params)
    if not result_dict or not result_dict['results']['bindings']:
        return None

    items_list = compress_keys_and_values(result_dict, keymap=keymap, ignore_keys=["total"])
    items_list = merge_by_id(items_list)
    add_prefix(items_list, query_params['class_prefix'])
    decorate_with_resource_id(items_list)
    return build_json(items_list, query_params)