Created
November 3, 2012 20:15
-
-
Save idan/4008567 to your computer and use it in GitHub Desktop.
Protocol-Relative S3BotoStorage for django-storages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from urlparse import urlsplit, urlunsplit | |
| from storages.backends.s3boto import S3BotoStorage | |
| class ProtocolRelativeS3BotoStorage(S3BotoStorage): | |
| """Extends S3BotoStorage to return protocol-relative URLs | |
| See: http://paulirish.com/2010/the-protocol-relative-url/ | |
| """ | |
| def url(self, name): | |
| """Modifies return URLs to be protocol-relative.""" | |
| url = super(ProtocolRelativeS3BotoStorage, self).url(name) | |
| parts = list(urlsplit(url)) | |
| parts[0] = '' | |
| return urlunsplit(parts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The result of
urlsplitis a namedtuple, so you can use its_replacemethod.It also has a
geturlmethod which saves you from usingurlunsplit: