本文整理汇总了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']