def index_atom(request): """ Get the search context JSON-LD """ mf = ManifestFeed() xml_string = mf.make_feed(request.GET) if xml_string is not False: req_neg = RequestNegotiation('application/atom+xml') req_neg.supported_types = ['application/atom+xml'] if 'HTTP_ACCEPT' in request.META: req_neg.check_request_support(request.META['HTTP_ACCEPT']) if req_neg.supported: if 'atom' in req_neg.use_response_type: # content negotiation requested Atom return HttpResponse(xml_string, content_type=req_neg.use_response_type + "; charset=utf8") else: # give atom anyway return HttpResponse(xml_string, content_type='application/atom+xml' + "; charset=utf8") else: # client wanted a mimetype we don't support return HttpResponse(req_neg.error_message, content_type='text/html' + "; charset=utf8", status=415) else: # no feed of this page or type raise Http404
def json_view(request, uuid): ocitem = OCitem() if 'hashes' in request.GET: ocitem.assertion_hashes = True ocitem.get_item(uuid, True) if(ocitem.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_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
def query_json(request, spatial_context=None): """ API for searching Open Context """ request_dict = utilities.make_request_obj_dict( request, spatial_context=spatial_context) response_dict = process_solr_query(request_dict) 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']) # 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 return make_json_response(request, req_neg, response_dict)
def json_view(request, uuid): ocitem = OCitem() if 'hashes' in request.GET: ocitem.assertion_hashes = True ocitem.get_item(uuid, True) 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
def gazetteer(request): """ Returns RDF void data describing different datasets for Pelagious """ p_gg = PelagiosGazetteerGraph() p_gg.request = request if 'refresh' in request.GET: # we're going to refresh the cache p_gg.refresh_cache = True p_gg.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_gg.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_gg.g.serialize(format='turtle') return HttpResponse(output, content_type='text/turtle; charset=utf8')
def html_view(request, uuid): ocitem = OCitem() ocitem.get_item(uuid) if ocitem.manifest is not False: rp = RootPath() base_url = rp.get_baseurl() temp_item = TemplateItem(request) temp_item.read_jsonld_dict(ocitem.json_ld) template = loader.get_template("media/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, "fullview": False, "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
def html_view(request, uuid): ocitem = OCitem() ocitem.get_item(uuid, True) if (ocitem.manifest is not False): rp = RootPath() base_url = rp.get_baseurl() temp_item = TemplateItem() temp_item.read_jsonld_dict(ocitem.json_ld) template = loader.get_template('projects/view.html') 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: raise Http404
def projects_json(request, uuid): """ provides a JSON-LD context for the data in a project. This will include annotations of predicates and types """ 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
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)
def gazetteer(request): """ Returns RDF void data describing different datasets for Pelagious """ p_gg = PelagiosGazetteerGraph() p_gg.request = request if 'refresh' in request.GET: # we're going to refresh the cache p_gg.refresh_cache = True p_gg.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_gg.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_gg.g.serialize(format='turtle') return HttpResponse(output, content_type='text/turtle; charset=utf8')
def html_view(request, uuid): ocitem = OCitem() ocitem.get_item(uuid, True) if(ocitem.manifest is not False): rp = RootPath() base_url = rp.get_baseurl() temp_item = TemplateItem() temp_item.read_jsonld_dict(ocitem.json_ld) template = loader.get_template('projects/view.html') 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: raise Http404
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)
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
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
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
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) # Proceed with constructing the HTML item ocitem = OCitem() if 'hashes' in request.GET: ocitem.assertion_hashes = True 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 ts = TypeSupplement(ocitem.json_ld) ocitem.json_ld = ts.get_arachne_comparanda() rp = RootPath() base_url = rp.get_baseurl() temp_item = TemplateItem(request) temp_item.read_jsonld_dict(ocitem.json_ld) 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) # Now add templated item to the a response object template = loader.get_template('types/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
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
def html_view(request, uuid, full_view=False): 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) # Construnct the item 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 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() temp_item = TemplateItem(request) temp_item.read_jsonld_dict(ocitem.json_ld) if full_view: template = loader.get_template('media/full.html') else: template = loader.get_template('media/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) # Now add templated item to the a response object context = { 'item': temp_item, 'fullview': full_view, 'base_url': base_url, 'user': request.user } response = HttpResponse(template.render(context, request)) patch_vary_headers(response, ['accept', 'Accept', 'content-type']) return response
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
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)
def html_view(request, uuid): ocitem = OCitem() ocitem.get_item(uuid) if (ocitem.manifest is not False): # check to see if there's related data via API calls. Add if so. 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 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}) return HttpResponse(template.render(context), status=401) else: raise Http404
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
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)
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)
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)
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
def html_view(request, uuid): ocitem = OCitem() ocitem.get_item(uuid) if ocitem.manifest is not False: # check to see if there's related data via API calls. Add if so. 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 temp_item.view_permitted: req_neg = RequestNegotiation('text/html') req_neg.supported_types = ['application/json', '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: 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}) return HttpResponse(template.render(context), status=401) else: raise Http404
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
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
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
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
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
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
def projects_html_view(request, spatial_context=None): """ returns HTML representation of projects search """ mem_cache_obj = MemoryCache() mem_cache_obj.ping_redis_server() rp = RootPath() base_url = rp.get_baseurl() rd = RequestDict() request_dict_json = rd.make_request_dict_json(request, spatial_context) if rd.security_ok is False: template = loader.get_template('400.html') context = RequestContext(request, {'abusive': True}) return HttpResponse(template.render(context), status=400) elif rd.do_bot_limit: # redirect bot requests away from faceted search where # they can negatively impact performance cache_control(no_cache=True) return redirect('/projects-search/', permanent=False) else: # url and json_url neeed for view templating 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' # see if search results are cached. this is not done # with a view decorator, because we want to handle bots differently db_cache = DatabaseCache() cache_key = db_cache.make_cache_key('projects-search', request_dict_json) if rd.refresh_cache: # the request wanted to refresh the cache db_cache.remove_cache_object(cache_key) # get the search result JSON-LD, if it exists in cache json_ld = db_cache.get_cache_object(cache_key) if json_ld is None: # cached result is not found, so make it with a new search solr_s = SolrSearch() solr_s.is_bot = rd.is_bot # True if bot detected solr_s.do_bot_limit = rd.do_bot_limit # Toggle limits on facets for bots solr_s.mem_cache_obj = mem_cache_obj 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) mem_cache_obj = solr_s.mem_cache_obj # reused cached memory items m_json_ld = MakeJsonLd(request_dict_json) m_json_ld.base_search_link = '/projects-search/' # share entities already looked up. Saves database queries m_json_ld.mem_cache_obj = mem_cache_obj 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) # now cache the resulting JSON-LD db_cache.save_cache_object(cache_key, json_ld) if json_ld is not None: req_neg = RequestNegotiation('text/html') req_neg.supported_types = ['application/json', 'application/ld+json', 'application/vnd.geo+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() p_aug = ProjectAugment(json_ld) p_aug.process_json_ld() template = loader.get_template('search/view.html') context = RequestContext(request, {'st': st, 'item_type': 'projects', 'base_search_link': m_json_ld.base_search_link, 'p_aug': p_aug, '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: cache_control(no_cache=True) template = loader.get_template('500.html') context = RequestContext(request, {'error': 'Solr Connection Problem'}) return HttpResponse(template.render(context), status=503)
def projects_json_view(request, spatial_context=None): """ API for searching Open Context, media only """ mem_cache_obj = MemoryCache() mem_cache_obj.ping_redis_server() rd = RequestDict() request_dict_json = rd.make_request_dict_json(request, spatial_context) if rd.security_ok is False: template = loader.get_template('400.html') context = RequestContext(request, {'abusive': True}) return HttpResponse(template.render(context), status=400) elif rd.do_bot_limit: # redirect bot requests away from faceted search where # they can negatively impact performance cache_control(no_cache=True) return redirect('/projects-search/', permanent=False) else: # see if search results are cached. this is not done # with a view decorator, because we want to handle bots differently db_cache = DatabaseCache() cache_key = db_cache.make_cache_key('projects-search', request_dict_json) if rd.refresh_cache: # the request wanted to refresh the cache db_cache.remove_cache_object(cache_key) # get the search result JSON-LD, if it exists in cache json_ld = db_cache.get_cache_object(cache_key) if json_ld is None: # cached result is not found, so make it with a new search solr_s = SolrSearch() solr_s.is_bot = rd.is_bot # True if bot detected solr_s.do_bot_limit = rd.do_bot_limit # Toggle limits on facets for bots 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-search/' # 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) # now cache the resulting JSON-LD db_cache.save_cache_object(cache_key, json_ld) if json_ld is not None: 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: # requester wanted a mimetype we DO support if 'callback' in request.GET: funct = request.GET['callback'] json_str = json.dumps(json_ld, ensure_ascii=False, indent=4) return HttpResponse(funct + '(' + json_str + ');', content_type='application/javascript' + "; charset=utf8") else: 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: cache_control(no_cache=True) template = loader.get_template('500.html') context = RequestContext(request, {'error': 'Solr Connection Problem'}) return HttpResponse(template.render(context), status=503)
def html_view(request, table_id): request = RequestNegotiation().anonymize_request(request) exp_tt = ExpTableTemplating(table_id) rp = RootPath() base_url = rp.get_baseurl() if exp_tt.exp_tab is not False: exp_tt.prep_html() template = loader.get_template('tables/view.html') if exp_tt.view_permitted: req_neg = RequestNegotiation('text/html') req_neg.supported_types = [ 'application/json', 'application/ld+json', 'text/csv' ] 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") elif 'csv' in req_neg.use_response_type: return redirect(exp_tt.csv_url, permanent=False) else: context = { 'page_title': exp_tt.exp_tab.label, 'item': exp_tt, 'base_url': base_url, 'user': request.user } return HttpResponse(template.render(context, request)) 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 = { 'item': temp_item, 'base_url': base_url, 'user': request.user } return HttpResponse(template.render(context, request), status=401) else: # did not find a record for the table, check for redirects r_url = RedirectURL() r_ok = r_url.get_direct_by_type_id('tables', exp_tt.public_table_id) if r_ok: # found a redirect!! return redirect(r_url.redirect, permanent=r_url.permanent) else: # raise Http404 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))
def projects_json_view(request, spatial_context=None): """ API for searching Open Context, media only """ rd = RequestDict() request_dict_json = rd.make_request_dict_json(request, spatial_context) if rd.security_ok is False: template = loader.get_template('400.html') context = RequestContext(request, {'abusive': True}) return HttpResponse(template.render(context), status=400) elif rd.do_bot_limit: # redirect bot requests away from faceted search where # they can negatively impact performance cache_control(no_cache=True) return redirect('/projects-search/', permanent=False) else: # see if search results are cached. this is not done # with a view decorator, because we want to handle bots differently db_cache = DatabaseCache() cache_key = db_cache.make_cache_key('projects-search', request_dict_json) if rd.refresh_cache: # the request wanted to refresh the cache db_cache.remove_cache_object(cache_key) # get the search result JSON-LD, if it exists in cache json_ld = db_cache.get_cache_object(cache_key) if json_ld is None: # cached result is not found, so make it with a new search solr_s = SolrSearch() solr_s.is_bot = rd.is_bot # True if bot detected solr_s.do_bot_limit = rd.do_bot_limit # Toggle limits on facets for bots 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-search/' 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) # now cache the resulting JSON-LD db_cache.save_cache_object(cache_key, json_ld) if json_ld is not None: 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: # requester wanted a mimetype we DO support request.content_type = req_neg.use_response_type if 'callback' in request.GET: funct = request.GET['callback'] json_str = json.dumps(json_ld, ensure_ascii=False, indent=4) return HttpResponse(funct + '(' + json_str + ');', content_type='application/javascript' + "; charset=utf8") else: 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: cache_control(no_cache=True) template = loader.get_template('500.html') context = RequestContext(request, {'error': 'Solr Connection Problem'}) return HttpResponse(template.render(context), status=503)
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
def projects_html_view(request, spatial_context=None): """ returns HTML representation of projects search """ request = RequestNegotiation().anonymize_request(request) item_type_limited = True rp = RootPath() base_url = rp.get_baseurl() rd = RequestDict() request_dict_json = rd.make_request_dict_json(request, spatial_context) if rd.security_ok is False: template = loader.get_template('400.html') context = RequestContext(request, {'abusive': True}) return HttpResponse(template.render(context), status=400) elif rd.do_bot_limit: # redirect bot requests away from faceted search where # they can negatively impact performance cache_control(no_cache=True) return redirect('/projects-search/', permanent=False) else: # url and json_url neeed for view templating 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' # see if search results are cached. this is not done # with a view decorator, because we want to handle bots differently db_cache = DatabaseCache() cache_key = db_cache.make_cache_key('projects-search', request_dict_json) if rd.refresh_cache: # the request wanted to refresh the cache db_cache.remove_cache_object(cache_key) # get the search result JSON-LD, if it exists in cache json_ld = db_cache.get_cache_object(cache_key) if json_ld is None: # cached result is not found, so make it with a new search solr_s = SolrSearch() solr_s.is_bot = rd.is_bot # True if bot detected solr_s.do_bot_limit = rd.do_bot_limit # Toggle limits on facets for bots 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-search/' 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) # now cache the resulting JSON-LD db_cache.save_cache_object(cache_key, json_ld) if json_ld is not None: req_neg = RequestNegotiation('text/html') req_neg.supported_types = ['application/json', 'application/ld+json', 'application/vnd.geo+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 request.content_type = req_neg.use_response_type recon_obj = Reconciliation() json_ld = recon_obj.process(request.GET, json_ld) response = HttpResponse(json.dumps(json_ld, ensure_ascii=False, indent=4), content_type=req_neg.use_response_type + "; charset=utf8") patch_vary_headers(response, ['accept', 'Accept', 'content-type']) return response else: # now make the JSON-LD into an object suitable for HTML templating st = SearchTemplate(json_ld) st.item_type_limited = item_type_limited st.process_json_ld() p_aug = ProjectAugment(json_ld) p_aug.process_json_ld() template = loader.get_template('search/view.html') context = { 'st': st, 'item_type': 'projects', 'base_search_link': m_json_ld.base_search_link, 'p_aug': p_aug, 'url': url, 'json_url': json_url, 'base_url': base_url } if req_neg.supported: response = HttpResponse(template.render(context, request)) patch_vary_headers(response, ['accept', 'Accept', 'content-type']) return response 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: cache_control(no_cache=True) template = loader.get_template('500.html') context = RequestContext(request, {'error': 'Solr Connection Problem'}) return HttpResponse(template.render(context), status=503)
def html_view(request, spatial_context=None): request = RequestNegotiation().anonymize_request(request) item_type_limited = False rp = RootPath() base_url = rp.get_baseurl() rd = RequestDict() chart = False # provide a chart, now only experimental if request.GET.get('chart') is not None: chart = True request_dict_json = rd.make_request_dict_json(request, spatial_context) # toggle if Human-Remains are OK to show in search results # defaults to FALSE, requires user interface action to allow if request.GET.get('human-remains') is not None: human_remains_ok = True else: human_remains_ok = False human_remains_opt_in = request.session.get('human_remains_ok') if human_remains_opt_in: # opt-in OK for this user in this session human_remains_ok = True if rd.security_ok is False: # looks like an abusive SQL injection request template = loader.get_template('400.html') context = RequestContext(request, {'abusive': True}) return HttpResponse(template.render(context), status=400) elif rd.do_bot_limit: # redirect bot requests away from faceted search where # they can negatively impact performance cache_control(no_cache=True) return redirect('/search/', permanent=False) else: # url and json_url neeed for view templating 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' # see if search results are cached. this is not done # with a view decorator, because we want to handle bots differently db_cache = DatabaseCache() cache_key = db_cache.make_cache_key('search', request_dict_json) # print('Cache key: ' + cache_key) geo_proj = False json_ld = None if rd.refresh_cache: # the request wanted to refresh the cache db_cache.remove_cache_object(cache_key) if 'response' in request.GET: if 'geo-project' in request.GET['response']: geo_proj = True # get the search result JSON-LD, if it exists in cache json_ld = db_cache.get_cache_object(cache_key) if json_ld is None: # cached result is not found, so make it with a new search solr_s = SolrSearch() solr_s.is_bot = rd.is_bot # True if bot detected solr_s.do_bot_limit = rd.do_bot_limit # Toggle limits on facets for bots if solr_s.solr is not False: response = solr_s.search_solr(request_dict_json) # are we filtering for item_types? item_type_limited = solr_s.item_type_limited m_json_ld = MakeJsonLd(request_dict_json) m_json_ld.base_search_link = '/search/' 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) # now cache the resulting JSON-LD db_cache.save_cache_object(cache_key, json_ld) if json_ld is not None: req_neg = RequestNegotiation('text/html') req_neg.supported_types = ['application/json', 'application/ld+json', 'application/vnd.geo+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) request.content_type = req_neg.use_response_type response = HttpResponse(json.dumps(json_ld, ensure_ascii=False, indent=4), content_type=req_neg.use_response_type + "; charset=utf8") patch_vary_headers(response, ['accept', 'Accept', 'content-type']) return response else: # now make the JSON-LD into an object suitable for HTML templating st = SearchTemplate(json_ld) st.item_type_limited = item_type_limited st.human_remains_ok = human_remains_ok st.process_json_ld() props = [] if 'prop' in request.GET: props = request.GET.getlist('prop') # check to make sure chrono chart will be ok if 'proj' in request.GET \ or len(props) > 1 \ or 'q' in request.GET \ or spatial_context is not None: if 'oc-api:has-form-use-life-ranges' in json_ld: if len(json_ld['oc-api:has-form-use-life-ranges']) > 0 and st.total_count > 0: chart = True template = loader.get_template('search/view.html') context = { 'st': st, 'item_type': '*', 'chart': chart, 'human_remains_ok': human_remains_ok, 'base_search_link': m_json_ld.base_search_link, 'url': url, 'json_url': json_url, 'base_url': base_url } if req_neg.supported: response = HttpResponse(template.render(context, request)) patch_vary_headers(response, ['accept', 'Accept', 'content-type']) return response 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: cache_control(no_cache=True) template = loader.get_template('500.html') context = RequestContext(request, {'error': 'Solr Connection Problem'}) return HttpResponse(template.render(context), status=503)
def html_view(request, table_id): request = RequestNegotiation().anonymize_request(request) exp_tt = ExpTableTemplating(table_id) rp = RootPath() base_url = rp.get_baseurl() if exp_tt.exp_tab is not False: exp_tt.prep_html() template = loader.get_template('tables/view.html') if exp_tt.view_permitted: req_neg = RequestNegotiation('text/html') req_neg.supported_types = ['application/json', 'application/ld+json', 'text/csv'] 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") elif 'csv' in req_neg.use_response_type: return redirect(exp_tt.csv_url, permanent=False) else: context = { 'page_title': exp_tt.exp_tab.label, 'item': exp_tt, 'base_url': base_url, 'user': request.user } return HttpResponse(template.render(context, request)) 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 = { 'item': temp_item, 'base_url': base_url, 'user': request.user } return HttpResponse(template.render(context, request), status=401) else: # did not find a record for the table, check for redirects r_url = RedirectURL() r_ok = r_url.get_direct_by_type_id('tables', exp_tt.public_table_id) if r_ok: # found a redirect!! return redirect(r_url.redirect, permanent=r_url.permanent) else: # raise Http404 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))