Пример #1
0
def json_view(request, spatial_context=None):
    """ API for searching Open Context """
    rd = RequestDict()
    request_dict_json = rd.make_request_dict_json(request,
                                                  spatial_context)
    solr_s = SolrSearch()
    if solr_s.solr is not False:
        response = solr_s.search_solr(request_dict_json)
        m_json_ld = MakeJsonLd(request_dict_json)
        # share entities already looked up. Saves database queries
        m_json_ld.entities = solr_s.entities
        m_json_ld.request_full_path = request.get_full_path()
        m_json_ld.spatial_context = spatial_context
        json_ld = m_json_ld.convert_solr_json(response.raw_content)
        req_neg = RequestNegotiation('application/json')
        req_neg.supported_types = ['application/ld+json']
        recon_obj = Reconciliation()
        json_ld = recon_obj.process(request.GET,
                                    json_ld)
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            # requester wanted a mimetype we DO support
            return HttpResponse(json.dumps(json_ld,
                                ensure_ascii=False, indent=4),
                                content_type=req_neg.use_response_type + "; charset=utf8")
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                status=415)
    else:
        template = loader.get_template('500.html')
        context = RequestContext(request,
                                 {'error': 'Solr Connection Problem'})
        return HttpResponse(template.render(context), status=503)
Пример #2
0
def void(request):
    """ Returns RDF void data describing different datasets for
        Pelagious
    """
    p_void = PelagiosVoid()
    p_void.request = request
    p_void.make_graph()
    req_neg = RequestNegotiation('text/turtle')
    req_neg.supported_types = [
        'application/rdf+xml', 'text/n3', 'application/n-triples'
    ]
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            content_type = req_neg.use_response_type + '; charset=utf8'
            output = p_void.g.serialize(format=req_neg.use_response_type)
            return HttpResponse(output, content_type=content_type)
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type="text/plain; charset=utf8",
                                status=415)
    else:
        # default to outputting in turtle
        output = p_void.g.serialize(format='turtle')
        return HttpResponse(output, content_type='text/turtle; charset=utf8')
Пример #3
0
def html_view(request, uuid):
    request = RequestNegotiation().anonymize_request(request)
    # Handle some content negotiation for the item.
    req_neg = RequestNegotiation('text/html')
    req_neg.supported_types = []
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if not req_neg.supported:
        # The client may be wanting a non-HTML representation, so
        # use the following function to get it.
        return items_graph(request, uuid, item_type=ITEM_TYPE)
    ocitem = OCitem()
    ocitem.get_item(uuid)
    if not ocitem.manifest:
        # Did not find a record for the table, check for redirects
        r_url = RedirectURL()
        r_ok = r_url.get_direct_by_type_id(ITEM_TYPE, uuid)
        if r_ok:
            # found a redirect!!
            return redirect(r_url.redirect, permanent=r_url.permanent)
        # raise Http404
        raise Http404
    # check to see if there's related data via API calls. Add if so.
    request.uuid = ocitem.manifest.uuid
    request.project_uuid = ocitem.manifest.project_uuid
    request.item_type = ocitem.manifest.item_type
    subj_s = SubjectSupplement(ocitem.json_ld)
    ocitem.json_ld = subj_s.get_catal_related()
    rp = RootPath()
    base_url = rp.get_baseurl()
    temp_item = TemplateItem(request)
    temp_item.read_jsonld_dict(ocitem.json_ld)
    template = loader.get_template('subjects/view.html')
    if not temp_item.view_permitted:
        # The client is not allowed to see this.
        template = loader.get_template('items/view401.html')
        context = {
            'item': temp_item,
            'base_url': base_url,
            'user': request.user
        }
        return HttpResponse(template.render(context, request), status=401)
    # The client is allowd to see the current item.
    context = {'item': temp_item, 'base_url': base_url, 'user': request.user}
    response = HttpResponse(template.render(context, request))
    patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
    return response
