예제 #1
0
파일: views.py 프로젝트: delving/nave
    def get_context_data(self, **kwargs):
        context = super(NaveDocumentTemplateView, self).get_context_data(**kwargs)
        absolute_uri = self.request.build_absolute_uri()
        target_uri = RDFRecord.get_internal_rdf_base_uri(absolute_uri)

        if "detail/foldout/" in target_uri:
            slug = self.kwargs.get('slug')
            record = ElasticSearchRDFRecord(hub_id=slug)
            graph = record.get_graph_by_id(self.kwargs.get('slug'))
            if graph is not None:
                target_uri = record.source_uri
            else:
                logger.warn("Unable to find source_uri for slug: {}".format(slug))
        else:
            target_uri = RDFRecord.get_internal_rdf_base_uri(absolute_uri)
            record = ElasticSearchRDFRecord(hub_id=self.kwargs.get('slug'))
            graph = record.get_graph_by_source_uri(target_uri)
        if graph is None:
            raise UnknownGraph("URI {} is not known in our graph store".format(target_uri))
        if "/resource/cache/" in target_uri:
            target_uri = target_uri.rstrip('/')
            cache_resource = CacheResource.objects.filter(document_uri=target_uri)
            if cache_resource.exists():
                graph = cache_resource.first().get_graph()
        elif settings.RDF_USE_LOCAL_GRAPH:
            mode = self.request.REQUEST.get('mode', 'default')
            acceptance = True if mode == 'acceptance' else False
            context['acceptance'] = acceptance

        elif '/resource/aggregation' in target_uri:
            target_named_graph = "{}/graph".format(target_uri.rstrip('/'))
            graph, nr_levels = RDFModel.get_context_graph(store=rdfstore.get_rdfstore(), named_graph=target_named_graph)
        else:
            graph, nr_levels = RDFModel.get_context_graph(
                store=rdfstore.get_rdfstore(),
                target_uri=target_uri
            )
        # todo: remove: should no longer be necessary with the addition of common.middleware.ForceLangMiddleware
        language = self.request.GET.get('lang', None)
        if language:
            activate(language)
        bindings = GraphBindings(
            about_uri=target_uri,
            graph=graph,
            excluded_properties=settings.RDF_EXCLUDED_PROPERTIES
        )
        context['resources'] = bindings
        context['absolute_uri'] = RDFRecord.get_external_rdf_url(target_uri, self.request)
        for rdf_type in bindings.get_about_resource().get_types():
            search_label = rdf_type.search_label.lower()
            content_template = settings.RDF_CONTENT_FOLDOUTS.get(search_label)
            if content_template:
                self.template_name = content_template
                break

        context['points'] = RDFModel.get_geo_points(graph)

        return context
예제 #2
0
파일: views.py 프로젝트: delving/nave
    def get_context_data(self, **kwargs):
        context = super(NaveDocumentDetailView, self).get_context_data(**kwargs)
        target_uri = self.object.document_uri
        if "/resource/cache/" in target_uri:
            target_uri = target_uri.rstrip('/')
            cache_resource = CacheResource.objects.filter(document_uri=target_uri)
            if cache_resource.exists():
                graph = cache_resource.first().get_graph()
        elif settings.RDF_USE_LOCAL_GRAPH:
            mode = self.request.REQUEST.get('mode', 'default')
            acceptance = True if mode == 'acceptance' else False
            context['acceptance'] = acceptance
            if isinstance(self.object, EDMRecord):
                graph = self.object.get_graph(with_mappings=True, include_mapping_target=True, acceptance=acceptance)
            else:
                graph = self.object.get_graph(acceptance=acceptance)
        elif '/resource/aggregation' in target_uri:
            target_named_graph = "{}/graph".format(target_uri.rstrip('/'))
            graph, nr_levels = RDFModel.get_context_graph(store=rdfstore.get_rdfstore(), named_graph=target_named_graph)
        else:
            graph, nr_levels = RDFModel.get_context_graph(
                store=rdfstore.get_rdfstore(),
                target_uri=target_uri
            )
        # todo: remove: should no longer be necessary with the addition of common.middleware.ForceLangMiddleware
        language = self.request.GET.get('lang', None)
        if language:
            activate(language)
        bindings = GraphBindings(
            about_uri=target_uri,
            graph=graph,
            excluded_properties=settings.RDF_EXCLUDED_PROPERTIES
        )
        context['resources'] = bindings
        for rdf_type in bindings.get_about_resource().get_types():
            search_label = rdf_type.search_label.lower()
            content_template = settings.RDF_CONTENT_FOLDOUTS.get(search_label)
            if content_template:
                self.template_name = content_template
                break

        context['points'] = RDFModel.get_geo_points(graph)

        return context
예제 #3
0
파일: test_tasks.py 프로젝트: delving/nave
 def test_context_graph_inlining(self):
     named_graph = "http://localhost:8000/resource/aggregation/ton-smits-huis/454/graph"
     inline_id = "http://data.cultureelerfgoed.nl/semnet/7403e26d-cf33-4372-ad72-a2f9fcf8f63b"
     context_graph, nr_levels = RDFModel.get_context_graph(
         store=self.store,
         named_graph=named_graph
     )
     self.assertIsNotNone(context_graph)
     self.assertIsInstance(context_graph, Graph)
     assert URIRef(inline_id) in list(context_graph.subjects())
     predicates = set(list(context_graph.predicates()))
     assert URIRef('http://www.openarchives.org/ore/terms/aggregates') in predicates
예제 #4
0
파일: tasks.py 프로젝트: delving/nave
def synchronise_record(graph_uri, ds, store, es_actions, index=settings.SITE_NAME, acceptance=False):
    # get graph
    # graph = store.get_graph_store.get(graph_uri, as_graph=True)
    graph, levels = RDFModel.get_context_graph(named_graph=graph_uri, store=store)
    # create EDM record
    edm_record = EDMRecord.graph_to_record(graph, ds)
    # create ES insert action
    index_action = edm_record.create_es_action(
        index=get_index_name(acceptance),
        record_type="Aggregated",
        store=store,
        context=False,
        exclude_fields=ds.excluded_index_fields.names()
    )
    es_actions.append(index_action)
    return edm_record, index_action
예제 #5
0
파일: views.py 프로젝트: delving/nave
    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)