本文整理匯總了Python中flask.request.accept_mimetypes方法的典型用法代碼示例。如果您正苦於以下問題:Python request.accept_mimetypes方法的具體用法?Python request.accept_mimetypes怎麽用?Python request.accept_mimetypes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類flask.request
的用法示例。
在下文中一共展示了request.accept_mimetypes方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: request_wants_html
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def request_wants_html(self):
best = request.accept_mimetypes.best_match(["application/json", "text/html"])
return (
best == "text/html"
and request.accept_mimetypes[best]
> request.accept_mimetypes["application/json"]
)
示例2: _accepts
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def _accepts(self, mime):
types = request.accept_mimetypes
best = types.best_match([mime, 'text/html'])
return best == mime and types[best] > types['text/html']
示例3: wants_json_response
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def wants_json_response():
"""Data type json response request."""
return request.accept_mimetypes['application/json'] >= request.accept_mimetypes['text/html']
示例4: download
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def download(package):
release = package.getDownloadRelease()
if release is None:
if "application/zip" in request.accept_mimetypes and \
not "text/html" in request.accept_mimetypes:
return "", 204
else:
flash("No download available.", "danger")
return redirect(package.getDetailsURL())
else:
return redirect(release.getDownloadURL(), code=302)
示例5: shouldReturnJson
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def shouldReturnJson():
return "application/json" in request.accept_mimetypes and \
not "text/html" in request.accept_mimetypes
示例6: render
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def render(obj, template=None):
mimetypes = request.accept_mimetypes
best = mimetypes.best_match(['text/html', 'application/json'], 'application/json')
if best == 'application/json':
json_obj = recursive_encoder(obj)
return jsonify(json_obj)
return render_template(template, data=obj)
示例7: _rewrite_schema_if_necessary
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def _rewrite_schema_if_necessary(namespace_name, repo_name, tag_name, manifest):
# As per the Docker protocol, if the manifest is not schema version 1 and the manifest's
# media type is not in the Accept header, we return a schema 1 version of the manifest for
# the amd64+linux platform, if any, or None if none.
# See: https://docs.docker.com/registry/spec/manifest-v2-2
mimetypes = [mimetype for mimetype, _ in request.accept_mimetypes]
if manifest.media_type in mimetypes:
return manifest.internal_manifest_bytes, manifest.digest, manifest.media_type
# Short-circuit check: If the mimetypes is empty or just `application/json`, verify we have
# a schema 1 manifest and return it.
if not mimetypes or mimetypes == ["application/json"]:
if manifest.media_type in DOCKER_SCHEMA1_CONTENT_TYPES:
return manifest.internal_manifest_bytes, manifest.digest, manifest.media_type
logger.debug(
"Manifest `%s` not compatible against %s; checking for conversion",
manifest.digest,
request.accept_mimetypes,
)
converted = registry_model.convert_manifest(
manifest, namespace_name, repo_name, tag_name, mimetypes, storage
)
if converted is not None:
return converted.bytes, converted.digest, converted.media_type
# For back-compat, we always default to schema 1 if the manifest could not be converted.
schema1 = registry_model.get_schema1_parsed_manifest(
manifest, namespace_name, repo_name, tag_name, storage
)
if schema1 is None:
return None, None, None
return schema1.bytes, schema1.digest, schema1.media_type
示例8: _doesnt_accept_schema_v1
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def _doesnt_accept_schema_v1():
# If the client doesn't specify anything, still give them Schema v1.
return (
len(request.accept_mimetypes) != 0
and DOCKER_SCHEMA1_MANIFEST_CONTENT_TYPE not in request.accept_mimetypes
)
示例9: is_accept_json
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def is_accept_json(or_post=True):
"""
用於確定是否返回 JSON 響應體
替代 request.xhr
:type or_post: bool, True 時 POST 請求也返回真
:return: bool
"""
return 'application/json' in str(request.accept_mimetypes) or \
request.environ.get('HTTP_X_REQUESTED_WITH', '').lower() == 'xmlhttprequest' or \
or_post and request.method == 'POST'
示例10: request_wants_json
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def request_wants_json():
best = request.accept_mimetypes \
.best_match(['application/json',
'application/json; charset=utf-8',
'text/html'])
return best == 'application/json' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['text/html']
示例11: get_response_mimetype
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def get_response_mimetype(self, response, accept=None, request=request):
if accept is None:
if request and has_request_context():
accept = map(itemgetter(0), request.accept_mimetypes)
return super(API, self).get_response_mimetype(response, accept)
示例12: wants_json_response
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def wants_json_response():
return request.accept_mimetypes['application/json'] >= request.accept_mimetypes['text/html']
示例13: request_wants_html
# 需要導入模塊: from flask import request [as 別名]
# 或者: from flask.request import accept_mimetypes [as 別名]
def request_wants_html(self):
best = request.accept_mimetypes \
.best_match(['application/json', 'text/html'])
return best == 'text/html' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['application/json']