Пример #4
0
def html_view(request, spatial_context=None):
    rp = RootPath()
    base_url = rp.get_baseurl()
    rd = RequestDict()
    request_dict_json = rd.make_request_dict_json(request,
                                                  spatial_context)
    url = request.get_full_path()
    if 'http://' not in url \
       and 'https://' not in url:
        url = base_url + url
    if '?' in url:
        json_url = url.replace('?', '.json?')
    else:
        json_url = url + '.json'
    solr_s = SolrSearch()
    if solr_s.solr is not False:
        response = solr_s.search_solr(request_dict_json)
        m_json_ld = MakeJsonLd(request_dict_json)
        # share entities already looked up. Saves database queries
        m_json_ld.entities = solr_s.entities
        m_json_ld.request_full_path = request.get_full_path()
        m_json_ld.spatial_context = spatial_context
        json_ld = m_json_ld.convert_solr_json(response.raw_content)
        req_neg = RequestNegotiation('text/html')
        req_neg.supported_types = ['application/json',
                                   'application/ld+json']
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if 'json' in req_neg.use_response_type:
            # content negotiation requested JSON or JSON-LD
            recon_obj = Reconciliation()
            json_ld = recon_obj.process(request.GET,
                                        json_ld)
            return HttpResponse(json.dumps(json_ld,
                                ensure_ascii=False, indent=4),
                                content_type=req_neg.use_response_type + "; charset=utf8")
        else:
            # now make the JSON-LD into an object suitable for HTML templating
            st = SearchTemplate(json_ld)
            st.process_json_ld()
            template = loader.get_template('sets/view.html')
            context = RequestContext(request,
                                     {'st': st,
                                      'url': url,
                                      'json_url': json_url,
                                      'base_url': base_url})
            if req_neg.supported:
                return HttpResponse(template.render(context))
            else:
                # client wanted a mimetype we don't support
                return HttpResponse(req_neg.error_message,
                                    content_type=req_neg.use_response_type + "; charset=utf8",
                                    status=415)
    else:
        template = loader.get_template('500.html')
        context = RequestContext(request,
                                 {'error': 'Solr Connection Problem'})
        return HttpResponse(template.render(context), status=503)
Пример #5
0
def index(request):
    """ redirects requests from the media index
        to the media-search view
    """
    request = RequestNegotiation().anonymize_request(request)
    rp = RootPath()
    base_url = rp.get_baseurl()
    new_url = base_url + '/search/?type=documents'
    return redirect(new_url, permanent=True)
