Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Switched django local storage to overwrite
Browse files Browse the repository at this point in the history
The default django local storage does not allow to overwrite files with
the same name and this is a problem for our static assets.
  • Loading branch information
Giovanni Di Milia committed Sep 18, 2015
1 parent f3b224d commit 1b31e5a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion learningresources/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_static_asset_filename_length(self):
FILE_PATH_MAX_LENGTH -
# minus the length of the base path of the Django storage and
# the base path of the file location temporary dir
len(os.path.join(base_path, temp_dir_path)) -
len('/'.join([base_path, temp_dir_path])) -
# minus 1 for the extra "/" to joint the paths
1
)
Expand Down
4 changes: 3 additions & 1 deletion lore/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def get_var(name, default):
if LORE_USE_S3:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
else:
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'
# by default use django.core.files.storage.FileSystemStorage with
# overwrite feature
DEFAULT_FILE_STORAGE = 'storages.backends.overwrite.OverwriteStorage'


# Lore preview settings
Expand Down
50 changes: 50 additions & 0 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Tests for django storage
"""
from __future__ import unicode_literals

import os
from shutil import rmtree
from tempfile import mkdtemp

from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.test.testcases import TestCase


class TestStorage(TestCase):
"""
Tests for Django storage
"""

def test_local_storage(self):
"""
Tests for local storage
It has to be OverwriteStorage to avoid problems with static assets
having the same name
"""

# default setting
self.assertEqual(
settings.DEFAULT_FILE_STORAGE,
'storages.backends.overwrite.OverwriteStorage'
)

temp_dir_path = mkdtemp()
self.addCleanup(rmtree, temp_dir_path)

path = os.path.join(temp_dir_path, 'temp_file.txt')

# create the first file
default_storage.save(path, ContentFile('nonsense content'))
# create another with the same name
content = 'nonsense content 2'
default_storage.save(path, ContentFile(content))

# there is only one file in the directory
self.assertEqual(len(os.listdir(temp_dir_path)), 1)

with open(path) as fhand:
content_read = fhand.read()
self.assertEqual(content, content_read)
2 changes: 1 addition & 1 deletion ui/tests/test_learningresources_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_serve_media(self):
"""Hit serve media"""
self.assertEqual(
settings.DEFAULT_FILE_STORAGE,
'django.core.files.storage.FileSystemStorage'
'storages.backends.overwrite.OverwriteStorage'
)
# upload a course
self.upload_test_file()
Expand Down

0 comments on commit 1b31e5a

Please sign in to comment.