From 96eb16cd729ce65df69b3f70d4b8d95341d8a650 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 6 Feb 2013 10:08:20 -0500 Subject: [PATCH 1/2] Make local dev work by trying checking staticfiles_storage for both the course_prefixed and non-course_prefixed urls --- common/djangoapps/static_replace/__init__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/common/djangoapps/static_replace/__init__.py b/common/djangoapps/static_replace/__init__.py index cfef798bdf..4833f5ef96 100644 --- a/common/djangoapps/static_replace/__init__.py +++ b/common/djangoapps/static_replace/__init__.py @@ -76,18 +76,19 @@ def replace_static_urls(text, data_directory, course_namespace=None): # course_namespace is not None, then use studio style urls if course_namespace is not None and not isinstance(modulestore(), XMLModuleStore): url = StaticContent.convert_legacy_static_url(rest, course_namespace) - # If we're in debug mode, and the file as requested exists, then don't change the links - elif (settings.DEBUG and finders.find(rest, True)): - return original - # Otherwise, look the file up in staticfiles_storage without the data directory + # Otherwise, look the file up in staticfiles_storage, and append the data directory if needed else: + course_path = "/".join((data_directory, rest)) try: - url = staticfiles_storage.url(rest) + if staticfiles_storage.exists(rest): + url = staticfiles_storage.url(rest) + else: + url = staticfiles_storage.url(course_path) # And if that fails, assume that it's course content, and add manually data directory except Exception as err: log.warning("staticfiles_storage couldn't find path {0}: {1}".format( rest, str(err))) - url = "".join([prefix, data_directory, '/', rest]) + url = "".join([prefix, course_path]) return "".join([quote, url, quote]) From 2302b40022384b37a82c59824f9c313920d7a6cb Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 6 Feb 2013 10:33:36 -0500 Subject: [PATCH 2/2] Fix static_replace tests --- .../test/test_static_replace.py | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/common/djangoapps/static_replace/test/test_static_replace.py b/common/djangoapps/static_replace/test/test_static_replace.py index e08c66c59f..63fc3eaa6a 100644 --- a/common/djangoapps/static_replace/test/test_static_replace.py +++ b/common/djangoapps/static_replace/test/test_static_replace.py @@ -24,15 +24,24 @@ def test_multi_replace(): ) -@patch('static_replace.finders') -@patch('static_replace.settings') -def test_debug_no_modify(mock_settings, mock_finders): - mock_settings.DEBUG = True - mock_finders.find.return_value = True +@patch('static_replace.staticfiles_storage') +def test_storage_url_exists(mock_storage): + mock_storage.exists.return_value = True + mock_storage.url.return_value = '/static/file.png' - assert_equals(STATIC_SOURCE, replace_static_urls(STATIC_SOURCE, DATA_DIRECTORY)) + assert_equals('"/static/file.png"', replace_static_urls(STATIC_SOURCE, DATA_DIRECTORY)) + mock_storage.exists.called_once_with('file.png') + mock_storage.url.called_once_with('data_dir/file.png') - mock_finders.find.assert_called_once_with('file.png', True) + +@patch('static_replace.staticfiles_storage') +def test_storage_url_not_exists(mock_storage): + mock_storage.exists.return_value = False + mock_storage.url.return_value = '/static/data_dir/file.png' + + assert_equals('"/static/data_dir/file.png"', replace_static_urls(STATIC_SOURCE, DATA_DIRECTORY)) + mock_storage.exists.called_once_with('file.png') + mock_storage.url.called_once_with('file.png') @patch('static_replace.StaticContent') @@ -58,7 +67,10 @@ def test_mongo_filestore(mock_modulestore, mock_static_content): @patch('static_replace.staticfiles_storage') def test_data_dir_fallback(mock_storage, mock_modulestore, mock_settings): mock_modulestore.return_value = Mock(XMLModuleStore) - mock_settings.DEBUG = False mock_storage.url.side_effect = Exception + mock_storage.exists.return_value = True + assert_equals('"/static/data_dir/file.png"', replace_static_urls(STATIC_SOURCE, DATA_DIRECTORY)) + + mock_storage.exists.return_value = False assert_equals('"/static/data_dir/file.png"', replace_static_urls(STATIC_SOURCE, DATA_DIRECTORY))