Пример #6
0
def project_vocabs(request, uuid, return_media=None):
    """ Provides a RDF serialization, defaulting to a
        JSON-LD context for
        the data in a project. This will include
        a graph object that has annotations
        annotations of predicates and types
    """
    proj_context = ProjectContext(uuid, request)
    if 'hashes' in request.GET:
        proj_context.assertion_hashes = True
    if not proj_context.manifest:
        raise Http404
    req_neg = RequestNegotiation('application/json')
    req_neg.supported_types = ['application/ld+json']
    req_neg.supported_types += RDF_SERIALIZATIONS
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if return_media:
        req_neg.check_request_support(return_media)
        req_neg.use_response_type = return_media
    # Associate the request media type with the request so we can
    # make sure that different representations of this resource get different
    # cache responses.
    request.content_type = req_neg.use_response_type
    if not req_neg.supported:
        # client wanted a mimetype we don't support
        response = HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8",
                                status=415)
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response
    json_ld = proj_context.make_context_and_vocab_json_ld()
    # Check first if the output is requested to be an RDF format
    graph_output = graph_serialize(req_neg.use_response_type, json_ld)
    if graph_output:
        # Return with some sort of graph output
        response = HttpResponse(graph_output,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8")
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response
    # We're outputing JSON
    json_output = json.dumps(json_ld, indent=4, ensure_ascii=False)
    if 'callback' in request.GET:
        funct = request.GET['callback']
        response = HttpResponse(funct + '(' + json_output + ');',
                                content_type='application/javascript' +
                                "; charset=utf8")
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response
    else:
        response = HttpResponse(json_output,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8")
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response
Пример #7
0
def index(request):
    """ redirects requests from the projects index
        to the project-search view
    """
    request = RequestNegotiation().anonymize_request(request)
    rp = RootPath()
    base_url = rp.get_baseurl()
    new_url = base_url + '/projects-search/'
    return redirect(new_url, permanent=True)
Пример #8
0
def html_view(request, identifier):
    rp = RootPath()
    base_url = rp.get_baseurl()
    uri = 'http://opencontext.org/vocabularies/' + str(identifier)
    lequiv = LinkEquivalence()
    id_list = lequiv.get_identifier_list_variants(uri)
    lequiv = LinkEquivalence()
    id_s_list = lequiv.get_identifier_list_variants(uri + '/')
    for id_s in id_s_list:
        if id_s not in id_list:
            # add the slashed version to the list
            id_list.append(id_s)
    entity = False
    for test_id in id_list:
        ent = Entity()
        found = ent.dereference(test_id)
        if found is False:
            found = ent.dereference(test_id, test_id)
        if found:
            entity = ent
            break
    if entity is not False:
        t_vocab = TemplateVocab()
        t_vocab.create_template_for_entity(entity)
        t_vocab.make_json_for_html()
        req_neg = RequestNegotiation('text/html')
        req_neg.supported_types = ['application/ld+json', 'application/json']
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            if 'json' in req_neg.use_response_type:
                # content negotiation requested JSON or JSON-LD
                json_obj = t_vocab.make_json_obj()
                return HttpResponse(json.dumps(json_obj,
                                               ensure_ascii=False,
                                               indent=4),
                                    content_type=req_neg.use_response_type +
                                    "; charset=utf8")
            else:
                template = loader.get_template('vocabularies/view.html')
                context = {
                    'item': t_vocab,
                    'base_url': base_url,
                    'page_title': 'Open Context: Vocabularies + Ontologies',
                    'act_nav': 'vocabularies',
                    'nav_items': settings.NAV_ITEMS
                }
                return HttpResponse(template.render(context, request))
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type="text/plain; charset=utf8",
                                status=415)
    else:
        raise Http404
Пример #9
0
def project_annotations(request, identifier):
    """ Returns RDF open annotation assertions conforming to
        Pelagios specifications that link
        Open Context resources with place entities in
        gazetteers
    """
    ent = Entity()
    found = ent.dereference(identifier)
    if found or identifier == 'web':
        if ent.item_type == 'projects' or identifier == 'web':
            pelagios = PelagiosGraph()
            pelagios.test_limit = None
            if 'refresh' in request.GET:
                # we're going to refresh the cache
                pelagios.refresh_cache = True
            if identifier != 'web':
                pp = ProjectPermissions(ent.uuid)
                permitted = pp.view_allowed(request)
                pelagios.project_uuids = [ent.uuid]
            else:
                # we're doing web annotations
                pelagios.do_web_annotations = True
                permitted = True
            if permitted:
                pelagios.get_graph()
                req_neg = RequestNegotiation('text/turtle')
                req_neg.supported_types = [
                    'application/rdf+xml', 'text/n3', 'application/n-triples'
                ]
                if 'HTTP_ACCEPT' in request.META:
                    req_neg.check_request_support(request.META['HTTP_ACCEPT'])
                    if req_neg.supported:
                        content_type = req_neg.use_response_type + '; charset=utf8'
                        output = pelagios.g.serialize(
                            format=req_neg.use_response_type)
                        return HttpResponse(output, content_type=content_type)
                    else:
                        # client wanted a mimetype we don't support
                        return HttpResponse(
                            req_neg.error_message,
                            content_type="text/plain; charset=utf8",
                            status=415)
                else:
                    # default to outputting in turtle
                    output = pelagios.g.serialize(format='turtle')
                    return HttpResponse(
                        output, content_type='text/turtle; charset=utf8')
            else:
                return HttpResponse('Not authorized to get this resource',
                                    content_type='text/html; charset=utf8',
                                    status=401)
        else:
            found = False
    if found is False:
        raise Http404
Пример #10
0
def concepts_view(request):
    """ Get concepts overview """
    request = RequestNegotiation().anonymize_request(request)
    rp = RootPath()
    base_url = rp.get_baseurl()
    req_neg = RequestNegotiation('text/html')
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if req_neg.supported:
        # requester wanted a mimetype we DO support
        template = loader.get_template('about/temp.html')
        context = {
            'base_url': base_url,
            'page_title': 'Open Context: About - Concepts',
            'act_nav': 'about',
            'nav_items': settings.NAV_ITEMS
        }
        return HttpResponse(template.render(context))
    else:
        # client wanted a mimetype we don't support
        return HttpResponse(req_neg.error_message, status=415)
Пример #11
0
def old_redirect_view(request):
    """ Redirects from the original PHP version of
        Open Context when ".php" was in URLs
    """
    request = RequestNegotiation().anonymize_request(request)
    rp = RootPath()
    base_url = rp.get_baseurl()
    new_url = base_url + '/subjects-search/'
    if 'item' in request.GET:
        uuid = request.GET['item']
        new_url = base_url + '/subjects/' + uuid
    return redirect(new_url, permanent=True)
Пример #12
0
def html_view(request, uuid):
    request = RequestNegotiation().anonymize_request(request)
    # Handle some content negotiation for the item.
    req_neg = RequestNegotiation('text/html')
    req_neg.supported_types = []
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if not req_neg.supported:
        # The client may be wanting a non-HTML representation, so
        # use the following function to get it.
        return items_graph(request, uuid, item_type=ITEM_TYPE)
    if request.GET.get('new') is not None:
        return html_view_new(request, uuid)
    ocitem = OCitem()
    ocitem.get_item(uuid, True)
    if not ocitem.manifest:
        # Did not find a record for the table, check for redirects
        r_url = RedirectURL()
        r_ok = r_url.get_direct_by_type_id(ITEM_TYPE, uuid)
        if r_ok:
            # found a redirect!!
            return redirect(r_url.redirect, permanent=r_url.permanent)
        # raise Http404
        raise Http404
    # Construnct item the JSON-LD
    request.uuid = ocitem.manifest.uuid
    request.project_uuid = ocitem.manifest.project_uuid
    request.item_type = ocitem.manifest.item_type
    rp = RootPath()
    base_url = rp.get_baseurl()
    proj_content = ProjectContent(ocitem.manifest.uuid, ocitem.manifest.slug,
                                  ocitem.json_ld)
    temp_item = TemplateItem()
    temp_item.proj_content = proj_content.get_project_content()
    temp_item.read_jsonld_dict(ocitem.json_ld)
    template = loader.get_template('projects/view.html')
    context = {'item': temp_item, 'base_url': base_url, 'user': request.user}
    response = HttpResponse(template.render(context, request))
    patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
    return response
Пример #13
0
def html_view_new(request, uuid):
    request = RequestNegotiation().anonymize_request(request)
    # Handle some content negotiation for the item.
    req_neg = RequestNegotiation('text/html')
    req_neg.supported_types = []
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if not req_neg.supported:
        # The client may be wanting a non-HTML representation, so
        # use the following function to get it.
        return items_graph(request, uuid, item_type=ITEM_TYPE)
    # Proceed with constructing the HTML item
    ocitem = OCitemNew()
    if 'hashes' in request.GET:
        ocitem.assertion_hashes = True
    exists = ocitem.check_exists(uuid)
    if not exists:
        # Did not find a record for the table, check for redirects
        r_url = RedirectURL()
        r_ok = r_url.get_direct_by_type_id(ITEM_TYPE, uuid)
        if r_ok:
            # found a redirect!!
            return redirect(r_url.redirect, permanent=r_url.permanent)
        # raise Http404
        raise Http404
    # Construnct item the JSON-LD
    ocitem.generate_json_ld()
    rp = RootPath()
    base_url = rp.get_baseurl()
    proj_content = ProjectContent(ocitem.manifest.uuid, ocitem.manifest.slug,
                                  ocitem.json_ld)
    html_temp = HTMLtemplate()
    html_temp.proj_context_json_ld = ocitem.proj_context_json_ld
    html_temp.proj_content = proj_content.get_project_content()
    html_temp.read_jsonld_dict(ocitem.json_ld)
    template = loader.get_template('projects/view.html')
    context = {'item': html_temp, 'base_url': base_url, 'user': request.user}
    response = HttpResponse(template.render(context, request))
    patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
    return response
Пример #14
0
def csv_view(request, table_id):
    request = RequestNegotiation().anonymize_request(request)
    exp_tt = ExpTableTemplating(table_id)
    if exp_tt.exp_tab is not False:
        exp_tt.prep_csv()
        req_neg = RequestNegotiation('text/csv')
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            if isinstance(exp_tt.csv_url, str):
                return redirect(exp_tt.csv_url, permanent=False)
            else:
                dump = CSVdump()
                return dump.web_dump(exp_tt.table_id)
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8",
                                status=415)
    else:
        raise Http404
