Example #1
0
def list_all_contexts(query_params):
    sparql_response = triplestore.query_sparql(QUERY_LIST_CONTEXT, query_params.triplestore_config)
    all_contexts_uris = sparql.filter_values(sparql_response, "graph")

    filtered_contexts = filter_and_build_contexts(all_contexts_uris)
    if not filtered_contexts:
        json = {
            "items": [],
            "warning": "No contexts were found."
        }
        return json

    page_index = int(query_params["page"])
    per_page = int(query_params["per_page"])
    contexts_pages = split_into_chunks(filtered_contexts, per_page)
    try:
        contexts = contexts_pages[page_index]
    except IndexError:
        json = {
            "items": [],
            "warning": u"No contexts were found in page {0:d}.".format(int(query_params["page"]) + 1)
        }
        return json

    json = {
        '_base_url': remove_last_slash(query_params.base_url),
        'items': contexts
    }

    def calculate_total_items():
        return len(filtered_contexts)
    decorate_dict_with_pagination(json, query_params, calculate_total_items)

    return json
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 build_json(items_list, query_params):
    class_url = build_class_url(query_params)
    schema_url = unquote(build_schema_url_for_instance(query_params, class_url))

    class_properties = get_class.get_cached_schema(query_params)["properties"]
    items_list = cast_items_values(items_list, class_properties)

    json = {
        '_schema_url': schema_url,
        'pattern': '',
        '_class_prefix': expand_uri(query_params['class_prefix']),
        '_base_url': remove_last_slash(query_params.base_url),
        'items': items_list,
        '@context': {"@language": query_params.get("lang")},
        '@id': query_params['class_uri']
    }

    def calculate_total_items():
        result_dict = query_count_filter_instances(query_params)
        total_items = int(get_one_value(result_dict, 'total'))
        return total_items

    decorate_dict_with_pagination(json, query_params, calculate_total_items)

    return json
Example #4
0
def list_all_contexts(query_params):
    sparql_response = triplestore.query_sparql(QUERY_LIST_CONTEXT,
                                               query_params.triplestore_config)
    all_contexts_uris = sparql.filter_values(sparql_response, "graph")

    filtered_contexts = filter_and_build_contexts(all_contexts_uris)
    if not filtered_contexts:
        json = {"items": [], "warning": "No contexts were found."}
        return json

    page_index = int(query_params["page"])
    per_page = int(query_params["per_page"])
    contexts_pages = split_into_chunks(filtered_contexts, per_page)
    try:
        contexts = contexts_pages[page_index]
    except IndexError:
        json = {
            "items": [],
            "warning":
            u"No contexts were found in page {0:d}.".format(
                int(query_params["page"]) + 1)
        }
        return json

    json = {
        '_base_url': remove_last_slash(query_params.base_url),
        'items': contexts
    }

    def calculate_total_items():
        return len(filtered_contexts)

    decorate_dict_with_pagination(json, query_params, calculate_total_items)

    return json
Example #5
0
def _get_response_dict(stored_queries_result, params):
    response_dict = {}
    total_items, items_dict = _get_items_dict(stored_queries_result)
    response_dict.update(items_dict)
    if not items_dict["items"]:
        response_dict.update({"warning": NO_RESULTS_MESSAGE_FORMAT})
    else:
        decorate_dict_with_pagination(response_dict, params, lambda: total_items)
    return response_dict
Example #6
0
def build_json(items_list, item_count, query_params):

    json = {
        '_base_url': query_params.base_url,
        'items': items_list,
        "@context": {"@language": query_params.get("lang")},
    }

    calculate_total_items = lambda: item_count
    resources.decorate_dict_with_pagination(json, query_params, calculate_total_items)

    return json
Example #7
0
def build_json(items_list, item_count, query_params):

    json = {
        '_base_url': query_params.base_url,
        'items': items_list,
        "@context": {
            "@language": query_params.get("lang")
        },
    }

    calculate_total_items = lambda: item_count
    resources.decorate_dict_with_pagination(json, query_params,
                                            calculate_total_items)

    return json
Example #8
0
def _build_json(items_list, item_count, query_params):

    json = {
        '_base_url': query_params.base_url,
        'items': items_list,
        "@context": {"@language": query_params.get("lang")},
        "pattern": query_params["pattern"],
        # Variables needed for corresponding json-schema
        "_graph_uri": query_params["graph_uri"],
        "_class_prefix": query_params["class_prefix"],
        "_class_uri": query_params["class_uri"]
    }

    calculate_total_items = lambda: item_count
    resources.decorate_dict_with_pagination(json, query_params, calculate_total_items)

    return json
Example #9
0
def _build_json(items_list, item_count, query_params):

    json = {
        '_base_url': query_params.base_url,
        'items': items_list,
        "@context": {
            "@language": query_params.get("lang")
        },
        "pattern": query_params["pattern"],
        # Variables needed for corresponding json-schema
        "_graph_uri": query_params["graph_uri"],
        "_class_prefix": query_params["class_prefix"],
        "_class_uri": query_params["class_uri"]
    }

    calculate_total_items = lambda: item_count
    resources.decorate_dict_with_pagination(json, query_params,
                                            calculate_total_items)

    return json