Python flask.request.accept_mimetypes() Examples

The following are 13 code examples of flask.request.accept_mimetypes(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask.request , or try the search function .
Example #1
Source File: api.py    From yeti with Apache License 2.0 6 votes vote down vote up
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) 
Example #2
Source File: graphqlview.py    From graphql-server-core with MIT License 5 votes vote down vote up
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"]
        ) 
Example #3
Source File: views.py    From Bad-Tools with MIT License 5 votes vote down vote up
def _accepts(self, mime):
        types = request.accept_mimetypes
        best = types.best_match([mime, 'text/html'])

        return best == mime and types[best] > types['text/html'] 
Example #4
Source File: handlers.py    From AUCR with GNU General Public License v3.0 5 votes vote down vote up
def wants_json_response():
    """Data type json response request."""
    return request.accept_mimetypes['application/json'] >= request.accept_mimetypes['text/html'] 
Example #5
Source File: packages.py    From contentdb with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #6
Source File: utils.py    From contentdb with GNU General Public License v3.0 5 votes vote down vote up
def shouldReturnJson():
	return "application/json" in request.accept_mimetypes and \
			not "text/html" in request.accept_mimetypes 
Example #7
Source File: manifest.py    From quay with Apache License 2.0 5 votes vote down vote up
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 
Example #8
Source File: manifest.py    From quay with Apache License 2.0 5 votes vote down vote up
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
    ) 
Example #9
Source File: helper.py    From FF.PyAdmin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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' 
Example #10
Source File: home.py    From bluemix-python-eve-sample with Apache License 2.0 5 votes vote down vote up
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'] 
Example #11
Source File: flask_raml.py    From flask-raml with MIT License 5 votes vote down vote up
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) 
Example #12
Source File: handlers.py    From flicket with MIT License 5 votes vote down vote up
def wants_json_response():
    return request.accept_mimetypes['application/json'] >= request.accept_mimetypes['text/html'] 
Example #13
Source File: graphqlview.py    From flask-graphql with MIT License 5 votes vote down vote up
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']