Пример #15
0
def index_view(request):
    """ Get the search context JSON-LD """
    request = RequestNegotiation().anonymize_request(request)
    rp = RootPath()
    base_url = rp.get_baseurl()
    req_neg = RequestNegotiation('text/html')
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if req_neg.supported:
        # requester wanted a mimetype we DO support
        open_graph = {
            'twitter_site':
            settings.TWITTER_SITE,
            'type':
            'website',
            'url':
            base_url + '/about/',
            'site_name':
            settings.CANONICAL_SITENAME,
            'description':
            'Video and introduction to Open Context, an open-access '
            'data publication service for archaeology ',
            'image':
            base_url + '/static/oc/images/index/oc-blue-square-logo.png',
            'video':
            'https://opencontext.wistia.com/medias/s0g0fsyqkz'
        }
        template = loader.get_template('about/index.html')
        context = {
            'base_url': base_url,
            'page_title': 'Open Context: About',
            'act_nav': 'about',
            'og': open_graph,
            'nav_items': settings.NAV_ITEMS
        }
        return HttpResponse(template.render(context, request))
    else:
        # client wanted a mimetype we don't support
        return HttpResponse(req_neg.error_message, status=415)
Пример #16
0
def index_view(request, table_id=None):
    """ Get the search context JSON-LD """
    request = RequestNegotiation().anonymize_request(request)
    rp = RootPath()
    base_url = rp.get_baseurl()
    req_neg = RequestNegotiation('text/html')
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if req_neg.supported:
        # requester wanted a mimetype we DO support
        template = loader.get_template('tables/index.html')
        context = {
            'base_url': base_url,
            'page_title': 'Open Context: Tables',
            'act_nav': 'tables',
            'nav_items': settings.NAV_ITEMS,
            'user': request.user
        }
        return HttpResponse(template.render(context, request))
    else:
        # client wanted a mimetype we don't support
        return HttpResponse(req_neg.error_message, status=415)
