Skip to content

Commit

Permalink
Added feature to disable temp directory deletion in response to issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
jcohen02 committed Feb 17, 2019
1 parent 87004f5 commit cd52162
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions django_drf_filepond/drf_filepond_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@

UPLOAD_TMP = getattr(settings, _app_prefix+'UPLOAD_TMP',
os.path.join(settings.BASE_DIR,'filepond_uploads'))

DELETE_UPLOAD_TMP_DIRS = getattr(settings,
_app_prefix+'DELETE_UPLOAD_TMP_DIRS', True)
12 changes: 8 additions & 4 deletions django_drf_filepond/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from django.dispatch import receiver
from django.core.files.storage import FileSystemStorage

import django_drf_filepond.drf_filepond_settings as local_settings

FILEPOND_UPLOAD_TMP = getattr(settings, 'DJANGO_DRF_FILEPOND_UPLOAD_TMP',
os.path.join(settings.BASE_DIR,'filepond_uploads'))
storage = FileSystemStorage(location=FILEPOND_UPLOAD_TMP)
Expand Down Expand Up @@ -56,8 +58,10 @@ def delete_temp_upload_file(sender, instance, **kwargs):
os.path.isfile(instance.file.path)):
os.remove(instance.file.path)

file_dir = os.path.join(storage.location, instance.upload_id)
if(os.path.exists(file_dir) and os.path.isdir(file_dir)):
os.rmdir(file_dir)
LOG.debug('*** post_delete signal handler called. Deleting temp dir that contained file.')
if local_settings.DELETE_UPLOAD_TMP_DIRS:
file_dir = os.path.join(storage.location, instance.upload_id)
if(os.path.exists(file_dir) and os.path.isdir(file_dir)):
os.rmdir(file_dir)
LOG.debug('*** post_delete signal handler called. Deleting temp '
'dir that contained file.')

34 changes: 33 additions & 1 deletion tests/test_revert_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from django_drf_filepond import views
from django_drf_filepond.models import TemporaryUpload, storage
import django_drf_filepond.drf_filepond_settings as local_settings

LOG = logging.getLogger(__name__)

Expand All @@ -27,6 +28,16 @@ def setUp(self):
upload_type=TemporaryUpload.FILE_DATA)

def tearDown(self):
# Check that temp files in the storage directory have been removed
upload_dir_to_check = os.path.join(storage.location, self.upload_id)
upload_file_to_check = os.path.join(upload_dir_to_check, self.file_id)
if (os.path.exists(upload_file_to_check) and
os.path.isfile(upload_file_to_check)):
os.remove(upload_file_to_check)
if (os.path.exists(upload_dir_to_check) and
os.path.isdir(upload_dir_to_check)):
os.rmdir(upload_dir_to_check)

# If the base upload dir didn't exist at startup, we remove it now
if not self.base_upload_dir_at_startup:
if len(os.listdir(storage.location)) == 0:
Expand Down Expand Up @@ -64,4 +75,25 @@ def test_revert(self):

# Check that the file was removed
self.assertFalse(os.path.exists(file_path),
'The test file wasn\'t removed.')
'The test file wasn\'t removed.')

def test_revert_no_delete_dir(self):
local_settings.DELETE_UPLOAD_TMP_DIRS = False
# Check that our record is in the database
tu = TemporaryUpload.objects.get(upload_id=self.upload_id)

# Check that the file exists
file_path = tu.get_file_path()
self.assertTrue(os.path.exists(file_path),
'Test file to remove doesn\'t exist.')

response = self.client.delete(reverse('revert'),
data=str(self.upload_id), content_type='text/plain')
self.assertEqual(response.status_code, 204,
'Expecting no content response code.')

# Check that the file was removed but the directory was not
self.assertFalse(os.path.exists(file_path),
'The test file wasn\'t removed.')
self.assertTrue(os.path.exists(os.path.dirname(file_path)),
'The test file temp dir was unexpectedly removed.')

0 comments on commit cd52162

Please sign in to comment.