本文整理汇总了Python中opencontext_py.libs.requestnegotiation.RequestNegotiation.content_type方法的典型用法代码示例。如果您正苦于以下问题:Python RequestNegotiation.content_type方法的具体用法?Python RequestNegotiation.content_type怎么用?Python RequestNegotiation.content_type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opencontext_py.libs.requestnegotiation.RequestNegotiation
的用法示例。
在下文中一共展示了RequestNegotiation.content_type方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: projects_html_view
# 需要导入模块: from opencontext_py.libs.requestnegotiation import RequestNegotiation [as 别名]
# 或者: from opencontext_py.libs.requestnegotiation.RequestNegotiation import content_type [as 别名]
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
#.........这里部分代码省略.........
示例2: html_view
# 需要导入模块: from opencontext_py.libs.requestnegotiation import RequestNegotiation [as 别名]
# 或者: from opencontext_py.libs.requestnegotiation.RequestNegotiation import content_type [as 别名]
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:
#.........这里部分代码省略.........