Пример #17
0
def html_view(request, uuid):
    ocitem = OCitem()
    ocitem.get_item(uuid)
    if (ocitem.manifest is not False):
        rp = RootPath()
        base_url = rp.get_baseurl()
        ts = TypeSupplement(ocitem.json_ld)
        ocitem.json_ld = ts.get_arachne_comparanda()
        temp_item = TemplateItem(request)
        temp_item.read_jsonld_dict(ocitem.json_ld)
        template = loader.get_template('types/view.html')
        if temp_item.view_permitted:
            req_neg = RequestNegotiation('text/html')
            req_neg.supported_types = [
                'application/json', 'application/ld+json'
            ]
            if 'HTTP_ACCEPT' in request.META:
                req_neg.check_request_support(request.META['HTTP_ACCEPT'])
            if req_neg.supported:
                if 'json' in req_neg.use_response_type:
                    # content negotiation requested JSON or JSON-LD
                    return HttpResponse(
                        json.dumps(ocitem.json_ld,
                                   ensure_ascii=False,
                                   indent=4),
                        content_type=req_neg.use_response_type +
                        "; charset=utf8")
                else:
                    context = RequestContext(request, {
                        'item': temp_item,
                        'base_url': base_url
                    })
                    return HttpResponse(template.render(context))
            else:
                # client wanted a mimetype we don't support
                return HttpResponse(req_neg.error_message,
                                    content_type=req_neg.use_response_type +
                                    "; charset=utf8",
                                    status=415)
        else:
            template = loader.get_template('items/view401.html')
            context = RequestContext(request, {
                'item': temp_item,
                'base_url': base_url
            })
            return HttpResponse(template.render(context), status=401)
    else:
        raise Http404
Пример #18
0
def lightbox_view(request, spatial_context=''):
    """ redirects requests from the legacy site 'lightbox'
        to the media-search view

        We can add URL parameter mappings to this later
        so that old url parameters can be mapped to the
        current parameters
    """
    request = RequestNegotiation().anonymize_request(request)
    url = request.get_full_path()
    new_url = url.replace('/lightbox/', '/media-search/')
    param_suffix = ''
    if '?' in url:
        url_ex = url.split('?')
        param_suffix = '?' + url_ex[1]
    return redirect(new_url, permanent=True)
