Skip to content

Support relative flask endpoints. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions flask_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from flask import url_for as flask_url_for
from flask import current_app
from flask import _request_ctx_stack, request
from boto.s3.connection import S3Connection
from boto.s3 import connect_to_region
from boto.exception import S3CreateError, S3ResponseError
Expand All @@ -28,6 +29,33 @@ def hash_file(filename):
return hasher.hexdigest()


def support_relative_endpoints(endpoint):
"""Copied from Flask's url_for().
"""
reqctx = _request_ctx_stack.top

# If request specific information is available we have some extra
# features that support "relative" URLs.
if reqctx is not None:
url_adapter = reqctx.url_adapter
blueprint_name = request.blueprint
if not reqctx.request._is_old_module:
if endpoint[:1] == '.':
if blueprint_name is not None:
endpoint = blueprint_name + endpoint
else:
endpoint = endpoint[1:]
else:
# TODO: get rid of this deprecated functionality in 1.0
if '.' not in endpoint:
if blueprint_name is not None:
endpoint = blueprint_name + '.' + endpoint
elif endpoint.startswith('.'):
endpoint = endpoint[1:]

return endpoint


def url_for(endpoint, **values):
"""
Generates a URL to the given endpoint.
Expand Down Expand Up @@ -67,6 +95,7 @@ def url_for(endpoint, **values):
if app.config['S3_CDN_DOMAIN']:
bucket_path = '%s' % app.config['S3_CDN_DOMAIN']
urls = app.url_map.bind(bucket_path, url_scheme=scheme)
endpoint = support_relative_endpoints(endpoint)
return urls.build(endpoint, values=values, force_external=True)
return flask_url_for(endpoint, **values)

Expand Down