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
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
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
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