Пример #19
0
def index(request):
    """ Get the item context JSON-LD """
    oai_obj = OAIpmh()
    req_neg = RequestNegotiation('application/xml')
    req_neg.supported_types = ['application/xml']
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if req_neg.supported:
        # requester wanted a mimetype we DO support
        xml = oai_obj.process_request(request)
        return HttpResponse(oai_obj.output_xml_string(),
                            content_type=req_neg.use_response_type + "; charset=utf8",
                            status=oai_obj.http_resp_code)
    else:
        # client wanted a mimetype we don't support
        return HttpResponse(req_neg.error_message,
                            status=415)
Пример #20
0
def search_view(request):
    """ Get the search context JSON-LD """
    search_context_obj = SearchContext()
    json_ld = LastUpdatedOrderedDict()
    json_ld['@context'] = search_context_obj.context
    req_neg = RequestNegotiation('application/json')
    req_neg.supported_types = ['application/ld+json']
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if req_neg.supported:
        # requester wanted a mimetype we DO support
        return HttpResponse(json.dumps(json_ld, ensure_ascii=False, indent=4),
                            content_type=req_neg.use_response_type +
                            "; charset=utf8")
    else:
        # client wanted a mimetype we don't support
        return HttpResponse(req_neg.error_message, status=415)
Пример #21
0
def query_html(request, spatial_context=None):
    """HTML representation for searching Open Context """

    request_dict = utilities.make_request_obj_dict(
        request, spatial_context=spatial_context)
    response_dict = process_solr_query(request_dict.copy())

    req_neg = RequestNegotiation('text/html')
    req_neg.supported_types = [
        'application/json',
        'application/ld+json',
    ]

    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])

    # Associate the request media type with the request so we can
    # make sure that different representations of this resource get different
    # cache responses.
    request.content_type = req_neg.use_response_type
    if not req_neg.supported:
        # Client wanted a mimetype we don't support
        response = HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8",
                                status=415)
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response

    if req_neg.use_response_type.endswith('json'):
        return make_json_response(request, req_neg, response_dict)

    rp = RootPath()
    # Disable the search template and just use vue with the JSON
    # API.
    # search_temp = SearchTemplate(response_dict.copy())
    context = {
        'st': response_dict.copy(),
        'base_url': rp.get_baseurl(),
        'api_url': response_dict.get('id'),
        'configs': configs,
    }
    template = loader.get_template('search_vue/view.html')
    response = HttpResponse(template.render(context, request))
    patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
    return response
Пример #22
0
def index_json(request):
    spatial_context = None
    rp = RootPath()
    base_url = rp.get_baseurl()
    rd = RequestDict()
    request_dict_json = rd.make_request_dict_json(request, spatial_context)
    url = request.get_full_path()
    if 'http://' not in url \
       and 'https://' not in url:
        url = base_url + url
    if '?' in url:
        json_url = url.replace('?', '.json?')
    else:
        json_url = url + '.json'
    json_url = json_url.replace('/projects/.json', '')
    solr_s = SolrSearch()
    solr_s.do_context_paths = False
    solr_s.item_type_limit = 'projects'
    if solr_s.solr is not False:
        response = solr_s.search_solr(request_dict_json)
        m_json_ld = MakeJsonLd(request_dict_json)
        m_json_ld.base_search_link = '/projects/'
        # share entities already looked up. Saves database queries
        m_json_ld.entities = solr_s.entities
        m_json_ld.request_full_path = request.get_full_path()
        m_json_ld.spatial_context = spatial_context
        json_ld = m_json_ld.convert_solr_json(response.raw_content)
        req_neg = RequestNegotiation('application/json')
        req_neg.supported_types = ['application/ld+json']
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            # requester wanted a mimetype we DO support
            return HttpResponse(json.dumps(json_ld,
                                           ensure_ascii=False,
                                           indent=4),
                                content_type=req_neg.use_response_type +
                                "; charset=utf8")
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message, status=415)
    else:
        template = loader.get_template('500.html')
        context = RequestContext(request, {'error': 'Solr Connection Problem'})
        return HttpResponse(template.render(context), status=503)
