def get(self, request, *args, **kwargs): target_uri = os.path.splitext(request.build_absolute_uri())[0].replace('/data/', '/resource/') if not self.request.path.startswith("/data"): target_uri = re.sub('/[a-z]{2}/resource/', '/resource/', target_uri, count=1) if target_uri.endswith('graph'): target_uri = re.sub("/graph", "", target_uri) extension_ = self.kwargs.get('extension') rdf_format = mime_to_extension(get_lod_mime_type(extension_, self.request)) if rdf_format == "rdf": rdf_format = "xml" resolved_uri = RDFRecord.get_internal_rdf_base_uri(target_uri) if "/resource/cache/" in target_uri: # old lookup rdfstore.get_rdfstore().get_cached_source_uri(target_uri) target_uri = target_uri.split('/resource/cache/')[-1] if 'geonames.org' in target_uri: target_uri = '{}/'.format(target_uri) if CacheResource.objects.filter(document_uri=target_uri).exists(): cache_object = CacheResource.objects.filter(document_uri=target_uri).first() content = cache_object.get_graph().serialize(format=rdf_format) else: raise UnknownGraph("URI {} is not known in our graph store".format(target_uri)) elif settings.RDF_USE_LOCAL_GRAPH: mode = self.request.REQUEST.get('mode', 'default') acceptance = True if mode == 'acceptance' else False local_object = ElasticSearchRDFRecord(source_uri=resolved_uri) local_object.get_graph_by_source_uri(uri=resolved_uri) if not local_object.exists(): # todo: temporary work around for EDMRecords not saved with subjects logger.warn("Unable to find graph for: {}".format(resolved_uri)) raise UnknownGraph("URI {} is not known in our graph store".format(resolved_uri)) mode = self.get_mode(request) if mode in ['context', 'api', 'api-flat']: # get_graph(with_mappings=True, include_mapping_target=True, acceptance=acceptance) content = local_object.get_context_graph(with_mappings=True, include_mapping_target=True) if mode in ['api', 'api-flat']: bindings = GraphBindings(about_uri=resolved_uri, graph=content) index_doc = bindings.to_index_doc() if mode == 'api' else bindings.to_flat_index_doc() content = json.dumps(index_doc) rdf_format = 'json-ld' else: content = content.serialize(format=rdf_format) else: content = local_object.get_graph() content = content.serialize(format=rdf_format) elif self.store.ask(uri=resolved_uri): target_uri = resolved_uri content = self.get_content(target_uri, rdf_format, request) return HttpResponse( content, content_type='{}; charset=utf8'.format(result_extension_to_mime(rdf_format)) )
def get_content(self, target_uri, rdf_format, request): if '/resource/aggregation' in target_uri: target_named_graph = "{}/graph".format(target_uri.rstrip('/')) mode = self.get_mode(request, 'graph') describe = self.get_graph(mode=mode, named_graph=target_named_graph) else: mode = self.get_mode(request, "describe") describe = self.get_graph(mode=mode, uri=target_uri) if not describe: logger.warn("Unable to find graph for: {}".format(target_uri)) raise Http404() if mode in ['api', 'api-flat']: bindings = GraphBindings(about_uri=target_uri, graph=describe) index_doc = bindings.to_index_doc() if mode == 'api' else bindings.to_flat_index_doc() content = json.dumps(index_doc) rdf_format = 'json' else: content = describe.serialize(format=rdf_format) return content, rdf_format
def create_es_action(self, action="index", record_type=None, index=settings.SITE_NAME, store=None, doc_type=None, context=True, flat=True, exclude_fields=None, acceptance=False): if doc_type is None: doc_type = self._generate_doc_type() if record_type is None: record_type = self.get_rdf_type() if not store: store = rdfstore.get_rdfstore() if acceptance: index = "{}_acceptance".format(index) if record_type == "http://www.openarchives.org/ore/terms/Aggregation": record_type = "mdr" if action == "delete": return { '_op_type': action, '_index': index, '_type': doc_type, '_id': self.hub_id } graph = None if not context: graph = self.get_graph() else: graph, nr_levels = self.get_context_graph(store=store, named_graph=self.named_graph) graph.namespace_manager = namespace_manager bindings = GraphBindings( about_uri=self.source_uri, graph=graph ) index_doc = bindings.to_flat_index_doc() if flat else bindings.to_index_doc() if exclude_fields: index_doc = {k: v for k, v in index_doc.items() if k not in exclude_fields} # add delving spec for default searchability index_doc["delving_spec"] = [ {'@type': "Literal", 'value': self.get_spec_name(), 'raw': self.get_spec_name(), 'lang': None} ] logger.debug(index_doc) mapping = { '_op_type': action, '_index': index, '_type': doc_type, '_id': self.hub_id, '_source': index_doc } thumbnail = bindings.get_about_thumbnail mapping['_source']['system'] = { 'slug': self.hub_id, 'spec': self.get_spec_name(), 'thumbnail': thumbnail if thumbnail else "", 'preview': "detail/foldout/{}/{}".format(doc_type, self.hub_id), 'caption': bindings.get_about_caption if bindings.get_about_caption else "", 'about_uri': self.document_uri, 'source_uri': self.source_uri, 'graph_name': self.named_graph, 'created_at': datetime.datetime.now().isoformat(), 'modified_at': datetime.datetime.now().isoformat(), 'source_graph': graph.serialize(format='nt', encoding="utf-8").decode(encoding="utf-8"), 'proxy_resource_graph': None, 'web_resource_graph': None, # 'about_type': [rdf_type.qname for rdf_type in bindings.get_about_resource().get_types()] # 'collections': None, todo find a way to add collections via link } data_owner = self.dataset.data_owner if hasattr(self, 'dataset') else None dataset_name = self.dataset.name if hasattr(self, 'dataset') else None mapping['_source']['legacy'] = { 'delving_hubId': self.hub_id, 'delving_recordType': record_type, 'delving_spec': self.get_spec_name(), 'delving_owner': data_owner, 'delving_orgId': settings.ORG_ID, 'delving_collection': dataset_name, 'delving_title': self.get_first_literal(DC.title, graph), 'delving_creator': self.get_first_literal(DC.creator, graph), 'delving_description': self.get_first_literal(DC.description, graph), 'delving_provider': index_doc.get('edm_provider')[0].get('value') if 'edm_provider' in index_doc else None, 'delving_hasGeoHash': "true" if bindings.has_geo() else "false", 'delving_hasDigitalObject': "true" if thumbnail else "false", 'delving_hasLandingePage': "true" if 'edm_isShownAt' in index_doc else "false", 'delving_hasDeepZoom': "true" if 'nave_deepZoom' in index_doc else "false", } return mapping
def retrieve(self, request, pk=None, format=None, *args, **kwargs): def get_mode(default=None): params = request.GET return params.get('schema', default) self._clean_callback(request) query = NaveESQuery( index_name=self.get_index_name, doc_types=self.doc_types, default_facets=self.facets, cluster_geo=False, size=1, converter=self.get_converter() ) try: query = query.build_item_query(query, request.query_params, pk) except ValueError as ve: logger.error("Unable to build request because: {}".format(ve)) # todo display error message when bad/unknown hubId is given return HttpResponseBadRequest() mlt = True if request.query_params.get('mlt', 'false') == "true" else False mlt_count = int(request.query_params.get('mlt.count', 5)) mlt_filter_queries = request.query_params.getlist('mlt.qf', []) mlt_fq_dict = {} for fq in mlt_filter_queries: if ":" in fq: k, v = fq.split(":", maxsplit=1) mlt_fq_dict[k] = v record = ElasticSearchRDFRecord(hub_id=pk) record.get_graph_by_id(hub_id=pk) response = NaveItemResponse( query, self, index=self.get_index_name, mlt=mlt, mlt_count=mlt_count, mlt_filter_query=mlt_fq_dict, rdf_record=record ) renderer_format = request.accepted_renderer.format if renderer_format in list(EXTENSION_TO_MIME_TYPE.keys()) and renderer_format not in ['xml', 'json']: graph = record.get_graph() graph_string = graph.serialize(format=renderer_format).decode('utf-8') mime_type = EXTENSION_TO_MIME_TYPE.get(renderer_format) return Response(data=graph_string, content_type=mime_type) target_uri = record.document_uri if settings.RDF_USE_LOCAL_GRAPH: graph = record.get_graph() else: store = rdfstore.get_rdfstore() graph, _ = RDFModel.get_context_graph(store, named_graph=record.named_graph) if not graph: from django.http import HttpResponseNotFound return HttpResponseNotFound() mode = get_mode(self.default_converter) bindings = GraphBindings(about_uri=target_uri, graph=graph) delving_fields = False if request.GET.get("delving_fields") == 'false' else True converter = None if mode in ['api', 'api-flat']: index_doc = bindings.to_index_doc() if mode == 'api' else bindings.to_flat_index_doc() elif mode in REGISTERED_CONVERTERS.keys(): converter = REGISTERED_CONVERTERS.get(mode) index_doc = converter( bindings=bindings, graph=graph, about_uri=bindings.about_uri() ).convert(add_delving_fields=delving_fields) elif self.default_converter in REGISTERED_CONVERTERS.keys(): converter = REGISTERED_CONVERTERS.get(self.default_converter) index_doc = converter( bindings=bindings, graph=graph, about_uri=bindings.about_uri() ).convert(add_delving_fields=delving_fields) else: logger.warn("unable to convert results to schema {}".format(mode)) index_doc = bindings.to_index_doc() layout_fields = OrderedDict() layout_fields['layout'] = converter().get_layout_fields() if converter else [] if response.get_mlt(): mlt = {"item": [NaveESItemSerializer(item).data for item in response.get_mlt()]} else: mlt = "" result = {'result': { 'layout': layout_fields, 'item': {'fields': index_doc}, "relatedItems": mlt}} return Response(result)