Django Friends Meetup, 2015-02-05
... or testing with settings override
Florian Demmer (@fdemmer)
www.floriandemmer.com
http://en.wikipedia.org/wiki/POODLE
We have disabled SSLv3 for all customers
who use SSL with the default CloudFront
domain name (*.cloudfront.net).
http://aws.amazon.com/security/security-bulletins/CVE-2014-3566-advisory/
don't use HTTPS for SmartTV API
... but still use it on web frontend.
... and also better use protocol relative URLs.
(but not in API)
CDN_URL = '//da2s5d7fli.cloudfront.net'
def rewrite_protocol(url, replacement=None):
replacement = replacement or settings.STV_API_MEDIA_PROTOCOL
if replacement:
parts = urlparse.urlsplit(url)
# only rewrite protocol if host is available
if parts[1]:
return urlparse.urlunsplit((replacement,) + parts[1:])
return url
class APITest(testcases.TestCase):
def test_http_location(self):
CDN_URL = 'http://cloudfront.com'
MEDIA_URL = '/media/'
with self.settings(MEDIA_URL=CDN_URL+MEDIA_URL):
...
happens at runtime
"ImageFields" use storage to generate URLs
should affect the "DefaultStorage" all "ImageFields" use
but "DefaultStorage" loads setting at init
how about a signal on setting change?
https://code.djangoproject.com/ticket/17744
@receiver(setting_changed)
def file_storage_changed(**kwargs):
if kwargs['setting'] in ('MEDIA_ROOT', 'DEFAULT_FILE_STORAGE'):
from django.core.files.storage import default_storage
default_storage._wrapped = empty
... no trigger on MEDIA_URL change.
eg. easy_thumbnails
quick solution in test class:
def _nuke_storage_instances(self):
# reset image storages to reload settings
from django.core.files.storage import default_storage
default_storage._wrapped = empty
from easy_thumbnails.storage import thumbnail_default_storage
thumbnail_default_storage._wrapped = empty
https://code.djangoproject.com/ticket/17744
https://github.com/django/django/commit/a24ffa52d02998f5082fbc5a461ccdc8004db4a6