Пример #23
0
def json_view(request, identifier):
    rp = RootPath()
    base_url = rp.get_baseurl()
    uri = 'http://opencontext.org/vocabularies/' + str(identifier)
    lequiv = LinkEquivalence()
    id_list = lequiv.get_identifier_list_variants(uri)
    lequiv = LinkEquivalence()
    id_s_list = lequiv.get_identifier_list_variants(uri + '/')
    for id_s in id_s_list:
        if id_s not in id_list:
            # add the slashed version to the list
            id_list.append(id_s)
    entity = False
    for test_id in id_list:
        ent = Entity()
        found = ent.dereference(test_id)
        if found is False:
            found = ent.dereference(test_id, test_id)
        if found:
            entity = ent
            break
    if entity is not False:
        t_vocab = TemplateVocab()
        t_vocab.create_template_for_entity(entity)
        json_obj = t_vocab.make_json_obj()
        req_neg = RequestNegotiation('application/ld+json')
        req_neg.supported_types = ['application/json']
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            return HttpResponse(json.dumps(json_obj,
                                           ensure_ascii=False,
                                           indent=4),
                                content_type=req_neg.use_response_type +
                                "; charset=utf8")
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type="text/plain; charset=utf8",
                                status=415)
    else:
        raise Http404
Пример #24
0
def json_view(request, table_id):
    exp_tt = ExpTableTemplating(table_id)
    if exp_tt.exp_tab is not False:
        json_ld = exp_tt.make_json_ld()
        req_neg = RequestNegotiation('application/json')
        req_neg.supported_types = ['application/ld+json']
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            json_output = json.dumps(json_ld, indent=4, ensure_ascii=False)
            return HttpResponse(json_output,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8")
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8",
                                status=415)
    else:
        raise Http404
Пример #25
0
def recipes_view(request):
    """ Get the search context JSON-LD """
    rp = RootPath()
    base_url = rp.get_baseurl()
    req_neg = RequestNegotiation('text/html')
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if req_neg.supported:
        # requester wanted a mimetype we DO support
        template = loader.get_template('about/recipes.html')
        context = RequestContext(
            request, {
                'base_url': base_url,
                'page_title': 'Open Context: About - API Cookbook',
                'act_nav': 'about',
                'nav_items': settings.NAV_ITEMS
            })
        return HttpResponse(template.render(context))
    else:
        # client wanted a mimetype we don't support
        return HttpResponse(req_neg.error_message, status=415)
Пример #26
0
def json_view(request, uuid):
    ocitem = OCitem()
    ocitem.get_item(uuid)
    if(ocitem.manifest is not False):
        req_neg = RequestNegotiation('application/json')
        req_neg.supported_types = ['application/ld+json']
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            json_output = json.dumps(ocitem.json_ld,
                                     indent=4,
                                     ensure_ascii=False)
            return HttpResponse(json_output,
                                content_type=req_neg.use_response_type + "; charset=utf8")
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type + "; charset=utf8",
                                status=415)
    else:
        raise Http404
Пример #27
0
def json_view_old(request, uuid):
    """ returns a json representation """
    ocitem = OCitem()
    if 'hashes' in request.GET:
        ocitem.assertion_hashes = True
    ocitem.get_item(uuid)
    if ocitem.manifest is not False:
        request.uuid = ocitem.manifest.uuid
        request.project_uuid = ocitem.manifest.project_uuid
        request.item_type = ocitem.manifest.item_type
        req_neg = RequestNegotiation('application/json')
        req_neg.supported_types = [
            'application/ld+json', 'application/vnd.geo+json'
        ]
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            request.content_type = req_neg.use_response_type
            json_output = json.dumps(ocitem.json_ld,
                                     indent=4,
                                     ensure_ascii=False)
            if 'callback' in request.GET:
                funct = request.GET['callback']
                return HttpResponse(funct + '(' + json_output + ');',
                                    content_type='application/javascript' +
                                    "; charset=utf8")
            else:
                return HttpResponse(json_output,
                                    content_type=req_neg.use_response_type +
                                    "; charset=utf8")
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8",
                                status=415)
    else:
        raise Http404
Пример #28
0
def projects_json(request, uuid):
    """ provides a JSON-LD context for
        the predicates used in a project.
        
        DEPRECATE THIS!
    """
    proj_context = ProjectContext(uuid, request)
    if 'hashes' in request.GET:
        proj_context.assertion_hashes = True
    if proj_context.manifest is not False:
        req_neg = RequestNegotiation('application/json')
        req_neg.supported_types = [
            'application/ld+json', 'application/vnd.geo+json'
        ]
        if 'HTTP_ACCEPT' in request.META:
            req_neg.check_request_support(request.META['HTTP_ACCEPT'])
        if req_neg.supported:
            json_ld = proj_context.make_context_json_ld()
            json_output = json.dumps(json_ld, indent=4, ensure_ascii=False)
            if 'callback' in request.GET:
                funct = request.GET['callback']
                return HttpResponse(funct + '(' + json_output + ');',
                                    content_type='application/javascript' +
                                    "; charset=utf8")
            else:
                return HttpResponse(json_output,
                                    content_type=req_neg.use_response_type +
                                    "; charset=utf8")
        else:
            # client wanted a mimetype we don't support
            return HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8",
                                status=415)
    else:
        raise Http404
Пример #29
0
def index(request):
    request = RequestNegotiation().anonymize_request(request)
    rp = RootPath()
    base_url = rp.get_baseurl()
    new_url = base_url + '/search/?type=types'
    return redirect(new_url, permanent=True)
Пример #30
0
def items_graph(request, identifier, return_media=None, item_type=None):
    # The new Open Context OCitem generator
    # that better integrates caching
    oc_item = OCitem()
    if 'hashes' in request.GET:
        oc_item.assertion_hashes = True
    if not oc_item.check_exists(identifier):
        # Did not find a record for the table, check for redirects
        r_ok = False
        if item_type:
            r_url = RedirectURL()
            r_ok = r_url.get_direct_by_type_id(item_type, identifier)
        if r_ok:
            # found a redirect!!
            return redirect(r_url.redirect, permanent=r_url.permanent)
        # raise Http404
        raise Http404
    if item_type and item_type != oc_item.manifest.item_type:
        # We have a rare case where the item_type is wrong, even though we found
        # something in the manifest, so throw an error.
        raise Http404
    oc_item.generate_json_ld()
    req_neg = RequestNegotiation('application/json')
    req_neg.supported_types = ['application/ld+json']
    if (not item_type
            or item_type not in ['persons', 'types', 'predicates', 'tables']):
        # We don't have specified item type, or the item_type is
        # not for a resource that's lacking a geospatial component. Therefore,
        # support GeoJSON as a media type.
        req_neg.supported_types.append('application/vnd.geo+json')
    req_neg.supported_types += RDF_SERIALIZATIONS
    if 'HTTP_ACCEPT' in request.META:
        req_neg.check_request_support(request.META['HTTP_ACCEPT'])
    if return_media:
        req_neg.check_request_support(return_media)
        req_neg.use_response_type = return_media
    # Associate the request media type with the request so we can
    # make sure that different representations of this resource get different
    # cache responses.
    request.content_type = req_neg.use_response_type
    if not req_neg.supported:
        # client wanted a mimetype we don't support
        response = HttpResponse(req_neg.error_message,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8",
                                status=415)
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response
    # Check first if the output is requested to be an RDF format
    graph_output = None
    if req_neg.use_response_type in RDF_SERIALIZATIONS:
        json_ld = oc_item.json_ld
        # We're making an RDF graph serialization, so consolidate all the
        # context resources so we don't have to make Web requests to generate
        # the graph
        consolidated_contexts = consolidate_contexts(oc_item.json_ld)
        json_ld['@context'] = consolidated_contexts
        # Now make and serialize the graph
        graph_output = graph_serialize(req_neg.use_response_type, json_ld)
    if graph_output:
        # Return with some sort of graph output
        response = HttpResponse(graph_output,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8")
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response
    # We're outputing JSON
    if (req_neg.use_response_type == 'application/ld+json'
            or return_media == 'application/ld+json'):
        # A hack to remove non-point features so JSON-LD will validate.
        json_ld = strip_non_point_features(oc_item.json_ld)
    else:
        json_ld = oc_item.json_ld
    json_output = json.dumps(json_ld, indent=4, ensure_ascii=False)
    if 'callback' in request.GET:
        funct = request.GET['callback']
        response = HttpResponse(funct + '(' + json_output + ');',
                                content_type='application/javascript' +
                                "; charset=utf8")
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response
    else:
        response = HttpResponse(json_output,
                                content_type=req_neg.use_response_type +
                                "; charset=utf8")
        patch_vary_headers(response, ['accept', 'Accept', 'content-type'])
        return response